[英]How to set the Opportunity Status - Dynamics CRM?
I'm trying to update the status of open opportunity by using the WinOpportunityRequest & LoseOpportunityRequest API provided in the MSDN. 我正在尝试使用MSDN中提供的WinOpportunityRequest和LoseOpportunityRequest API更新开放机会的状态。 I've followed the code which was given in the MSDN & I even referred to stackoverflow's Set Opportunity Status 我遵循了MSDN中给出的代码,甚至提到了stackoverflow的“ 设置机会状态”
But, When I run this following code for the open opportunity it throws error stating that 但是,当我为公开机会运行以下代码时,会引发错误,指出
LoseOpportunityRequest req = new LoseOpportunityRequest();
Entity opportunityClose = new Entity("opportunityclose");
opportunityClose.Attributes.Add("opportunityid", new EntityReference(OptyEntityName, new Guid("xxxx-xxx")));
opportunityClose.Attributes.Add("subject", "Lost the Opportunity!");
req.OpportunityClose = opportunityClose;
// 4 = Cancelled and 5 = Out-Sold
req.Status = new OptionSetValue(4);
LoseOpportunityResponse resp = (LoseOpportunityResponse)_serviceProxy.Execute(req);
Error - 错误-
4 is not a valid status code on opportunity with Id(Guid) Id(Guid)上的机会不是有效的状态码4
When I tried to change the status of the closed opportunity it says that opportunity is already closed . 当我尝试更改已关闭机会的状态时,它表示该机会已经关闭 。
One more thing to consider is this status in my CRM has a padlock icon that means it is locked. 需要考虑的另一件事是,我的CRM中的此状态带有一个挂锁图标,表示它已被锁定。
So is it possible to change the status or not and is it based on the role? 那么是否可以更改状态,是否基于角色?
For an open opportunity, we can change the status to either win or lose. 对于开放的机会,我们可以将状态更改为赢或输。 So we will use the WinOpportunityRequest and LoseOpportunityRequest in here . 因此,我们将在此处使用WinOpportunityRequest和LoseOpportunityRequest 。
So, we need to change the value to -1 so that CRM can load the default status code. 因此,我们需要将值更改为-1,以便CRM可以加载默认状态代码。
req.Status = new OptionSetValue(4);
after changing to -1 it doesn't throw any exception. 更改为-1后,不会引发任何异常。
req.Status = new OptionSetValue(-1);
once the execute call is performed. 一旦执行了执行调用。 The opportunity value will be changed to lost. 机会价值将变为丢失。 The opportunity will be closed. 机会将被关闭。
To re-open the closed opportunity, we can use the SetStateRequest class . 要重新打开已关闭的机会,我们可以使用SetStateRequest类 。 The code would be as follows. 代码如下。
var stateRef = new EntityReference("optyname", new Guid("optyid"));
SetStateRequest req = new SetStateRequest();
req.State = new OptionSetValue(0);
req.Status = new OptionSetValue(2);
req.EntityMoniker = stateRef;
SetStateResponse stateSet = (SetStateResponse)_serviceProxy.Execute(req);
After the execute call is performed the opportunity status is set back to open and the status is displayed as open. 执行执行调用后,机会状态将重新设置为打开,并且状态显示为打开。
state code is different from status. 状态码与状态不同。 state code can have open, win or close. 状态代码可以具有打开,赢得或关闭状态。 status can have multiple values. 状态可以有多个值。 detailed info is provide at msdn . 详细信息在msdn中提供。
You are right. 你是对的。 State & Status are conjoined twins. 状态和状态是双胞胎。 You cannot update only one of them, always go in pair. 您不能仅更新其中之一,请始终配对。
State = StateCode 状态= StateCode
Status Reason = StatusCode (field with Padlock) 状态原因= StatusCode(带有挂锁的字段)
In your answer code, this is framed correctly in SetStateRequest req. 在您的答案代码中,这在SetStateRequest req中正确构建。
req.State = new OptionSetValue(0);
req.Status = new OptionSetValue(2);
But in OP, you set only Status not State. 但是在OP中,仅设置状态而不是状态。
Per MSDN , LoseOpportunityRequest
with OpportunityClose
entity has to close it without issues when you pass only Status. 根据MSDN ,仅通过状态时,带有OpportunityClose
实体的LoseOpportunityRequest
必须关闭它而没有问题。 But you are not alone . 但是你并不孤单 。
Ref: Opportunity & OpportunityClose 参考: 机会与机会关闭
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.