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
BellaBella 

Contacts & Opportunities coming from Leads

Hi, is there any way to know if a contact and opportunity come from a lead conversion rather than a regular insertion? I need to write an apex trigger on insertion for both of those objects, but ONLY for those that come from a converted lead. For contacts, I'm thinking I can probably try to look at all inserted contacts then match on email within the lead (email is a required field) but I'm not sure about Opportunities. Is there a good way to differentiate? Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Leads have fields containing the convertedaccountid, convertedcontactid and converted opportunity id.  Thus you could probably query these to see if they match the id of the object(s) in your trigger.  This would probably have to be after insert to ensure that ids have been allocated.

 

I've also used field mappings for this.

 

E.g. I have checkboxes on the lead named AccountFromLead and ContactFromLead which default to true and are not present on the page layout.

 

Then I have a checkbox on Account called FromLead which defaults to false and is not present on the account page layout, similar for contact.

 

I then have a field mappings for lead conversion that set the Acccount's FromLead field to the Leads AccountFromLead field.  The upshot of which is that if the checkbox is true, it came from a lead.

All Answers

bob_buzzardbob_buzzard

Leads have fields containing the convertedaccountid, convertedcontactid and converted opportunity id.  Thus you could probably query these to see if they match the id of the object(s) in your trigger.  This would probably have to be after insert to ensure that ids have been allocated.

 

I've also used field mappings for this.

 

E.g. I have checkboxes on the lead named AccountFromLead and ContactFromLead which default to true and are not present on the page layout.

 

Then I have a checkbox on Account called FromLead which defaults to false and is not present on the account page layout, similar for contact.

 

I then have a field mappings for lead conversion that set the Acccount's FromLead field to the Leads AccountFromLead field.  The upshot of which is that if the checkbox is true, it came from a lead.

This was selected as the best answer
BellaBella

Oohh I didn't even know about the fields. I'll look into those but the checkbox approach sounds good to. If I grab the contact and opportunity that get created from a lead, can I update one of their fields? Specificly I have a trigger that reassigns the account owner on insertion (on all accounts, including those from leads) and then I'd like to make the owner of the contact and opportunity the same as the newly updated owner of the account. Do you think I can just grab the Account.OwnerId from the Contact and Opportunity lookup fields to the Account, or will there be some problems like... I don't know, maybe the account owner hasn't changed yet or something....

bob_buzzardbob_buzzard

I can't think of any reason why that wouldn't work.  I've seen something similar done by one of our guys when a lead owned by a portal user is converted.

BellaBella

Just tried it and it doesn't work... not using ConvertedContactId and ConvertedOpportunityId anyway. I keep getting nupp poiner exceptions on Account Owner. Seems things aren't quite in the system yet when this trigger fires. Oh well, I'll try the checkbox approach.