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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to use SetStateRequest with multipleRequest in CRM C#

(0) ShareShare
ReportReport
Posted on by 45

Hi All,

I need some help, I want to use "SetStateRequest" to Active\Deactive records in entity but i want to use "SetStateRequest" with "ExecuteMultipleRequest" and currently i am using below code which is Deactivating  records one by one. So my requirement is that, i want to bulk Active\Deactive record in CRM using "ExecuteMultipleRequest".

SetStateRequest req = new SetStateRequest();

req.EntityMoniker = new EntityReference("contact" ,entity.Id);

req.State = new OptionSetValue(1);

req.Status = new OptionSetValue(2);

service.Execute(req);

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Dhiraj,

    I don't think you can pass multiple record in SetStateRequest. You can use for loop for multiple record.

  • Suggested answer
    Psyionic Profile Picture
    195 on at

    Hi Dhiraj,

    You can use code below:

    var requestWithResults = new ExecuteMultipleRequest()
        {
            // Assign settings that define execution behavior: continue on error, return responses. 
            Settings = new ExecuteMultipleSettings()
            {
                ContinueOnError = false,
                ReturnResponses = true
            },
            // Create an empty organization request collection.
            Requests = new OrganizationRequestCollection()
        };
    
        // Add a SetStateRequest for each entity to the request collection. Contacts is entity collection with all your contacts
        foreach (var entity in Contacts.Entities)
        {
            SetStateRequest req = new SetStateRequest();
            req.EntityMoniker = new EntityReference("contact" ,entity.Id);
            req.State = new OptionSetValue(1);
            req.Status = new OptionSetValue(2);
            requestWithResults.Requests.Add(req);
        }
    
        // Execute all the requests in the request collection using a single web method call.
        ExecuteMultipleResponse responseWithResults =
            (ExecuteMultipleResponse)service.Execute(requestWithResults);


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

    Hi Dhiraj,

    You can use ExecuteMultipleRequest just like you would use it for any other message (create/ update, delete etc). You just need to add your setstaterequest onject to the executemultiplerequest collection using a loop and then execute the multiple request.

    The above example should work for you.

    Hope this helps.

  • Emre GULCAN Profile Picture
    2,379 on at

    Hi,

    Additional to answers, please be aware of that, "SetStateRequest" deprecated and you should use "UpdateRequest" instead of it, please find details on msdn.microsoft.com/.../dn932124.aspx

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    You should be able to call an Update on the record in order to set the State:

    Entity entity = new Entity("contact");

    entity.Id = new entityId;

    entity["statecode"] = new OptionSetValue(1);

    entity["statuscode"] = new OptionSetValue(2);

    service.Update(entity);

    Try it out. This should work with Dynamics CRM 2016 and Execute Multiple Request.

    Hope this helps.

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Dhiraj,

    You can use ExecuteMultipleRequest with UpdateRequests to set the statecode and statuscode values.

    See the link below for sample code.

    msdn.microsoft.com/.../jj863604.aspx

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
JS-09031509-0 Profile Picture

JS-09031509-0 3

#3
Ciprian  P Profile Picture

Ciprian P 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans