检索记录的Guid并在Dynamics CRM中创建新记录

时间:2017-12-15 06:14:01

标签: c# dynamics-crm microsoft-dynamics dynamics-crm-online

我想在CRM中创建新的机会记录。将根据此条件创建机会记录。它将CRM中的帐户与pipedrive中的组织进行比较。如果找到匹配的名称,那么它将直接创建机会,否则它将首先创建帐户然后创建机会。

如何在“机会”中的“查询”字段的“帐户”字段中添加值?

以下是我到目前为止编写的代码。

从CRM获取帐户记录:

public EntityCollection GetAccount()
{
    QueryExpression query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet("name", "accountid") };
    EntityCollection accountname = this.crmService.RetrieveMultiple(query);
    return accountname;
}

对于支票帐户是否已经存在:

int accountCount = account.Entities.Count;
string[] name = new string[accountCount];     
for (int i = 0; i < accountCount; i++)
{
    name[i] = account.Entities[i].Attributes["name"].ToString();
    if (name[i] == message.Current.org_name || name[i].Contains(message.Current.org_name))
    {
        this.counter++;
        this.flag = 1;
        continue;
    }
}
if (this.counter == 1)
{
    c.CreateOpportunity(message);
}
else if (this.counter > 1)
{
    c.CreateAccount(message);
    c.CreateOpportunity(message);
}
if (this.flag == 0)
{
    c.CreateAccount(message);
    c.CreateOpportunity(message);
}

创建机会记录:

public void CreateOpportunity(Message message)
{
    Entity opportunity = new Entity("opportunity");
    opportunity["name"] = message.Current.Title;
    opportunity["estimatedvalue"] = message.Current.value;
    this.crmService.Create(opportunity);
}

1 个答案:

答案 0 :(得分:0)

您必须在CreateOpportunity方法中使用以下内容:

opportunity["CustomerId"] = new EntityReference("account", accountId);

“CustomerId”是客户属性的逻辑名称,“account”是帐户实体的逻辑名称,accountId应该从创建的帐户记录中传递PK Guid或来自您拥有的实体集合的匹配帐户PK

Guid accountId = account.Entities[i].Id;