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
Jeffrey UyJeffrey Uy 

How to get LastName from Contact

Hello,

I am not a developer but would like to learn a few basic things.
One of which I am working on is to auto-create a lead from a case based on certain criteria.  The code below works but I need to get the lastname from Contact instead of an ID.  Can anyone show me how?  Thanks.

trigger AutoCreateLead on Case (before insert) 
{
    case c=trigger.new[0];
    if(c.Approved_for_Lead__c  == TRUE)
  {
    list<lead> 
    l = [select id, Email from Lead where Email = :c.ContactEmail];
     if(l.size()==0)
    {
      lead l1=new lead(lastname=c.ContactId , Email=c.ContactEmail, Company=c.SuppliedName, LeadSource='Converted From Case', Status = 'Open - Not Contacted', Title='Converted From Case', Phone=c.ContactPhone);
      insert l1;
    }
  }
}
Best Answer chosen by Jeffrey Uy
Raj VakatiRaj Vakati
Use this code and its bulkfied also 
 
trigger AutoCreateLead on Case (before insert) 
{
	Set<String> conIds = new Set<String>() ;	
	Set<String> emails = new Set<String>() ;	

	for (Case c : Trigger.new){
		if(c.Approved_for_Lead__c  == TRUE)
		  {
			conIds.add(c.ContactId);
			emails.add(c.ContactEmail) ; 
		 }	  
	}

	Map<Id, Contact> conMap = new Map<Id,Contact>([select Id ,LastName from Contact where Id IN : conIds]) ; 	
	Set<String> lemails =new Set<String>() ; 
	
	List<Lead> leads = [Select Id,Email From Lead where Email IN : emails];
	for(Lead l :leads){
		lemails.add(l.email); 
	}
	List<lead> leasUp = new List<lead>() ;
	for (Case c : Trigger.new){
	if(c.Approved_for_Lead__c  == TRUE)
	  {
		 if(!lemails.contains(c.ContactEmail)){
			 lead l1=new lead(lastname=conMap.get(c.ContactId).LastName , Email=c.ContactEmail, 
			Company=c.SuppliedName, LeadSource='Converted From Case', Status = 'Open - Not Contacted', Title='Converted From Case', Phone=c.ContactPhone);
			leasUp.add(l1) ; 
		 }
		  
	  }
	}
		  
   insert  leasUp ;
}

 

All Answers

Raj VakatiRaj Vakati
Use this code and its bulkfied also 
 
trigger AutoCreateLead on Case (before insert) 
{
	Set<String> conIds = new Set<String>() ;	
	Set<String> emails = new Set<String>() ;	

	for (Case c : Trigger.new){
		if(c.Approved_for_Lead__c  == TRUE)
		  {
			conIds.add(c.ContactId);
			emails.add(c.ContactEmail) ; 
		 }	  
	}

	Map<Id, Contact> conMap = new Map<Id,Contact>([select Id ,LastName from Contact where Id IN : conIds]) ; 	
	Set<String> lemails =new Set<String>() ; 
	
	List<Lead> leads = [Select Id,Email From Lead where Email IN : emails];
	for(Lead l :leads){
		lemails.add(l.email); 
	}
	List<lead> leasUp = new List<lead>() ;
	for (Case c : Trigger.new){
	if(c.Approved_for_Lead__c  == TRUE)
	  {
		 if(!lemails.contains(c.ContactEmail)){
			 lead l1=new lead(lastname=conMap.get(c.ContactId).LastName , Email=c.ContactEmail, 
			Company=c.SuppliedName, LeadSource='Converted From Case', Status = 'Open - Not Contacted', Title='Converted From Case', Phone=c.ContactPhone);
			leasUp.add(l1) ; 
		 }
		  
	  }
	}
		  
   insert  leasUp ;
}

 
This was selected as the best answer
Jeffrey UyJeffrey Uy
Thanks Raj.  At first it didn't work, then I changed it to after insert, after update and it's working now.  thank you so much for your help.