Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

De-Activate Records

Posted on by
Hello, I would like to de-activate records returned in my For Each .. Can someone please assist on how to do this ?

EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetch));
try { foreach (var cases in results.Entities) { if (cases.Attributes.Contains("createdon")) { DateTime newcreatedon = ((DateTime)(cases["createdon"])); if (newcreatedon != createdon) { // Start - Code for De-Activating records SetStateRequest setState = new SetStateRequest(); setState.EntityMoniker = cases.ToEntityReference(); setState.State = new OptionSetValue(); setState.State.Value = 1; setState.Status = new OptionSetValue(); setState.Status.Value = 2; SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);
service.Update(cases)
// End - Code for De-Activating records
                      } 
}
}
}

Many Thanks

*This post is locked for comments

  • Jay2014 Profile Picture
    Jay2014 on at
    RE: De-Activate Records

    Thank you all for your help !

  • Rajkumar Rajaraman Profile Picture
    Rajkumar Rajaraman 18,108 on at
    RE: De-Activate Records

    Try this:

    public static void DeactivateRecord(string entityName, Guid recordId, IOrganizationService organizationService)

    {

       var cols = new ColumnSet(new[] { "statecode", "statuscode" });

       //Check if it is Active or not

       var entity = organizationService.Retrieve(entityName, recordId, cols);

       if (entity != null && entity.GetAttributeValue<OptionSetValue>("statecode").Value == 0)

       {

           //StateCode = 1 and StatusCode = 2 for deactivating Account or Contact

           SetStateRequest setStateRequest = new SetStateRequest()

           {

               EntityMoniker = new EntityReference

               {

                   Id = recordId,

                   LogicalName = entityName,

               },

               State = new OptionSetValue(1),

               Status = new OptionSetValue(2)

           };

           organizationService.Execute(setStateRequest);

       }

    }

  • Verified answer
    anilambadan Profile Picture
    anilambadan 1,160 on at
    RE: De-Activate Records

    Please pass the values as 1 & 2. You can assign it to the variable or you can hardcord it.

  • Jay2014 Profile Picture
    Jay2014 on at
    RE: De-Activate Records

    Thanks a lot Anil, the state and status value have not been declared ?

    Optionset.PNG

  • Verified answer
    anilambadan Profile Picture
    anilambadan 1,160 on at
    RE: De-Activate Records

    Please try to change the code like below

    var request = new SetStateRequest()

    {

    EntityMoniker = new EntityReference

    {

    Id = cases.ToEntityReference().Id,

    LogicalName = cases.ToEntityReference().LogicalName

    },

    State = new OptionSetValue(stateValue),

    Status = new OptionSetValue(statusValue)

    };

    service.Execute(request);

    thanks

    Anil

  • Rajkumar Rajaraman Profile Picture
    Rajkumar Rajaraman 18,108 on at
    RE: De-Activate Records

    Jay,

    Refer this links:

    msdynamicscrmblog.wordpress.com/.../comment-page-1

    srmscrm.wordpress.com/.../plugin-code-to-deactivate-entity-record-ms-crm-2011

    Hope this helps.

    Regards,

    R.Rajkumar

    "Please mark my answer as verified if you found it helpful"

  • Jay2014 Profile Picture
    Jay2014 on at
    RE: De-Activate Records

    Thank you both, i have removed the update but its still not working .. Help please ?

  • Suggested answer
    Mamatha Swamy Profile Picture
    Mamatha Swamy 5,422 on at
    RE: De-Activate Records

    service.Execute(setState) will deactivate the record. You don't need service.Update(cases) after that.

  • Royal King Profile Picture
    Royal King 27,686 on at
    RE: De-Activate Records

    you are calling update after de-activating the record, it will throw an error because you can't update deactivated record. if you want to update the the record call update before deactivating the record.

    service.Update(cases);

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans