在Dynamics CRM中,潜在客户实体同时具有状态和状态原因。使用API我可以获得所有状态原因。我被绊倒的地方是当我的用户选择状态原因时我想向后工作并找出哪个状态与所选状态原因相关联。
以下是我获得所有状态原因的方法:
//get the list of status reasons
RetrieveAttributeRequest request = new RetrieveAttributeRequest();
request.EntityLogicalName = "lead";
request.LogicalName = "statuscode";
RetrieveAttributeResponse response = RetrieveAttributeResponse)theOrgContext.Execute(request);
StatusAttributeMetadata picklist = (StatusAttributeMetadata)response.AttributeMetadata;
foreach (OptionMetadata option in picklist.OptionSet.Options)
{
retval.ListOfStatuses.Add(option.Value.Value, option.Label.UserLocalizedLabel.Label.ToString());
}
要更新实体,我只使用LINQ:
//set the status to the new value
theLead.StatusCode.Value = int.Parse(statusValue);
theLead.StateCode = ???
//mark the object as updated
theContext.UpdateObject(theLead);
//persist the changes back to the CRM system
theContext.SaveChanges();
我只是无法弄清楚如何查询CRM以确定我需要为???添加什么值?
答案 0 :(得分:3)
您可以检索状态信息。
RetrieveAttributeRequest req = new RetrieveAttributeRequest();
req.EntityLogicalName = "lead";
req.LogicalName = "statuscode";
req.RetrieveAsIfPublished = true;
RetrieveAttributeResponse res = (RetrieveAttributeResponse)yourContext.Execute(req);
StatusAttributeMetadata attribute = (StatusAttributeMetadata)res.AttributeMetadata;
foreach (StatusOptionMetadata oStatusOptionMetaData in attribute.OptionSet.Options)
{
var state = oStatusOptionMetaData.State.Value;
var status = oStatusOptionMetaData.Value.Value;
}