如何从Microsoft CRM中的Guid查找客户实体逻辑名称

时间:2014-02-28 16:08:54

标签: c# .net dynamics-crm

在CRM中,我正在尝试自动化从链中的先前电子邮件创建新电子邮件的过程。此电子邮件必须发送给案例的客户,该客户可以是accountcontact

我可以检索联系人/帐户的Guid,但我不知道如何检索逻辑名称。

这是我到目前为止所做的:

OrganizationServiceProxy service = CRMCentralCRMServiceInstance;

Guid customerId = GetCustomerIdFromCase(caseId);

Entity email = new Entity("email");

Entity activityPartyTo = new Entity("activityparty");

//"account" is a guess, it could be "contact"
EntityReference customerReferenceTo = new EntityReference("account", customerId); 

activityPartyTo["partyid"] = customerReferenceTo;
EntityCollection toEntityCollection = new EntityCollection();
toEntityCollection.Entities.Add(activityPartyTo);
email["to"] = toEntityCollection;
.
.
.
newEmailId = service.Create(email);



public Guid GetCustomerIdFromCase(Guid caseId) {
    Guid customerId = Guid.Empty;
    List<CRMCase> caseList = GetCRMCasesById(caseId);

    if (caseList.Count > 0)
    {
        CRMCase cmcCase = caseList.First();
        customerId = cmcCase.CustomerId;
    }

    return (customerId);
}

public List<CRMCase> GetCRMCasesById(Guid caseId)
    {
        List<CRMCase> crmCases = new List<CRMCase>();

        try
        {
            OrganizationServiceProxy service = CRMCentralCRMServiceInstance;

            ConditionExpression condition1 = new ConditionExpression();
            ConditionExpression condition2 = new ConditionExpression();

            condition1.AttributeName = "incidentid";
            condition1.Operator = ConditionOperator.Equal;
            condition1.Values.Add(caseId.ToString("N"));

            condition2.AttributeName = "statecode";
            condition2.Operator = ConditionOperator.In;
            condition2.Values.Add("Active");
            condition2.Values.Add("Resolved");

            FilterExpression filter = new FilterExpression();

            filter.FilterOperator = LogicalOperator.And;
            filter.Conditions.Add(condition1);
            filter.Conditions.Add(condition2);

            QueryExpression query = new QueryExpression();

            query.EntityName = "incident";
            query.ColumnSet = new ColumnSet(true);
            query.Criteria = filter;


            RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest();
            retrieveAttributeRequest.EntityLogicalName = "incident";
            retrieveAttributeRequest.LogicalName = "statuscode";
            retrieveAttributeRequest.RetrieveAsIfPublished = true;

            RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
            StatusAttributeMetadata statusCodeAttribute = (StatusAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;


            retrieveAttributeRequest = new RetrieveAttributeRequest();
            retrieveAttributeRequest.EntityLogicalName = "incident";
            retrieveAttributeRequest.LogicalName = "prioritycode";
            retrieveAttributeRequest.RetrieveAsIfPublished = true;

            retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
            PicklistAttributeMetadata priorityCodeAttribute = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;


            retrieveAttributeRequest = new RetrieveAttributeRequest();
            retrieveAttributeRequest.EntityLogicalName = "incident";
            retrieveAttributeRequest.LogicalName = "statecode";
            retrieveAttributeRequest.RetrieveAsIfPublished = true;

            retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
            StateAttributeMetadata stateCodeAttribute = (StateAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;


            EntityCollection casesColl = service.RetrieveMultiple(query);

            foreach (Entity entity in casesColl.Entities)
            {
                Entity incidentCRMCase = entity;
                CRMCase cRMCase = GetCRMCaseFromIncidentCase(incidentCRMCase, statusCodeAttribute.OptionSet.Options, stateCodeAttribute.OptionSet.Options, priorityCodeAttribute.OptionSet.Options);
                crmCases.Add(cRMCase);
            }
        }
        catch (SoapException se)
        {
            string action = MethodBase.GetCurrentMethod().DeclaringType.Name + " :: " + MethodBase.GetCurrentMethod().Name;
            string message = "Unexpected error in action: " + action
                                + Environment.NewLine + se.Message
                                + Environment.NewLine + se.Detail.InnerText;

            throw new Exception(message);
        }

        return (crmCases);
    }

我找到了this暴力法,但如果有的话,我宁愿找到更清洁的方法。

1 个答案:

答案 0 :(得分:3)

确定。代码真的很复杂。尝试使用以下内容:

private EntityReference GetCustomerFromCase(Guid caseId)
{
    Entity Case = CRMCentralCRMServiceInstance.Retrieve("incident", caseId, new ColumnSet("customerid"));
    return Case.GetAttributeValue<EntityReference>("customerid");
}