Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Change state and status by script

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 671 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 167 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 138 Most Valuable Professional

Product updates

Dynamics 365 release plans