Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Answered

Close (set to complete) task using javascript from incident page.

Posted on by 25

Hey guys, 

I'm using CRM 2015 on prem.

From the case webpage, I want my users to close all open tasks using a button (on my webressource)

When i attempt to close a task, I'm getting a "web 500 error"  Iif i change the status to something else it works fine (differed or in progress) 

code is fairly straightforward... (see bellow)

I wonder if its because i need to update statuscode And Statecode at the same time, (statuscode is not in tasks, and statecode is not in activitypointer) 

activityId='98ec5ed5-ace6-eb11-813e-00155d012d09'
var entity = {};
entity.StatusCode = { Value: 4 };
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/TaskSet(guid'"+activityId+"')", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("X-HTTP-Method", "MERGE");
req.send(JSON.stringify(entity));

  • RE: Close (set to complete) task using javascript from incident page.

    So, from what I understand (i'm no a pro)

    If  you are on CRM Online UPdate 1 or 2016 then your examples will work, if you are running an older like CRM 2015 On premise (or less) then you cannot simply update statecode, you need to use "SetStateRequest" of the entity record.

    in order to update State code we need to SetStatus through a XML Soap "execute" (instead of normal http web request post)

    there is a function called "setState" in the XrmServiceToolkit.js that will do that. but it was returning some warnings for me.

    I used bits from there and build a simple (less flexible) function ...

    var setState = function (entityName, id, stateCode, statusCode) { 
        ///
        ///
        ///
        ///
        var requestType = "Execute"
        var soapBody = [
            "",
            "",
            "",
            "EntityMoniker",
            "",
            "", id, "",
            "", entityName, "",
            "",
            "",
            "",
            "",
            "State",
            "",
            "", stateCode.toString(), "",
            "",
            "",
            "",
            "Status",
            "",
            "", statusCode.toString(), "",
            "",
            "",
            "",
            "",
            "SetState",
            ""
        ].join("");
    
        var soapXml = ["",
        "",
        "<", requestType, " xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>",soapBody, "",
        "",
        ""
        ].join("");
    
        var req = new XMLHttpRequest();
        req.open("POST",  Xrm.Page.context.getClientUrl()   "/XRMServices/2011/Organization.svc/web", false);
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/"   requestType);            
        req.send(soapXml);    
        
        if (req.status === 200) {        
            return "ok"; //req.responseXML;               
        } else {
            return "error";
        }
    };

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Close (set to complete) task using javascript from incident page.

    Hi partner,

    Maybe you can refer following linkd:

    Microsoft Dynamics CRM Online 2016: Cannot set task/phonecall statecode to 'Completed' - Stack Overflow

    Setting 'statuscode' throws exception · Issue #24 · AlexaCRM/php-crm-toolkit · GitHub

    (+) ——— is not a valid status code for state code entity - Microsoft Dynamics CRM Community

    And i test code again with only StateCode , it can also work.

    8551.pastedimage1626837606822v1.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • RE: Close (set to complete) task using javascript from incident page.

    Looks like i spoke too soon.

    i'm not sure why i thought it worked once, but i can't get it going again :(

    when I check response value from the request i get this message:

    5 is not a valid status code for state code TaskState.Open on task with Id a5c53649-5ae9-eb11-813e-00155d012d09

    it as if it is ignoring my StateCode value change.

    I am under the impression that StateCode might be readonly and I have to use some other method to change it.  (in c# i  use "a SetStateRequest" ) ... not sure how to do this in JS though...

    var activityId = 'A5C53649-5AE9-EB11-813E-00155D012D09'                        

               var entity = {};    

               entity.StateCode =  { Value: 1 };

               entity.StatusCode = { Value: 5 };            

               var req = new XMLHttpRequest();

               req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/TaskSet(guid'"+activityId+"')", true);

               req.setRequestHeader("Accept", "application/json");

               req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               req.setRequestHeader("X-HTTP-Method", "MERGE");

               req.send(JSON.stringify(entity));

  • RE: Close (set to complete) task using javascript from incident page.

    omg... I should have tried that !!

    I suspected I needed to change both values, but I used CRM Rest Builder and Statecode was not in the list of attributes...   So i figured it was not available... I Should have known!

    Serves me right for working into the wee hours of the night instead of getting descent sleep :)

    Thanks a bunch it works like a charm now!

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Close (set to complete) task using javascript from incident page.

    Hi,

    This is the normal behavior of the CRM. In fact, you should always consider changing the two values 'statecode' and 'statuscode' at the same time.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Close (set to complete) task using javascript from incident page.

    Hi parter,

    From the following document, you can view statecode and statuscode value of task.

    4137.pastedimage1626765072182v1.png

    task EntityType (Microsoft.Dynamics.CRM) | Microsoft Docs

    If you want to close task (set to complete), you need set statecode and statuscode with correct value at the same time, just like following screenshot:

    5734.pastedimage1626765259320v2.png

    var entity = {};
    entity.statecode = 1;
    entity.statuscode = 5;
    var req = new XMLHttpRequest();
    req.open("PATCH", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/tasks(f7d48976-7bb7-eb11-8236-0022480a07ec)", 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.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                //Success - No Return Data - Do Something
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans