Sunday, October 12, 2008

CRM 4.0 Creating Activities

When importing activities or creating them for a process, there are three important Steps.

  1. Create the Activity relating it to all appropriate entities.
  2. (Optional) Attach a file using an Annotation or ActivityMimeAttachment
  3. Set the State of the Activity

MS CRM has numerous Activities. They are all part of an ActivityPointer Entity, but we won't work with that directly.  We will directly create task, email, letter, appointment, and phonecall entities. I'll cover Campaign and Service Activities another day.

Activities need to be related with a number of entities before they are useful.

Activities need

  1. An owner, a systemuser who is responsible for carrying an activity out.
  2. A regardingobject, the entity the activity is related to, like a Contact, Opportunity or Account.
  3. Numerous activityparty arrays for holding the different types of related entities that this activity affects. For example an email could cc: a systemuser and a contact, and there is a cc activityparty array for an email.
  4. Any file attachments in the form of an annotation or activitymimeattachment
  5. Start and End dates Scheduled and Actual.
  6. A state/status combination
    1. Active will show up in the owners WorkPlace Activities and under the regardingobject entity and possible rollup to an entity above that.
    2. Inactive - will show up in history under the regardingobject and possible rollup to an entity above that.

Setting up each activity is very similar. Below is some example code that creates a phonecall, and completes that activity.

Notice the following in the code sample:

  • The activityparty arrays that allow any number of systemuser, contact, account and lead entities to be related to an activity.
  • The SetStatePhoneCallRequest to set the state of the phonecall to completed. The PhoneCallStatus of 2 for outgoing. You can refer to the following link for this and find other state enumerations required to set the state and status of all entities. Link to MSDN PhoneCallState Enumeration
  • I use the CRM SDK CrmTypes and other Enumeration helpers. They come in handy so why not?
var newphoneCall = new phonecall
  {
      ownerid = CrmTypes.CreateOwner(EntityName.systemuser.ToString(), activityOwnerId),
      from = new[]
          {
              new activityparty
                  {
                      participationtypemask = CrmTypes.CreatePicklist(ActivityPartyType.Sender),
                      partyid = CrmTypes.CreateLookup(EntityName.systemuser.ToString(), activityOwnerId)
                  }
          },
      subject = subject,
      description = description,
      regardingobjectid = (matchedContact) ? CrmTypes.CreateLookup(EntityName.contact.ToString(), relatedContactId) : null,
      to =  (!matchedContact)? null : new []
         {
            new activityparty
              {
                  participationtypemask = CrmTypes.CreatePicklist(ActivityPartyType.ToRecipient),
                  partyid = CrmTypes.CreateLookup(EntityName.contact.ToString(), relatedContactId)
              }
         },
      scheduledstart = CrmTypes.CreateCrmDateTime(Convert.ToDateTime(scheduledStart).ToShortDateString()),
      actualstart = CrmTypes.CreateCrmDateTime(Convert.ToDateTime(scheduledStart).ToShortDateString()),
      scheduledend = CrmTypes.CreateCrmDateTime(Convert.ToDateTime(scheduledEnd).ToShortDateString()),
      actualend = (isComplete) ? CrmTypes.CreateCrmDateTime(Convert.ToDateTime(scheduledEnd).ToShortDateString()) : null,
  };

try
  {
      Guid phonecallId = service.Create(newphoneCall);

      if (isComplete)
      {
          var request = new SetStatePhoneCallRequest
              {
                  EntityId = phonecallId,
                   PhoneCallState = PhoneCallState.Completed,
                   PhoneCallStatus = 2
              };

          try
          {
              service.Execute(request);
          }

          catch (SoapException ex)
          {
              string errormsg = String.Format("Update Error Setting PhoneCall State to Complete error {0}", ex.Detail.InnerText);
              WriteErrorFile(errormsg);
          }
      }

  }
catch (SoapException ex)
   {
       string errormsg = String.Format("Write Error for PhoneCall {0} error {1}", row[(int) tblCal.ACTVCODE], ex.Detail.InnerText);
       WriteErrorFile(errormsg);
   }

Next: My next post will cover importing files into CRM in the form of annotations and activitymimeattachments as well as mimeTypes, and CRM file size limitations and their configuration.

No comments: