locked
Script to Cancel Task is not working in CRM 2016 RRS feed

  • Question

  • Hello,

    We have a custom button to cancel Task. 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.

    Below is my script - is there anything different that's not supported in 2016 causing this not to work?

    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);
            }
        });
    
    }

    Friday, November 4, 2016 2:05 PM

All replies

  • Hello Triangular,

    Only for troubleshooting, Can you try to call this script directly on to form instead of calling this on Custom button click, and then try to debug the script or check whether function is calling at the time of event executing or not.

    If function is executing and having any unsupported property/function then we get that here.

    Hope this will help you.

    Thanks!!


    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    Wednesday, November 9, 2016 9:57 AM