cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
skolisetti
Helper IV
Helper IV

Validations in Model driven app form

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 

1 ACCEPTED SOLUTION

Accepted Solutions
rampprakash
Super User
Super User

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

 

View solution in original post

8 REPLIES 8
rampprakash
Super User
Super User

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

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

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

 

rampprakash
Super User
Super User

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

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

 

 

 

rampprakash
Super User
Super User

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

 

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)

rampprakash
Super User
Super User

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

 

rampprakash
Super User
Super User

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

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

May UG Leader Call Carousel 768x460.png

June User Group Leader Call

Join us on June 28 for our monthly User Group leader call!

PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

Users online (1,486)