Pages

Sunday, September 18, 2011

Automatically close incident in CRM 2011

In CRM 2011 the Javascript API received a good piece of an overhaul, and so there is a lot more you can do using Javascript.

One thing that's still a bit nasty is changing the state of the current forms entity using the API, and while you can still do that using a SOAP SetStateRequest, you will not get a good UI interaction.

In my case, I had to automatically close an incident, which also has to result in the creation of an Incident Resolution Activity.

The usual recommendation you get is to use the CloseIncidentRequest and use the SOAP Api accordingly, but I did feel, but the user would have to refresh the form manually in order to see that the action really was executed. So this was not the kind of experience I wanted our users to have, so I analyzed how Microsoft did this on the default Close Incident dialog and reused their implementation like this:

 // Close the incident.
// Xml has to be set to Hidden Input crActivityXml to create incidentresolution
// New statuscode has to be set to HiddenInput crNewStatus
var xml = "";
xml += "0";
xml += "";
xml += "Anfrage abgeschlossen";
xml += ""+Xrm.Page.data.entity.getId()+"";
xml += "";

var oActivityXml=createHiddenInput("crActivityXml",xml),oStatusCode=createHiddenInput("crNewStatus",5); //StatusCode "Behoben"
if(!crmForm.SubmitCrmForm(FormEventId_Deactivate,true,true,false)){
     deleteInput(oActivityXml);deleteInput(oStatusCode);
}

Here I am creating the IncidentResolution activity, it is done in the Close Incident Dialog, but instead of asking the User for Input, it is automatically set.

The IncidentResolution and the new state of this incident is set to hidden fields, and the Save operation of the Form is called.

This would also allow you to do some REST calls to calculate the Sum of the time used or doing gathering some other data for the incident resolution.

Be aware, that this solution uses unsupported API, so you might have to update your code in case of an change from Microsoft, but this gives you an excellent experience.

2 comments:

  1. Good info; do you know where the "There are still open activities.." alert box is coming from? I want to hack the JS and close the open activities before this dialog shows up (I tried writing a plugin but that idea failed).

    ReplyDelete
  2. Yes, it was intended to leave that in the solution in my case and you have to decide what todo about it.
    Microsoft CRM exepects you to have all open activities for an incident closed, before you can continue and close the incident.
    You have to decide, what you want to do about it:
    1. Leave it as it is: In most cases this is a very useful feature, it reminds you that you still have open tasks todo in this incident.
    2. You can use some javascript programming to automatically close all the activities attached to the incident. You can do that with some additional JavaScript calling the SOAP endpoints on CRM. But be careful, that you close the activities in a way where you can differentiate the activites that were closed by you automatically from those closed by a user on the end of a task.

    ReplyDelete