0

We have a custom button to cancel Task in CRM. The custom button triggers a javascript that cancels the Task. We have created the script based on a blog by Guru Prasad.

It used to work in CRM 2013 but after we upgraded to CRM 2016 it's not working anymore. Clicking the button will not do anything - no error message, etc. This is not working because of the new Turbo Form feature in CRM 2016. When I turned off Turbo Form to use legacy form, the cancel Task button is working fine.

Below is my script - do you know if there's anything that prevents the script to work with CRM 2016 Turbo Form feature?

PS: Initially the code in Guru Prasad's blog is still using getServerUrl(), but as per msdn link it has been deprecated and changed to getClientUrl() which we have updated our script - but still not working.

function cancelTaskRecord(RECORD_ID) {
    //set Task record to Cancel status, then force to close the page
    Xrm.Page.data.save().then(changeRecordStatus(RECORD_ID, 2, 6), Xrm.Page.ui.close());
}

function changeRecordStatus(RECORD_ID, stateCode, statusCode)
    {
    // create the SetState request
    var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    request += "<s:Body>";
    request += "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    request += "<request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    request += "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>EntityMoniker</c:key>";
    request += "<c:value i:type=\"a:EntityReference\">";
    request += "<a:Id>" + RECORD_ID + "</a:Id>";
    request += "<a:LogicalName>task</a:LogicalName>";
    request += "<a:Name i:nil=\"true\" />";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>State</c:key>";
    request += "<c:value i:type=\"a:OptionSetValue\">";
    request += "<a:Value>" + stateCode + "</a:Value>";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>Status</c:key>";
    request += "<c:value i:type=\"a:OptionSetValue\">";
    request += "<a:Value>" + statusCode + "</a:Value>";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "</a:Parameters>";
    request += "<a:RequestId i:nil=\"true\" />";
    request += "<a:RequestName>SetState</a:RequestName>";
    request += "</request>";
    request += "</Execute>";
    request += "</s:Body>";
    request += "</s:Envelope>";

    //send set state request
    $.ajax({
        type: "POST",
        contentType: "text/xml; charset=utf-8",
        datatype: "xml",
        url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web",
        data: request,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
            XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            Xrm.Page.ui.close();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });

}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
ichachan
  • 637
  • 1
  • 10
  • 34
  • Nothing obvious, looks like it should work. Did you check to see if your code is even triggering? `save().then` would only work if there aren't any validation errors or if there is custom code that prevents a save. – dynamicallyCRM Nov 07 '16 at 21:59
  • 1
    I sneaked in an alert pop-up window on my script and it triggers, regardless turbo form is on or not. It is the Cancelling Task part that does not work. The Cancelling Task only works when turbo form is off. Is there anything on my script that doesn't work when turbo form is active? Also I removed the save().then portion on my script and it still does not cancel anything when turbo form is active. – ichachan Nov 08 '16 at 19:47

1 Answers1

1

In your comment you've mentioned that the code is being triggered in both cases. This means that your issue is with the actual Jquery call to perform the SetState request. Your next step is two fold, are you actually sending the request in both cases (Is Jquery getting loaded correctly), and is the request that is being sent identical. If you have an alert, you can place a break point in the code and ensure that jquery is working in both cases. You can also install fiddler and compare the requests in both Turbo Forms and legacy to see if the requests are identical or not.

Daryl
  • 18,592
  • 9
  • 78
  • 145