1

I'm having an issue with a trigger, and can't seem to figure it out, so I'm hoping someone can help me.

Basically, I wrote a trigger that runs when a lead is converted. It is supposed to populate a field called Contact__c on the new opportunity, with the ID of the Primary Contact of that Opp.

For some reason, it doesn't seem to be putting anything in that field. I added a bunch of debug statements, and it looks like it's having trouble pulling a value from the map

Here's the trigger:

trigger AddPrimaryContactToConvertedOpp on Lead (After Update) {


// THIS TRIGGER WILL OVERWRITE ANY CONTACT DEFINED IN THE CUSTOM FIELD CONTACT__C ON THE OPPORTUNITY OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVIOD DATA BEING OVERWRITTEN BY MISTAKE...


//STEP 1: Create a list of all Opp IDs we are working on, ie: those created in this lead conversion (or batch conversion)
   map <Opportunity,Id> OppsAndContacts = new map <Opportunity,Id>();
   for (Lead l : Trigger.new) {
       If (l.IsConverted){
           Opportunity Opp = [SELECT Id from Opportunity where Id = :l.convertedopportunityid];
           OppsAndContacts.put(Opp, l.ConvertedContactID);
           System.Debug('>>> adding to map contact: ' + l.convertedcontactid + ' opp: '+ Opp);
       }
   }
    System.Debug('>>> Printing the map: '+ OppsAndContacts);

//STEP 2: Loop through all records in OppsAndContacts, adding the contactID to the Opps
    if (!OppsAndContacts.isEmpty()){ //only run if there are actually some newly created Opps
       System.Debug('>>> the list of OppsAndContacts is not empty');
       for (Opportunity o : OppsAndContacts.keySet()) {
         o.Contact__c = OppsAndContacts.get(o);
         System.Debug('>>> adding to opp record: ' + o.Id + ' the contact with ID: ' + OppsAndContacts.get(o));
       }

   }//END IF
}//END TRIGGER

It seems to be having trouble with OppsAndContacts.get(o)

If anyone can help me out I'd greatly appreciate it!

  • 1) you've got a select statement inside of a for loop on Trigger.new -- not a good idea, you'll break governor limits on batch operations; 2) normal practice would be for your map to be <ID,ID> that is, oppoId to ContactId; 3) your trigger doesn't actually do any dml to update the Oppos – cropredy Dec 10 '14 at 21:30
  • Thanks for answering! I can switch the map to be Id,Id... . The line that I thought would update the Opp record is "o.Contact__c = OppsAndContacts.get(o);". What do you think I should change it to? – user3515 Dec 10 '14 at 21:39
4
trigger AddPrimaryContactToConvertedOpp on Lead (After Update) {


// THIS TRIGGER WILL OVERWRITE ANY CONTACT DEFINED IN THE CUSTOM FIELD CONTACT__C ON THE OPPORTUNITY OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVIOD DATA BEING OVERWRITTEN BY MISTAKE...

// [1] Build list of Oppos to update
List<Opportunity> oUpdList = new List<Opportunity> ();
for (Lead l : Trigger.new) 
    if (l.IsConverted && l.convertedOpportunityId != null)
        oUpdList.add(new Opportunity(id = l.convertedOpportunityId, contact__c = l.convertedContactId));

// [2] Update the converted Oppos
update oUpdList;  // could be Database.update(oUpdList,false) if you want partial successes
}
  • Okay so I'm trying to understand your code. Looks like it's making a list of Opportunities to update, and then updating them? Cool! Well, for some reason it's giving me an error on line 17 - expecting a right parentheses, found ';'. Any idea why? Thanks! – user3515 Dec 10 '14 at 21:57
  • Just added a right parenthesis to the end of the line, and...... IT WORKED!!!! Thanks so much for your help! – user3515 Dec 10 '14 at 22:01
  • sorry about the missing paren; fixed. In order for an after trigger to do any DML, it needs to have a list of the sobject to update/insert/delete; only before insert/update triggers implicitly insert/update the trigger.new sobject – cropredy Dec 10 '14 at 22:12
  • I get it! That explains why the code I copied from a before insert trigger on Opp wasn't working here. – user3515 Dec 11 '14 at 16:06
  • i woke up at 0300h and realized the trigger could be simpler -- updated answer – cropredy Dec 11 '14 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.