[SalesForce] How to trigger email when creating a case via REST API

When it comes to the SOAP Api, it exposes among its headers EmailHeader which can be used to trigger email sent when a case is created via API.

By default, when a case is created via API, the case emails are not sent. However, while the SOAP Api exposes among its headers EmailHeader which can be used to trigger email sent when a case is created via API, there is nothing like that with the REST API.

How to trigger email when creating a case via REST API?

Best Answer

The same way you cause emails to be sent when you create a Case through any other Apex, by setting DMLOptions.

There is documentation on setting DMLOptions, but the highlights of that page are...

Database.DMLOptions dml = new Database.DMLOptions();
// For sending emails to the person designated as the Contact for the case
dml.EmailHeader.triggerAutoResponseEmail = true;
// For sending emails to salesforce users of your org
dml.EmailHeader.triggerUserEmail = true;
// For sending emails to addresses outside of your org
dml.EmailHeader.triggerOtherEmail = true;

If you also have auto-assignment rules for cases that are created, you may need to set another DML option AssignmentRuleHeader.useDefaultRule.

If you're only working with a single case, you can use the setOptions() method of the SObject class to actually set the dml options.

Case c = new Case();
Database.DMLOptions dml = new Database.DMLOptions();
c.setOptions(dml);
insert case;

For lists of SObjects, you'd need to use the database.<dml method>() method instead of just [insert|update] <list of SObject>;. That method only exists for insert and update (not delete, undelete, or upsert)

Database.DMLOptions dml = new Database.DMLOptions();
Database.insert(myCaseList, dml);
Related Topic