function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sanne Kjær Sørensen 4Sanne Kjær Sørensen 4 

No email is send when assigned Rule is triggered from API (external LEAD system)

No email is send using Assignment Rules when Lead is created from external system. The LEads is assigned to the correct Queue. When I do manual LEad creation the Mail is send. Why is that

We add the Apex code: dmlOpts.EmailHeader.triggerUserEmail = true
The email is an HTML Template and the assignement Rules is working: TEST: when I create an email manully within SAlesforce (create new LEad) the email IS send.

yes, the Assignement Rule got a template (See attachment1) and the LEad is assignemed to the correct Queue
(see attachmenet 2). Attechment1 also shows the Criteria

 
Carlos Campillo GallegoCarlos Campillo Gallego
Hi Sanne :)

When the Lead comes from the external system, does it trigger the Assignment Rule aswell? I mean, is the Assignment rule getting triggered in both scenarios or just when you create it within Salesforce?
If the answer to this is "just when it's created within Salesforce", then it's quite possible that you need to "force" the Assignment rule execution.
In order to do that, add this dmlOpt:
try{
                AR = [SELECT Id 
                      FROM AssignmentRule 
                      WHERE SobjectType = 'Lead' AND 
                            Name = : /*your Assignment Rule*/ AND 
                            Active = true 
                      LIMIT 1]; 
            }catch(exception e){
                system.debug('######## No assignment rule found. Error line ' + e.getLineNumber() + ' with exception ' + e.getCause() + ', message: ' + e.getMessage());
            }
           


Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.assignmentRuleId = AR[0].Id;

Remember that you have to set this options before inserting the Lead, in this example:  newLead.setOptions(dmlOpts);
newLead would be the list of Leads you want to insert.

Hope this helps you :)

Kind regards,

Carlos.
Sanne Kjær Sørensen 4Sanne Kjær Sørensen 4
Dear Carlos. Thank you so much for your reply.  The Apex code is set ip so The Assignmemt  Rules is triggered when a new Lead is insereted. So the Lead Will be addigned to virret Queue. But. ni e-mail is send. Then we added: dmlOpts.EmailHeader.triggerUserEmail = true;  
but this did NOT help. Thanks for you help, we Will look info out Apex script once more. Have a Nice Day. Br. Sanne
MarkRamsellMarkRamsell
Hi Sanne, did you work out what was wrong with the code? We've run into the same issue but with Case creation. We set all of the DMLoptions, including 'dmlOpts.EmailHeader.triggerUserEmail = true', the case is re-assigned to our Queue, but an email is not sent.