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