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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Update Entity through C#

(0) ShareShare
ReportReport
Posted on by 795

Hi All,

I am creating a record inside a custom entity through C# code and then i want to access the similar newly created record by fetching its id.I want to set that particular record to inactive state ..But its not working..I used the below lines of code -:

Id = service.Create(entity_name);
Console.WriteLine("Record Created ");
Entity contact_unsubscribe = new Entity("custom entity");
contact_unsubscribe = service.Retrieve("new_contact_unsubscribe", Id, new ColumnSet(true));
contact_unsubscribe["statecode"] = new OptionSetValue(1);
service.Update(contact_unsubscribe);

Any help please!

*This post is locked for comments

I have the same question (0)
  • Radu Chiribelea Profile Picture
    6,667 on at

    Hi there,

    Can you please give more details on what's not working?

    Do you see any error messages, or what is the behavior that results from the update?

    Thanks,

    Radu

  • Suggested answer
    Michel van den Brink Profile Picture
    4,697 on at

    Hello,

    Changing the State ('statecode') or Status ('statuscode') for an entity record has it's own dedicated request type. They can't be changed though the regular 'Update' request.

    You need to use the request type Microsoft.Crm.Sdk.Messages.SetStateRequest and pass it to 'Execute' on the organization service.
    Also, you always have to specify both the State ('statecode') and the Status ('statuscode') fields. Make sure that they are compatible with your entity settings, don't try to set a Status that belongs to a different State.

    Example:

    service.Execute(new SetStateRequest()
    {
        EntityMoniker = new EntityReference("new_contact_unsubscribe", id),
        State = new OptionSetValue(1), // 'statecode'
        Status = new OptionSetValue(2) // 'statuscode'
    });

    Hope this helps you out! If you have any more questions, let me know.

    If you found my answer helpful, please mark it as such/verified :-)

  • CRM Beginner  Profile Picture
    795 on at

    Thanks Michel. Can i know how to identify the statuscode and statecode for a custom entity ?

  • TomSmelser Profile Picture
    1,570 on at

    See here similar example. I think this will help.

    https://www.powerobjects.com/2012/03/30/how-to-programmatically-set-the-status-code-field-of-a-dynamic-entity/

    Good luck!

  • Verified answer
    Michel van den Brink Profile Picture
    4,697 on at

    Yes you can identify the different codes.

    1. For a custom entity there is always just two options for 'statecode' - you can not change these or add any.
      You can optionally change the labels

      0 = 'Active'
      1 = 'Inactive'

    2. For a custom entity there are by default two options for 'statuscode'. You can add any or change these if you like via customizations.

    State
    ('statecode')

    Status Reason
    ('statuscode')

    0 : Active

    1 : Active

    1 : Inactive

    2 : Inactive

    ---

    For the default (out-of-the-box) entities like 'Account', 'Contact' and 'Opportunity' there are many more combinations.
    Please refer to this document about all the different combinations: 
    https://technet.microsoft.com/en-us/library/dn531157.aspx

  • Suggested answer
    Radu Chiribelea Profile Picture
    6,667 on at

    Hello,

    With regards to the he SetStateRequest message, I would suggest to keep in mind that this is deprecated: msdn.microsoft.com/.../microsoft.crm.sdk.messages.setstaterequest.aspx

    Below you can see : This message request is deprecated. Consider using the UpdateRequest instead. For more information see Perform specialized operations using Update.

    Additional Info:

    msdn.microsoft.com/.../dn932124.aspx

    community.dynamics.com/.../status-not-getting-updated-using-setstaterequest-in-dynamics-365

    Hope this helps!

    Rad

  • David Jennaway Profile Picture
    14,065 on at

    To clarify whether to use SetStateRequest. Going back a few versions, you had to use SetStateRequest to change the statecode, as it wasn't supported to change statecode via an Update. However, in CRM 2015 SP1, it changed to allow you to change the statecode via an Update.

  • CRM Beginner  Profile Picture
    795 on at

    Thanks Michel, it worked.

  • CRM Beginner  Profile Picture
    795 on at

    @Michel, i want one more help..If i don't want to make the record inactive, just want to disable the form or read only state, how could i do it?

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    You need to write a JavaScript to make the form read only.

    here is the sample js code-

    ============

    function MakeReadOnlyForm() {

           Xrm.Page.ui.controls.forEach(function (control, i) {

               if (control && control.getDisabled && !control.getDisabled()) {

                   control.setDisabled(true);

               }

           });

       }

    ==================

    You refer the following links on the same-

    carldesouza.com/dynamics-crm-read-only-form-disable-fields-with-javascript

    community.dynamics.com/.../189988

    Hope this helps.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans