web
You’re offline. This is a read only version of the page.
close
Skip to main content
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
    Community Member Profile Picture
    on at
    RE: Update an active record to inactive

    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);

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Update an active record to inactive

    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
    Rahul-Waghmare Profile Picture
    990 on at
    RE: Update an active record to inactive

    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
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Update an active record to inactive

    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

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#3
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans