Dynamics Crm online 2016 set opportunity statecode

2019-07-28 16:21发布

问题:

I am looking for the C# (console application) syntax required to set the statecode of an opportunity to "not active" or "closed". In fact I would also like to know where I can find the available values for statecode, since in the field properties I see that the datatype is "Status", but I don't see the available values.

Thank you in advance

回答1:

Valid state codes for Opportunity:

statecode        -   statuscode 
0 (Open)         -   1 (In Progress), 2 (On Hold)
1 (Won)          -   3 (Won)
2 (Lost)         -   4 (Cancelled), 5 (Out-Sold)

Because you want to "Close" the opportunity, use LoseOpportunityRequest SDK message to "Cancel" the opportunity.



回答2:

You need to use the LoseOpportunityRequest to change the status. There is also a WonOpportunityRequest. As part of the changing to a closed-lost status you need to create an opportunityclose entity, which is part of the LoseOpportunityRequest processing.

LoseOpportunityRequest req = new LoseOpportunityRequest();

Entity opportunityClose = new Entity("opportunityclose");
opportunityClose.Attributes.Add("opportunityid", new EntityReference("opportunity", new Guid("D711C1BD-23DA-E011-94B4-1CC1DEF177C2")));
opportunityClose.Attributes.Add("subject", "Lost the Opportunity!");
req.OpportunityClose = opportunityClose;
// 4 = Cancelled and 5 = Out-Sold
req.Status = new OptionSetValue(4);
LoseOpportunityResponse resp = (LoseOpportunityResponse)_orgService.Execute(req);

Credit to https://community.dynamics.com/crm/b/mileyja/archive/2011/09/08/close-an-opportunity-as-lost-using-net-or-jscript-in-microsoft-dynamics-crm-2011-with-loseopportunityrequest