Tuesday 15 October 2013

DMLOption Usage : How to control Email Notification On Case Insertion

Hi Folks,

Today I would like to present an implementation so you get some control over UI feature from Apex. Generally when we create case, an option is given in order to send email notification of recorded case but while inserting case from apex there is not such property is available from Apex on case object to control email notification.

There is a new way for getting this problem resolved. This is a salesforce option which is called DMLOption.


/* Creating a instance of DMLOPTION entity */
Database.DMLOptions DmlOpt = new Database.DMLOptions();
DmlOpt.EmailHeader.triggerAutoResponseEmail = true;

/* creating instance of Contact */
Contact testContact = new Contact();
testContact.email = 'sksworld10@gmail.com';
testContact.lastName = 'testName' ;
insert testContact  ;

/* creating instance of Case */
Case testCase = new Case();
testCase.status = 'New';
testCase.origin = 'Phone';
testCase.ContactId = testContact.id ; 

/* Inserting Case with Data base method so we can apply entity we created above */
database.insert(testCase, DmlOpt);
So since next time you can enjoy controlling email notifications with Apex.