My experiences with CRM, USD and Family

Menu

Stop session close incase of Unsaved changes in the entity record.

It’s been pretty long since I touched my blog. It’s all the new role I took up and crazy work life. Anyways, with the inspiration stolen from Mr. Parkhurst the MVP, the USD “geek” – I will try to be in touch with my readers as much as possible through my posts and comments.
One of the previous projects, the requirement is something like – “Agents should be alerted before closing the session in case of any unsaved changes are there in the Contact/account/Case form they are working on”. The OOB behavior of USD is, it will through “Leave/Stay on page” dialog and not even waits for user to respond rather it will simply kills all tabs. Unfortunately, we are not able to find a solution for this problem at that time and so we ended up “masking” the requirement with some generic warning.
In some investigation & reading today, I got this idea which brings the absolute fit solution for this problem. Note that, this is bit of lengthy solution for now – I’m still trying to make it more simpler and will let you all know in a different post if I got succeeded. The good part is, It’s a “NO CODE” solution, which many of my friends out there like a lot 🙂
The solution involves,
a)Create action call for each entity you want to check for changes
b)Attach action call to “session close requested” event
c)Alert user with display message if any unsaved changes found and stop closing session.
Note, For the sake of the blog post, I have considered Account form changes. You need to repeat the same for all other required entities in your project.

Create Action call to check if the form is Dirty.

Use the RunXrmCommand action of “Account” hosted control and call the following script which simply checks if the form is Dirty and returns Yes/No. (At this juncture, take a second and Refer this https://neilparkhurst.com/2016/03/26/usd-return-results-from-a-runxrmcommand/ wonderful blog of Neil on RunXrmCommand to know how to use this action and how to take the result from Action).

function IsAccountDirty() {

return Xrm.Page.data.entity.getIsDirty()==true? “yes”:”no”;

}

IsAccountDirty();

Note: I don’t know if it is a bug or what, but the RunXrmCommand is returning result only if the return value of the command is string. Henceforth, I’m using If condition and returning Yes/No.

Attach Action call to “SessionCloseRequested” Event.

This is the event USD automatically fires whenever user closes a session. If you are using predefined configurations, You might have already observed MS is using this event to check notes is saved or not and then closing the session. We are going to use the same event to check our Account form status. Attach the action call to the event as shown below.
So, whenever this event executed it will set the $Return.IsAccountDirty parameter with Yes/No based on the Unsaved changes in the Account form. (FYR, here is a quick screenshot of debugger).

Stop session closing and showing an Alert to user.

Create a new action call with below information to display the message to user if the form is “Unsaved”.Name : Tell user about Unsaved changes in AccountHosted Control : CRM Global ManagerAction : Display MessageData : Hey, Account form has unsaved changes. You can’t close the session like this.. 🙂 (Or whatever the message you want to show)Condition : “[[$Return.IsAccountDirty]+]”==”yes” (Meaning, we want to execute the action call and show the message only if IsAccountDirty is “yes”.)
Attach this action call to the same “SessionCloseRequested” event as shown below.
Next, Stopping the Session close. Navigate to action calls, find for the action call named “Close Session”. Add a condition to check if the AccountIsDirty which stops the execution of action as shown below.
That’s it, we are done. Open USD, Start a session change something on the account and try to close the session without saving the changes ..
Wolaa.. 🙂
Hope this helps you guys, All the best !! Please share your thoughts through comments !!