Featured Post

Web API Requests Series

Web API Series:  Create and Retrieve , Update and Delete , Retrieve Multiple , Associate/Disassociate , Impersonation , Run Workflows

04 July 2012

How to associate a record as Activity Party to an activity.

In this post I will show you how to associate a record to an activity (in this case an email) as an Activity Party using the OData REST end point with Javascript.
Setting the "To", "From", "Cc", "Bcc" fields on an email message is not so straight forward as other lookup fields such as the "Regarding" field. This is because these fields are of Activity Party type.
There are two important steps to follow:
  1.  Set the "party" of the ActivityParty (what will be related to the activity) as an EntityReference. activityParty.PartyId = { Id: record.RecordId, LogicalName: "logicalName" }; 
  2.  Set the "activity" of the ActivityParty (the e-mail, in this case) as an EntityReference. activityParty.ActivityId = { Id: email.ActivityId, LogicalName: "email" }; 
The third steps is to set the participation type (what role the party has on the activity)

Use: activityParty.ParticipationTypeMask = { Value: 1 };  To set the "From" field
Use: activityParty.ParticipationTypeMask = { Value: 2 };  To set the "To" field
Use: activityParty.ParticipationTypeMask = { Value: 3 };  To set the "Cc" field 
Use: activityParty.ParticipationTypeMask = { Value: 4 };  To set the "Bcc" fiel.

Ultimately perform the REST request.

Here is the complete code:

 createActivityParty: function (object, email, logicalName, recipient) {

        var activityParty = {}, jsonActivityParty;
        switch (logicalName) {
            case "account":
                activityParty.PartyId = {
                    Id: object.AccountId,
                    LogicalName: "account"
                };
                break;

            case "contact":
                activityParty.PartyId = {
                    Id: object.ContactId,
                    LogicalName: "contact"
                };
                break;
            case "systemUser":
                activityParty.PartyId = {
                    Id: object.SystemUserId,
                    LogicalName: "systemuser"
                };
                break;
            default:
        }


        // Set the "activity" of the ActivityParty (the e-mail, in this case) as an EntityReference.
        activityParty.ActivityId = {
            Id: email.ActivityId,
            LogicalName: "email"
        };

        // Set the participation type (what role the party has on the activity). For this
        // example, we'll put the account in the From field (which has a value of 1).
        if (recipient == "sender") {
            activityParty.ParticipationTypeMask = { Value: 1 };
        }
        else if (recipient == "to") {
            activityParty.ParticipationTypeMask = { Value: 2 };
        }
        else if (recipient == "cc") {
            activityParty.ParticipationTypeMask = { Value: 3 };
        }

        //stringify the object to be created
        jsonActivityParty = JSON.stringify(activityParty);

        this._performRequest({
            type: "POST",
            url: this._ODataPath() + "/ActivityPartySet",
            data: jsonActivityParty,
            success: function (request) {

            },
            error: function (request) {
                this._errorHandler(request);

            }
        });
    },

Hope this helps, also don't forget to check the SDK. which is where I learned about this.