-1

I want to create new opportunity record in CRM. Opportunity record will get created based on this condition. It will compare account in CRM with organization in pipedrive. If matching Name is found then it will directly create opportunity else it will first create account and then opportunity.

How Can I add value to Account field which is Lookup field in Opportunity?

Below is the Code which I have written till now.

For getting Account records from CRM:

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

For checking account is already there or not:

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);
}

To Create Opportunity record:

public void CreateOpportunity(Message message)
{
    Entity opportunity = new Entity("opportunity");
    opportunity["name"] = message.Current.Title;
    opportunity["estimatedvalue"] = message.Current.value;
    this.crmService.Create(opportunity);
}
vidhi
  • 69
  • 2
  • 9
  • have you run the debugger for the code you posted..? can you tell us where `name` is declared and what is it's datatype looking at your for loop it would appear that `name` in this line would be a single `char` ` name[i] = account.Entities[i].Attributes["name"].ToString();` please show us what name is.. or where it's declared. – MethodMan Dec 15 '17 at 06:26
  • @MethodMan I made changes you can check now. – vidhi Dec 15 '17 at 06:31

1 Answers1

0

You have to use like below in CreateOpportunity method:

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

"CustomerId" is logical name of customer attribute, "account" is logical name of Account entity, accountId should be passed from created account record PK Guid or matched account PK from entity collection you have

Guid accountId = account.Entities[i].Id;
jasonscript
  • 6,039
  • 3
  • 28
  • 43