web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Validations in Model d...
Power Apps
Answered

Validations in Model driven app form

(0) ShareShare
ReportReport
Posted on by 223

Team,

 

I've a condition on the "Feedback Record Status" column say if "Status=Resolved" it checks if there is any pending tasks in "Active".If there is no "Active" tasks it auto populates the "closed Date" with todays date. And click save&Close.

 

But the issue here is if any "Active" tasks are open it will show a "Pop-up" message saying "Close all pending tasks" . However we can still make the status as "Resolved" and "Save & Close" the record which should not happen.

 

Can you please advise/update the attached script and restrict without closing the record if the tasks are in "Active" state.

################################################################

function checkTaskStatus(){

var id = Xrm.Page.data.entity.getId();

var feedbackRecordStatus = Xrm.Page.getAttribute("new_feedbackrecordstatus").getValue();

if (feedbackRecordStatus == 100000003) {
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/tasks?$filter=_regardingobjectid_value eq '" + id + "' and statecode eq 0&$count=true", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
var recordCount = results["@odata.count"];

 

if (recordCount > 0) {
Xrm.Utility.alertDialog("Please Complete All The Tasks.");
}
else {
var today = new Date();
today.setHours(0, 0, 0, 0);
Xrm.Page.getAttribute("new_closeddate").setValue(today);
}
for (var i = 0; i < results.value.length; i++) {
var activityid = results.value[i]["activityid"];
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
}


function futuredatevalidation() {
var fieldDate = Xrm.Page.getAttribute('new_closeddate').getValue();

if (fieldDate != null) {

fieldDate.setHours(0, 0, 0, 0);

var today = new Date();

today.setHours(0, 0, 0, 0);
if (fieldDate > today) {
Xrm.Page.getAttribute("new_closeddate").setValue(null);
Xrm.Utility.alertDialog("You can't enter future date");
}
}
}

#######################################################

@rampprakash 

Record Status.jpg
I have the same question (0)
  • Ram Prakash Duraisamy Profile Picture
    5,699 Super User 2026 Season 1 on at

    Hello @skolisetti,

     

    You are very close to it. The code which you have written will work Perfectly. 

     

    In the Form Level in ONSAVE operation call the function (checkTaskStatusOnSave)

     

    function checkTaskStatusOnSave() {
    var isErrorAvaialble = false;
    var id = Xrm.Page.data.entity.getId();
    var feedbackRecordStatus = Xrm.Page.getAttribute("new_feedbackrecordstatus").getValue();
    if (feedbackRecordStatus == 100000003) {
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/tasks?$filter=_regardingobjectid_value eq '" + id + "' and statecode eq 0&$count=true", false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var results = JSON.parse(this.response);
    var recordCount = results["@odata.count"];
    if (recordCount > 0) {
    Xrm.Utility.alertDialog("Please Complete All The Tasks.");
    isErrorAvaialble = true;
    }
    else {
    var today = new Date();
    today.setHours(0, 0, 0, 0);
    Xrm.Page.getAttribute("new_closeddate").setValue(today);
    }
    for (var i = 0; i < results.value.length; i++) {
    var activityid = results.value[i]["activityid"];
    }
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    }

    if (isErrorAvaialble) {
    Xrm.Page.context.getEventArgs.preventDefault();
    }
    }


    function futuredatevalidation() {
    var fieldDate = Xrm.Page.getAttribute('new_closeddate').getValue();

    if (fieldDate != null) {

    fieldDate.setHours(0, 0, 0, 0);

    var today = new Date();

    today.setHours(0, 0, 0, 0);
    if (fieldDate > today) {
    Xrm.Page.getAttribute("new_closeddate").setValue(null);
    Xrm.Utility.alertDialog("You can't enter future date");
    }
    }
    }

     

    i have declareed Global variable isErrorAvaialble and collecting only if Error occurs and Using PREVENT DEFALT to stop save at last.

     

    And You are using true in the Code which means its Async Call, after save only it will return result.

    Try Changing it to false to make it work Synchronously so that u will get result immediately. 

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/tasks?$filter=_regardingobjectid_value eq '" + id + "' and statecode eq 0&$count=true", false);

     

    Please mark as Answer if it is helpful and provide Kudos

     

    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA

    Blog : https://microsoftcrmtechie.blogspot.com

  • skvsp Profile Picture
    223 on at

    Ram thank you for your response. I've just pasted the updated code you have sent i'm getting script error as attached. And logs below can you advise ?

     

    ###################################################

     

    TypeError: Cannot read properties of undefined (reading 'preventDefault')
    at checkTaskStatusOnSave (https://orgbaf1b8ec.crm.dynamics.com/%7b637726326200000143%7d/webresources/new_checkTaskStatus:40:31)
    at y._executeFunctionInternal (https://orgbaf1b8ec.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:1980:5296)
    at y.execute (https://orgbaf1b8ec.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:1980:3765)
    at https://orgbaf1b8ec.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:147:26335
    at i (https://orgbaf1b8ec.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:308:88)
    at ee._executeIndividualEvent (https://orgbaf1b8ec.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:147:26309)
    at ee._executeEventHandler (https://orgbaf1b8ec.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:147:23314)
    at Object.execute (https://orgbaf1b8ec.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:147:22742)
    at N._executeSyncAction (https://orgbaf1b8ec.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:923:692)
    at N._executeSync (https://orgbaf1b8ec.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:923:419)

    Error Details:
    Event Name: onchange
    Function Name: checkTaskStatusOnSave
    Web Resource Name: new_checkTaskStatus
    Solution Name: Active
    Publisher Name: DefaultPublisherorgbaf1b8ec

    ######################################################

     

    script error.jpg
  • Ram Prakash Duraisamy Profile Picture
    5,699 Super User 2026 Season 1 on at

    Hello @skolisetti,

     

    You have called this function in ONCHANGE can you please change it to ONSAVE

     

    Event Name: onchange
    Function Name: checkTaskStatusOnSave
    Web Resource Name: new_checkTaskStatus
    Solution Name: Active
    Publisher Name: DefaultPublisherorgbaf1b8ec

     

    Please mark as Answer if it is helpful and provide Kudos

     

    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA

    Blog : https://microsoftcrmtechie.blogspot.com

  • skvsp Profile Picture
    223 on at

    Sorry still getting error.  Please see attached Field and Form properties change

    #####################################################

    TypeError: Cannot read properties of undefined (reading 'preventDefault')
    at checkTaskStatusOnSave (https://orgbaf1b8eb.crm.dynamics.com/%7b637726360670000143%7d/webresources/new_checkTaskStatus:40:31)
    at y._executeFunctionInternal (https://orgbaf1b8eb.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:1980:5296)
    at y.execute (https://orgbaf1b8eb.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:1980:3765)
    at https://orgbaf1b8eb.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:147:26335
    at i (https://orgbaf1b8eb.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:308:88)
    at ee._executeIndividualEvent (https://orgbaf1b8eb.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:147:26309)
    at ee._executeEventHandler (https://orgbaf1b8eb.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:147:23314)
    at Object.execute (https://orgbaf1b8eb.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:147:20830)
    at N._executeSyncAction (https://orgbaf1b8eb.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:923:692)
    at N._executeSync (https://orgbaf1b8eb.crm.dynamics.com/uclient/scripts/app.js?v=1.4.3458-2110.2:923:419)

    Error Details:
    Event Name: onsave
    Function Name: checkTaskStatusOnSave
    Web Resource Name: new_checkTaskStatus
    Solution Name: Active
    Publisher Name: DefaultPublisherorgbaf1b8eb

     

     

     

    Field and Form.jpg
  • Ram Prakash Duraisamy Profile Picture
    5,699 Super User 2026 Season 1 on at

    Hello @skolisetti,

     

    That was my wrong try using below code

     

    function checkTaskStatusOnSave(executionContext) {
    var isErrorAvaialble = false;
    var id = Xrm.Page.data.entity.getId();
    var feedbackRecordStatus = Xrm.Page.getAttribute("new_feedbackrecordstatus").getValue();
    if (feedbackRecordStatus == 100000003) {
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/tasks?$filter=_regardingobjectid_value eq '" + id + "' and statecode eq 0&$count=true", false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var results = JSON.parse(this.response);
    var recordCount = results["@odata.count"];
    if (recordCount > 0) {
    Xrm.Utility.alertDialog("Please Complete All The Tasks.");
    isErrorAvaialble = true;
    }
    else {
    var today = new Date();
    today.setHours(0, 0, 0, 0);
    Xrm.Page.getAttribute("new_closeddate").setValue(today);
    }
    for (var i = 0; i < results.value.length; i++) {
    var activityid = results.value[i]["activityid"];
    }
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    }

    if (isErrorAvaialble) {
    executionContext.getEventArgs().preventDefault();
    }
    }

    function OnSave() {
    Xrm.Page.context.getEventArgs.preventDefault();
    }

    function futuredatevalidation() {
    var fieldDate = Xrm.Page.getAttribute('new_closeddate').getValue();

    if (fieldDate != null) {

    fieldDate.setHours(0, 0, 0, 0);

    var today = new Date();

    today.setHours(0, 0, 0, 0);
    if (fieldDate > today) {
    Xrm.Page.getAttribute("new_closeddate").setValue(null);
    Xrm.Utility.alertDialog("You can't enter future date");
    }
    }

     

    Make Sure you are enabling this in onsave form properties

     

    rampprakash_0-1637041661767.png

     

    Please mark as Answer if it is helpful and provide Kudos

     

    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA

    Blog : https://microsoftcrmtechie.blogspot.com

     

  • skvsp Profile Picture
    223 on at

    Sorry no go . I've added the event in the "Form Properties" with Function checkTaskStatusOnSave .

    Created a record and saved getting an error.

     

     

    Web resource method does not exist: checkTaskStatusOnSave
    Session Id: 0bf9ccdd-d71a-45ae-a4e1-d403f0023214
    Correlation Id: d0e5c585-4485-4e3c-a68c-51acacdd478d
    Event Name: onsave
    Function Name: checkTaskStatusOnSave
    Web Resource Name: new_checkTaskStatus
    Solution Name: Active
    Publisher Name: DefaultPublisherorgbaf1b8eb
    Time: Tue Nov 16 2021 00:06:43 GMT-0600 (Central Standard Time)

    check task status updated.jpg
  • Verified answer
    Ram Prakash Duraisamy Profile Picture
    5,699 Super User 2026 Season 1 on at

    Hello @skolisetti,

     

    Ping me in private we will jump into a call to fix the same

     

    As per your error you didnt select the checkbox

    rampprakash_0-1637044859087.png

     

    Please mark as Answer if it is helpful and provide Kudos

     

    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA

    Blog : https://microsoftcrmtechie.blogspot.com

     

  • Ram Prakash Duraisamy Profile Picture
    5,699 Super User 2026 Season 1 on at

    Hello @skolisetti,

     

    is your issue fixed?

     

    Please mark as Answer if it is helpful and provide Kudos

     

    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA

    Blog : https://microsoftcrmtechie.blogspot.com

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 549 Most Valuable Professional

#2
Kalathiya Profile Picture

Kalathiya 225 Super User 2026 Season 1

#3
Haque Profile Picture

Haque 224

Last 30 days Overall leaderboard