Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin with SetStateRequest in Create Record

(0) ShareShare
ReportReport
Posted on by

I'm creating a plugin with SetStateRequest inside. The plugin registered on Create message in Case Entity. The idea is to set the Status (Cancelled) of new Case if meets some criteria. 

Here the code:

if (!String.IsNullOrEmpty(customer_name))
                    {
                        QueryExpression query_contacy = new QueryExpression("new_listinternal");
                        query_contacy.ColumnSet = new ColumnSet(true);

                        EntityCollection ent = _service.RetrieveMultiple(query_contacy);
                        foreach (Entity e in ent.Entities)
                        {
                            if (e.Attributes.Contains("new_email"))
                            {
                                 emil = e.GetAttributeValue<String>("new_email");
                                   
                                 if(customer_name.Equals(emil))
                                 {
                                     
                                     try
                                     {
                                         SetStateRequest stateReq = new SetStateRequest();
                                         stateReq.EntityMoniker = (EntityReference)entity.ToEntityReference();
                                         stateReq.State = new OptionSetValue(2);
                                         stateReq.Status = new OptionSetValue(6);

                                         SetStateResponse response = new SetStateResponse();
                                         response = (SetStateResponse)_service.Execute(stateReq);
                                     }
                                     catch (Exception ex)
                                     {
                                         throw new InvalidPluginExecutionException(ex.Message);
                                     }
                                 }
                            }
                        }
                    }


When i test create a record that meets the condition. It's throw an error that says the Guid Of the records doesn't exist.

<ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic">
<KeyValuePairOfstringanyType>
<d2p1:key>OperationStatus</d2p1:key>
<d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:string">0</d2p1:value>
</KeyValuePairOfstringanyType>
<KeyValuePairOfstringanyType>
<d2p1:key>SubErrorCode</d2p1:key>
<d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:string">-2146233088</d2p1:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>incident With Id = 017e981c-5db2-e711-80be-a4934caa8811 Does Not Exist</Message>
<Timestamp>2017-10-16T10:31:05.1266682Z</Timestamp>
<ExceptionSource i:nil="true" />
<InnerFault i:nil="true" />
<OriginalException i:nil="true" />
<TraceText>

*This post is locked for comments

  • Verified answer
    RE: Plugin with SetStateRequest in Create Record

    You have to use the following code in post update operation:

    SetStateIncidentRequest request = new SetStateIncidentRequest()
    {
    IncidentState = new OptionSetValue(2), // Cancelled
    IncidentStatus = new OptionSetValue(6), // Cancelled
    EntityId = new EntityReference("incident", incidentId)
    }

    If you found the answer helpful, please mark as Verified 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna, ITALY

    Independent Contractor

    http://www.francescopicchi.com 

  • Verified answer
    Alessandro Graps Profile Picture
    Alessandro Graps 2,664 on at
    RE: Plugin with SetStateRequest in Create Record

    Check if it is in PRE or POST operation. You must run plugin in post operation.

    Thanks

    A.G.

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Plugin with SetStateRequest in Create Record

    Can you try making it an asynchronous plugin?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans