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 an active record to inactive

(0) ShareShare
ReportReport
Posted on by

Hi all,

I have an entity with active and inactive records. I want to update the state of the record(from active to inactive).

For this I have written the code:

public  static void CheckIfInworkActions(ICrmContext context)
        {
           QueryExpression exe= new QueryExpression(avd_agenthandlingtime.EntityLogicalName);

       exe.ColumnSet.AddColumn(XrmAttributes.AgentHandlingTime.statecode);
            exe.Criteria.AddCondition(XrmAttributes.StateCode, ConditionOperator.Equal, 0);
          

            var coll1 = context.OrganizationService.RetrieveMultiple(exe);

            foreach (Entity e in coll1.Entities)
            {
                if (coll1 != null && coll1.Entities != null && coll1.Entities.Count > 0)
                {
                   e.Attributes["statecode"] = new OptionSetValue((int)1);

                    context.OrganizationService.Update(e);

                }


            }

        }

can any one suggest me where I am going wrong?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Your code looks good. What version are you on? Direct update of statecode to change state appeared in 2015. To change state for sure try to use SetState message - stackoverflow.com/.../how-to-change-state-status-in-crm-2013-using-c-sharp

  • Suggested answer
    Rahul-Waghmare Profile Picture
    990 on at

    Hi,

    This is not the right way to change the status of entity record in CRM. You need to use setstaterequest & entitymoniker, like below.

    _sdk.Execute(new SetStateRequest

    {

       EntityMoniker = new EntityReference("contact", contactId),

       State = new OptionSetValue(1),

       Status = new OptionSetValue(-1)

    });

    Regards,

    Rahul

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    I have used this code and worked well for me.

    SetStateRequest setStateRequest = new SetStateRequest()
            {
                EntityMoniker = new EntityReference
                {
                    Id = recordId,
                    LogicalName = entityName,
                },
                State = new OptionSetValue(1),
                Status = new OptionSetValue(2)
            };
            organizationService.Execute(setStateRequest);


    here is the ref link : https://msdynamicscrmblog.wordpress.com/2013/05/02/activatedeactivate-a-record-using-c-in-dynamics-crm-2011/comment-page-1/

    Regards

  • Suggested answer
    Community Member Profile Picture
    on at

    hi,

    I have used this code, You need to use entitymoniker

    SetStateRequest setStateRequest = new SetStateRequest()

                       {

                           EntityMoniker = new EntityReference

                           {

                               Id = EntityId,

                               LogicalName = "yourEntity",

                           },

                           State = new OptionSetValue(1),

                           Status = new OptionSetValue(2)

                       };

                       _orgService.Execute(setStateRequest);

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