web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Answered

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

(0) ShareShare
ReportReport
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));

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    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.

  • Suggested answer
    meelamri Profile Picture
    13,218 User Group Leader on at

    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.

  • whydoihavetocreateanotherlogin Profile Picture
    25 on at

    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!

  • whydoihavetocreateanotherlogin Profile Picture
    25 on at

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

  • Community Member Profile Picture
    on at

    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.

  • whydoihavetocreateanotherlogin Profile Picture
    25 on at

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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
NeerajPawar Profile Picture

NeerajPawar 31

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 21 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 16

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans