Saturday, May 16, 2009

Assignment Rules Using APEX

Did you know you can use the assignment rules functionality that is provided to you with the standard Salesforce.com interface with Apex code?

This code creates a Case, sets the owner using the standard assignment rules, and sends the email to the new Case owner. It could also be used for any other object that support assignment rules.

Apex Code:

// Instantiate a new Case
Case newCase = new Case();

// Put here the code to initialize the fields with the information...

// Instantiate the database options
Database.DMLOptions dlo = new Database.DMLOptions();

// This line assigns the ID for the assignment rule you want to use
// dlo.assignmentRuleHeader.assignmentRuleId = getAssignmentRuleId();

// This line assigns the active assignment rule
dlo.assignmentRuleHeader. useDefaultRule = true;

// This line sends email to the related contact.
dlo.EmailHeader.triggerAutoResponseEmail = true;

// This line sends email to the new case owner
dlo.EmailHeader.triggerUserEmail = true;

// Set the options just defined
newCase.setOptions(dlo);

// Create case.
insert newCase;

No comments:

Post a Comment