• Matt Scholl
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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('');
        }
    }
  }
}