+ Start a Discussion
Matt SchollMatt Scholl 

web-to-lead, assignment rule, auto-convert lead, workflow email alert

All,

I am trying to use web-to-lead to insert a lead, assign it to the correct owner based on a field value, automatically convert the lead to an account and contact, then notify the contact owner of the new contact via workflow email alert.

I nearly have it working except the email alert goes to the original default lead owner, not the assigned ower, even though after processing the contact owner is correct. I suspect the problem is due to the order of execution and the fact that I had to use an asynch trigger to do the lead conversion.

I'd appreciate any help, thoughts, or alternatives.

Pseudo-code and code:

1. Web-to-lead - load field values; auto-convert set to FALSE
2. After trigger - auto-convert=FALSE, so lead not converted (delays conversion until 2nd time after trigger fires)
3. Assignment rule
4. Lead workflow rule - when created, set auto-convert = TRUE
5. After trigger - auto-convert=TRUE, so lead converted to account and contact
6. Contact workflow email alert - notify owner of new contact (***Here is where the problem is. Default lead owner is notified, not contact owner.***)

trigger LeadAutoConvert on Lead (after insert) {
    asyncApex.asyncLeadAutoConvert(Trigger.newMap.keySet());
}

global class asyncApex {
  @future
  public static void asyncLeadAutoConvert(Set<Id> leadIds) {
    List<Lead> leadlist = [select id, IsConverted, Auto_Convert__c, Chapter__c, ConvertedContactID from Lead where id IN:leadIds];
    for(Lead myLead: leadlist){
        if((myLead.IsConverted==false)&&(myLead.Auto_Convert__c)){
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(myLead.Id);
            lc.convertedStatus = 'Qualified';
            lc.setDoNotCreateOpportunity(true);
            //Convert Lead
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
            system.debug('');
        }
    }
  }
}
ShashForceShashForce
Hi Matt,

I would suggest you to use a Time-based workflow email alert instead and send the email after may be 30 minutes (0.5 hours) or 1 hour, so that the owner value is changed by then.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank