In this post we will learn how to create an Email Activity and associated Activity Parties using JavaScript and REST.The sample code used below is to create an email activity for quote. You can change it as per your requirements.
We are providing Subject, Description, Regarding (Regarding to that Quote), From (Passing it Owner) and To (Setting customer of the quote as a recipient of the email activity) and to set activity parties (Sender, Recipient), we are using relationship (email_activity_parties).
function CreateEmail() { var email = new Object(); var quoteId = Xrm.Page.data.entity.getId(); var OwnerLookup = Xrm.Page.getAttribute("ownerid").getValue(); OwnerGuid = OwnerLookup[0].id; OwnerGuid = OwnerGuid.replace(/[{}]/g, ""); var CustomerLookUp = Xrm.Page.getAttribute("customerid").getValue(); if (CustomerLookUp != null) { var AccountId = CustomerLookUp[0].id; var AccountTypeName = CustomerLookUp[0].typename; var AccountName = CustomerLookUp[0].name; } email.RegardingObjectId = { // Setting Regarding Object id of Email Activity. Id: quoteId, LogicalName: "quote" }; email.Subject = "New Email"; email.Description = "Email Description"; var activityParties = new Array(); var partyObj0 = new Object(); partyObj0.PartyId = { Id: OwnerGuid, LogicalName: "systemuser" }; partyObj0.ParticipationTypeMask = { Value: 1 }; //Setting "From" of Email activityParties[0] = partyObj0; var partyObj1 = new Object(); partyObj1.PartyId = { Id: AccountId, LogicalName: "account" }; partyObj1.ParticipationTypeMask = { Value: 2 }; //Setting "To" of Email activityParties[1] = partyObj1; email.email_activity_parties = activityParties; //set email.email_activity_parties to activityParties SDK.REST.createRecord(email, "Email", EmailCallBack, function (error) { alert(error.message); }); } // Email Call Back function function EmailCallBack(result) { alert("Email Created"); }
You can also create multiple activity parties (to, from) by simply creating their objects and setting their Participation Mask Value.