There can be scenarios where we have to add activities programmatically to the CRM organization either through a plugin or through a workflow. Below is how we can achieve this.
Entity followupActivity = new Entity("phonecall");
followupActivity["subject"] = "Followup Phone call activity";
Entity activityPartyFrom = new Entity("activityparty");
activityPartyFrom.Attributes.Add("partyid", new EntityReference("systemuser", workflowContext.UserId));followupActivity.Attributes.Add("from", new Entity[] { activityPartyFrom });
Entity activityPartyTo = new Entity("activityparty");
activityPartyTo.Attributes.Add("partyid", new EntityReference("lead", "Lead's Guid"));
followupActivity.Attributes.Add("to", new Entity[] { activityPartyTo });
followupActivity["scheduledend"] = DateTime.Now.AddDays(30);
service.Create(followupActivity);
Here I am trying to add a phone call activity and assuming that this is from the workflow, workflowcontext used is the workflow context to get the owner of the activity. I am here trying to add a phone call and the entity should change according to our need. Due date is set to 30 days from today and the from is the user under which the workflow is running and to is a lead whose Guid we need to pass so as to set the To field.
Hope this helps !!!