Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Unanswered

Change state and status by script

Posted on by 55

Hello, I became aware of something in Dynamics programming through C#. It is about setting the state as well as status code through the script.

Although all permissions are given for both read and write, I get the exception/error : "This entity is readonly." The setting of the state and status code is done as follows:

entity["statecode"] = new OptionSetValue(0);
entity["statuscode"] = new OptionSetValue(1);
service.Update(entity);


The interesting thing is that this way works in many other examples... but not for the "invoice" entity. So what is being done? I tried this with the SetStateRequest class - which also eventually led to success.

SetStateRequest setStateRequest = new SetStateRequest()
{
    
    EntityMoniker = new EntityReference
    {
        Id = invoice.Id,
        LogicalName = "invoice",
    },
    State = new OptionSetValue(0),
    Status = new OptionSetValue(1)
};

service.Execute(setStateRequest);

Now my question about this - what is the crucial reason that the top lines of code point to "readonly" and the bottom one works. Is this possibly just a "bug" or is there something behind it?

Thank you and have a pleasant day.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans