Search code examples
c#dynamics-crmmicrosoft-dynamicsdynamics-crm-2015

assigning a lead to customer field in Phonecall activity


My code generates leads in Microsoft dynamics CRM system. There are phone call activities associated with leads. There are "To" and "From" properties in the phone call activities which can be either a contact or lead. I need my code to assign the lead to those properties. I implemented this:

Entity account = new Entity("lead");
lead_id= service.Create(account);
Entity activity1 = new Entity("phonecall");
activity1["description"] = "Phone call activity";
activity1["to"]=account;
activity1.Attributes.Add("regardingobjectid", new EntityReference("lead", lead_id));
service.Create(activity1);

It doesn't show any error but doesn't work. I can see that the "to" field in the activity is empty in CRM system.

public static Guid createActivity(Guid lead_id, Entity sendr, Entity recvr)
{
Entity activity1 = new Entity("phonecall");
activity1["description"] = ImgURL;
var activityParty1 = new Entity("activityparty");
activityParty1["partyid"] = sendr.ToEntityReference();
activity1["from"] = new[] { activityParty1 };

var activityParty = new Entity("activityparty");
activityParty["partyid"] = recvr.ToEntityReference();
activity1["to"] = new[] { activityParty };
activity1.Attributes.Add("regardingobjectid", new EntityReference("lead", lead_id));
Guid acc_id = service.Create(activity1);
return acc_id;
}

Exception at: Guid acc_id = service.Create(activity1);


Solution

  • Activity Party is the connection between PhoneCall and related "to" field. Create an add an activity party.

    Early Bound:

      activity1.To = new[]
      {
         new ActivityParty() {PartyId = account.ToEntityReference()}
      };
    

    Late Bound:

     var activityParty = new Entity("activityparty");
     activityParty["partyid"] = account.ToEntityReference();
     activity1["to"] = new[] {activityParty};