Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Execute SendEmailFromTemplate Web API Action from JS

(0) ShareShare
ReportReport
Posted on by 5

Hi,

I´m solving the problem with sending e-mails using JS and Web API - SendEmailFromTemplate. I tried to generate code with CRMRESTBuilder and edited it based on:

Unfortunately, the following code returns HTTP 400 and the "Invalid Argument" error message:

var parameters = {
TemplateId: {
guid: 'A49CD460-17A2-E911-A9A0-000D3AB5CCB7'
},
Regarding: {
contactid: "9602CEB2-55F7-E711-A954-000D3A34A0AA",
"@odata.type": "Microsoft.Dynamics.CRM.contact"
},
Target: {
"@odata.type": "Microsoft.Dynamics.CRM.email",
email_activity_parties: [{
"partyid_contact@odata.bind": "/contacts(9602CEB2-55F7-E711-A954-000D3A34A0AA)",
participationtypemask: 2 //To
}]
}
};

var sendEmailFromTemplateRequest = {
TemplateId: parameters.TemplateId,
Regarding: parameters.Regarding,
Target: parameters.Target,

getMetadata: function() {
return {
boundParameter: null,
parameterTypes: {
"TemplateId": {
"typeName": "Edm.Guid",
"structuralProperty": 1
},
"Regarding": {
"typeName": "mscrm.crmbaseentity",
"structuralProperty": 5
},
"Target": {
"typeName": "mscrm.crmbaseentity",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "SendEmailFromTemplate"
};
}
};

Xrm.WebApi.online.execute(sendEmailFromTemplateRequest).then(
function success(result) {
if (result.ok) {
var results = JSON.parse(result.responseText);
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);

Can you please advise me how to call Web API action correctly?

Our enviroment is in Version 1710 (9.1.0.6227) online and we have new UUI turned on.

Thanks,

Albert

  • Suggested answer
    Dawidvh Profile Picture
    Dawidvh on at
    RE: Execute SendEmailFromTemplate Web API Action from JS

    Hello,

    Please see below example of what the JSON should look like for the Api and the response you should see when called.

    {
        "TemplateId": "DBC5E9AF-AA33-EA11-A813-000D3A24C29D",
        "Regarding": {
            "contactid": "b8fe2ee9-9f33-ea11-a813-000d3a24c29d",
            "@odata.type": "Microsoft.Dynamics.CRM.contact"
            
        },
    
        "Target": {
            "subject": "",
            "description": "",
            "regardingobjectid_contact@odata.bind": "/contacts(b8fe2ee9-9f33-ea11-a813-000d3a24c29d)",
            "email_activity_parties": [{
                "partyid_systemuser@odata.bind": "/systemusers(fba8443b-6365-4ade-ab81-abe6c47cba3b)",
                "participationtypemask": 1
            }, {
                "partyid_contact@odata.bind": "/contacts(b8fe2ee9-9f33-ea11-a813-000d3a24c29d)",
                "participationtypemask": 2
            }],
            "@odata.type": "Microsoft.Dynamics.CRM.email"
        }
    }

    Postman-json.jpg

    Dawid van Heerden

    Follow on Twitter: @davestechtips

    Subscribe to YouTube: https://www.youtube.com/davestechtips?sub_confirmation=1

    **If you found this reply helpful, please like it and mark this as the answer to close the topic and make it easier to find for other people with similar questions.

  • Aditya Profile Picture
    Aditya 260 on at
    RE: Execute SendEmailFromTemplate Web API Action from JS

    You can refer the sample code at

    community.dynamics.com/.../execute-a-workflow-using-xrm-webapi-online-execute-method-via-javascript-in-dynamics-365-crm-v9

  • LeoAlt Profile Picture
    LeoAlt 16,331 on at
    RE: Execute SendEmailFromTemplate Web API Action from JS

    Hi partner,

    Since you are using xrm.webapi.online, you could refer to this DOC for more details about parameters or formats.

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/online/execute

    In this DOC, there is a sample about how to send request with parameters by xrm.webapi.online.

    BTW, you could also go to F12 developer tools -> Network tab and catah your request to find more details.

    Hope it helps.

    Best Regards,

    Leo

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,387 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans