简体   繁体   中英

Create an email activity using REST Endpoints in CRM2011-2013

The code below will create a Email Activity in CRM but I can't figure out how to add multiple recipients. If I try to add a second recipient it just replaces the first recipient.

function CreateEmail() {
    alert("CreateEmail Begin");

    var email = new Object();
    email.Subject = "Sample Email Using REST";
    SDK.JScriptRESTDataOperations.Create(email, "Email", EmailCallBack, function (error) { alert(error.message); });
    }

    // Email Call Back function
    function EmailCallBack(result)
    {
    var activityParty=new Object();
    // Set the "party" of the ActivityParty // EntityReference of an entity this activityparty relatated to. 
    activityParty.PartyId = {
      Id: "8384E684-7686-E011-8AF0-00155D32042E",//replace this with the contactid from your system.
      LogicalName: "contact"
    };
    // Set the "activity" of the ActivityParty
    // EntityReference.
    activityParty.ActivityId = {
      Id: result.ActivityId, 
      LogicalName: "email"
    };
    // Set the participation type (what role the party has on the activity).
    activityParty.ParticipationTypeMask = { Value: 2 }; // 2 mean ToRecipients
    SDK.JScriptRESTDataOperations.Create(activityParty, "ActivityParty",ActivityPartyCallBack , function (error) { alert(error.message); });
    }

    function ActivityPartyCallBack(reuslt)
    {
    alert("Process Completed");
    }

Here's a snippet that creates a email with multiple Recipients. The key was to set the email_activity_parties attribute so that we can pass an object.

Essentially email_activity_parties lets us submit a Array of Object instead a top level Object.

function CreateEmail() {
    debugger;

    var email = new Object();

    email.Subject = "my email";
    email.Description = "my email description";

    var activityParties = new Array();

    var partyObj0 = new Object();
    partyObj0.PartyId = { Id: "a9568879-e61c-e411-80bb-000c29c1100f", LogicalName: "systemuser" };
    partyObj0.ParticipationTypeMask = { Value: 1 };
    activityParties[0] = partyObj0;

    var partyObj1 = new Object();
    partyObj1.PartyId = { Id: "b23f7a24-2223-e411-80c8-000c29c1100f", LogicalName: "contact" };
    partyObj1.ParticipationTypeMask = { Value: 2 };
    activityParties[1] = partyObj1;

    var partyObj2 = new Object();
    partyObj2.PartyId = { Id: "ffd09f25-1748-e411-80cb-000c29c1100f", LogicalName: "contact" };
    partyObj2.ParticipationTypeMask = { Value: 2 };
    activityParties[2] = partyObj2;

    //set email.email_activity_parties to activityParties

    email.email_activity_parties = activityParties;
    SDK.REST.createRecord(email, "Email", EmailCallBack, function (error) { alert(error.message); });
}

// Email Call Back function
function EmailCallBack(result) {
    debugger;

}

Dont have a REST sample I'm afraid, but in C# SOAP you have to pass a collection of entities, perhaps its the same in REST?

Entity e = new Entity("phonecall");
e["to"] = new Entity[] 
{ 
    ToActivityParty(new EntityReference("contact", contact1)),
    ToActivityParty(new EntityReference("contact", contact2)),
};

static Entity ToActivityParty(EntityReference entityReference)
{
    Entity party = new Entity("activityparty");
    party["partyid"] = entityReference;
    return party;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM