+ Start a Discussion
EBeachEBeach 

Edit all records in related list on Lead Convert

Our problem: we need all records of a custom object related to the lead to map to the contact/account/opportunity on lead convert.

 

Attempted solution: an app from hiSoft (http://sites.force.com/appexchange/listingDetail?listingId=a0N30000003GuisEAC)

This app creates a new custom object with lookup fields for Lead, Contact, Account and Opportunity.

It also includes the Trigger below.  This trigger only seems to update a single record of all records of the custom object related to the lead.  I don't understand why as the map should pull all custom object records for the lead Id.  Any recommendations of why this trigger isn't functioning properly?

 

 

trigger UpdateCustomeObject_Trigger on Lead (before update) {

	for (Integer i = 0; i < Trigger.new.size(); i++){
	    if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
		    Set<Id> leadIds = new Set<Id>();
		    for (Lead lead : Trigger.new) {
		        leadIds.add(lead.Id);
		    }
		    
		    Map<Id, CustomObject__c> entries = new Map<Id, CustomObject__c>([select Contact__c, Opportunity__c, Account__c, Lead__c from CustomObject__c where lead__c in :leadIds]);        
		    if(!Trigger.new.isEmpty()) {
			    for (Lead lead : Trigger.new)  {
			    	for (CustomObject__c CustomObject : entries.values()) {
			    		if (CustomObject.Lead__c == lead.Id) {
					        CustomObject.contact__c = lead.ConvertedContactId;
					        CustomObject.opportunity__c = lead.ConvertedOpportunityId;
					        CustomObject.account__c = lead.ConvertedAccountId;
					        update CustomObject;
					        break;
			    		}
			    	}
			    }
		    }
	    }
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
EBeachEBeach

Wow - i didn't see the "break" command in the loop.

Opps!

All Answers

EBeachEBeach

Wow - i didn't see the "break" command in the loop.

Opps!

This was selected as the best answer
BrandiTBrandiT

How were you able to correct this problem?  I am having the same issue.  I tried just removing that break line you mentioned, but it is still only converting one record from my related list.

 

Also, I have another custom lookup field called Heading__c that is populated by the sales on my custom object, but after I convert the lead to an account, the information in the Heading__c field disappears.  I thought the trigger was supposed to leave all information as is, but just update the account, opportunity, and contact record.

 

Any ideas?

 

Thanks a lot!

BrandiTBrandiT

Ok I was able to fix the problem with my heading__c field,

but I still can't get all the related records to move over to the new account/opportunity.

 

Any help you can give would be greatly appreciated!!

 

Thanks!

BrandiTBrandiT

I found a wonderful resource that was able to help me with this.  Here is the code I'm using:

 

trigger UpdateOHObject_Trigger on Lead (after update) {

  Map<Id, Lead> leadMap = new Map<Id,Lead>();
  Lead parent;
 
  for (Integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false) {
      leadMap.put( Trigger.new[i].Id, Trigger.new[i]);
    }
  }
   
  if( leadMap.size() > 0 ) {
      Set<Id> leadIds = leadMap.keySet();
      List<Opportunity_Headings__c> allChildren =
        [select Id, Opportunity__c, Account__c, Lead__c from Opportunity_Headings__c where lead__c in :leadIds];      
 
 System.debug(allChildren);
   
      for ( Opportunity_Headings__c child : allChildren ) {
        if ( leadMap.containsKey( child.Lead__c ) ) {
           // lookup the parent lead
           parent = leadMap.get( child.Lead__c );
           // update the fields on the child object
           child.opportunity__c = parent.ConvertedOpportunityId;
           child.account__c = parent.ConvertedAccountId;
        }
      }

System.debug(allChildren);

    //try {
      update allChildren;
   // } catch( Exception e ) {
         // could put something here to notify on error
         // otherwise it fails silently
   // }
     
  }
}

 

 

Thanks again Robert!!