Reputation: 555
I am creating phone call activities against accounts in CRM, I need to assign the to property of the phone call to the retrieved accounts related contact, how can I do this, please see my code below
public static void GetRelevantOutboundCallCenter(IOrganizationService service)
{
QueryExpression qe = new QueryExpression();
qe.EntityName = "account";
qe.ColumnSet = new ColumnSet("new_accountactionedstatus", "name", "telephone1", "primarycontactid");
qe.Criteria.AddCondition("new_deliverystatus", ConditionOperator.Equal, 279640000);
qe.Criteria.AddCondition("new_province", ConditionOperator.Equal, 100000018);
qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
EntityCollection response = service.RetrieveMultiple(qe);
var counter = 0;
foreach (Entity account in response.Entities)
{
PhoneCall phone = new PhoneCall();
ActivityParty from = new ActivityParty();
phone.OwnerId = from.PartyId = new EntityReference("systemuser", new Guid("6DEFA813-56F9-E411-80CC-00155D0B0C2D"));
ActivityParty to = new ActivityParty();
to.PartyId = account.ToEntityReference();
phone.From = new ActivityParty[] { from };
phone.DirectionCode = true;
phone.To = new ActivityParty[] { to };// I need to set the related Contact of the account here
phone.PhoneNumber = account.Attributes["telephone1"].ToString();
phone.Subject = "TEST FOR OUTBOUND";
service.Create(phone);
counter++;
if (counter == 438)
return;
}
}
This is where I set the lookup account, I need this to be the contact related to the retrieved account -- phone.To = new ActivityParty[] { to };
Upvotes: 1
Views: 1282
Reputation: 5446
In case you want to assign your calls to primary contact of account change line
to.PartyId = account.ToEntityReference();
to line
to.PartyId = account.GetEntityAttribute<EntityReference>("primarycontactid");
Upvotes: 2