Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Change state code and status code at the same time

Posted on by Microsoft Employee


Good afternoon. I'm new to programming in dynamics, and I'm trying to simultaneously change the state code and status reason of a particular record. The code is as follows:

ChangeStatusCodeOnApproval: function (primaryControl) {

debugger;

primaryControl.getAttribute ("statecode"). setValue (1);

primaryControl.getAttribute ("statuscode"). setValue (2);

primaryControl.data.refresh (true); }

However, it is not happening as planned. There is only the statecode change, but not the status code change and the refresh does not happen. Any suggestion?

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Change state code and status code at the same time

    Hi partner,

    1. You could use this js code to do this. I've tried in my instance and it worked well.

    function setvalue(executionContext){
    var b=executionContext.getFormContext().getAttribute("statuscode");
    var bvalue=b.getValue();
    b.setValue(100000001);
    }


    In this js function we used "executionContext" parameter so that we should select "Pass execution context as first parameter" option when add function to the form.

    community316.png

    You should pay attention that if the value of other options are correct. For example, in my status code field, "wait" option's value is 100,000,001.

    community316.png

    community317.png

    2.You could try to use webapi to update the status code.

    var entity = {};
    entity.statuscode = 2;
    entity.statecode = 1;
    
    var req = new XMLHttpRequest();
    req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts(0000000-0000-0000-0000-000000000000)", 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) {
                
            }
            else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));


    https://community.dynamics.com/crm/f/117/t/250502

    Hope it helps.

    Best Regards,

    Leo

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Change state code and status code at the same time

    Hello,

    I would recommend to update data using WebApi - docs.microsoft.com/.../updaterecord

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

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