【发布时间】:2015-04-27 20:43:59
【问题描述】:
我需要能够仅在相关机会具有statecode 或1 的条件下执行我的代码
在我的代码中,我可以使用 Microsoft Dynamics SDK 提供的 GenerateSalesOrderFromOpportunityRequest 类在创建机会关闭活动时创建新的销售订单。
这种方法的缺点是,当机会以赢得(1) 或丢失(2) 关闭时,系统会创建opportunityclose 活动。此外,opportunityclose 活动中没有属性表明它是赢还是输。因此,找出它的唯一方法是从相关机会中获取该属性。
在我的代码中,我能够从相关机会中获取其他属性,例如 name,但除了 0 之外,我无法获得 statecode 的任何其他值。
这是我的代码:
Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
if (postImageEntity.LogicalName == "opportunityclose" && postImageEntity.Attributes.Contains("opportunityid") && postImageEntity.Attributes["opportunityid"] != null)
{
// Create an entity reference for the related opportunity to get the id for the GenerateSalesOrderFromOpportunityRequest class
EntityReference entityRef = (EntityReference)postImageEntity.Attributes["opportunityid"];
// Retrieve the opportunity that the closed opportunity activity was created for.
Entity RelatedEntityRef = service.Retrieve("opportunity", entityRef.Id, new ColumnSet( new String[] {"statecode","statuscode", "name"}));
OptionSetValue StateCode = (OptionSetValue)RelatedEntityRef.Attributes["statecode"];
OptionSetValue StatusCode = (OptionSetValue)RelatedEntityRef.Attributes["statuscode"];
string OppName = (string)RelatedEntityRef.Attributes["name"];
if (entityRef.LogicalName == "opportunity" && StateCode.Value == 1)
{
try
{
GenerateSalesOrderFromOpportunityRequest req = new GenerateSalesOrderFromOpportunityRequest();
req.OpportunityId = entityRef.Id;
req.ColumnSet = new ColumnSet(true);
GenerateSalesOrderFromOpportunityResponse resp = (GenerateSalesOrderFromOpportunityResponse)service.Execute(req);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
回顾:为此,我只需要能够获得与opportunityclose 相关的opportunity 的实际statecode 值。目前我只能得到0,即使我知道机会的状态代码是1。
其他信息:
- 这适用于 Microsoft Dynamics Online 2013/2015(两者都适用)
- 使用 SKD v6.1.1
- 插件有效,但无论机会是赢还是输都会触发。 (无意)
【问题讨论】:
标签: c# plugins entity dynamics-crm dynamics-crm-2013