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
SunnyButCloudySunnyButCloudy 

Lead Convert to carry lookup to custom object?

Hello,

 

I have a problem... I have a custom object that has a lookup to the Leads object.  (Leads >> Feedback)

 

Upon converting the lead, I would like the like the feedback object to attach to the newly created opportunity.  How would I attempt to do this? The feedback object also has a lookup to an opportunity.  Any help is appreciated.

 

There's a similar thread here but doesn't help much: http://boards.developerforce.com/t5/General-Development/Converting-Lead-lookup-relationship-w-custom-object/m-p/69891

 

Thank you-

 

Kenny 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

Aagh. Got it. You had reversed the order (Parent vs Child) of the relationship between Lead and Feedback in your original post and hence this confusion. Try this:

 

trigger populateFeedback on Lead (after update)

{

Map<Id, Id> feedback2OppId = new Map<Id, Id>();

For (Lead l : Trigger.new)

       {

             if (l.isConverted && l.convertedOpportunityId != null)

             {

                      feedback2OppId.put(l.Id, l.convertedOpportunityId);

             }

       }

 

      List<Feedback__c> fbs = [select lead__c, opportunity__c from Feedback__c where lead__c in :feedback2OppId.keySet()];

     for (Feedback__c f : fbs)

     {

            f.opportunity__c = feedback2OppId.get(f.lead__c);

     }

 

     update fbs;

}

All Answers

forecast_is_cloudyforecast_is_cloudy

You'll need to write an after update trigger on the Lead object to accomplish this. Something like

 

trigger populateFeedback on Lead (after update)

{

Map<Id, Id> feedback2OppId = new Map<Id, Id>();

For (Lead l : Trigger.new)

       {

             if (l.isConverted && convertedOpportunityId != null)

             {

                      feedback2OppId.put(l.feedback__c, l.convertedOpportunityId);

             }

       }

 

      List<Feedback__c> fbs = [select opportunity__c from Feedback__c where id in :feedback2OppId.keySet()];

     for (Feedback__c f : fbs)

     {

            f.opportunity__c = feedback2OppId.get(f.Id);

     }

 

     update fbs;

}

SunnyButCloudySunnyButCloudy

Thanks Forecast!  I will try this today and let you know how it goes.. Thanks again!

SunnyButCloudySunnyButCloudy

I'm getting an error when I try to save:

 

Variable does not exist: convertedOpportunityId

 

Any help?

 

forecast_is_cloudyforecast_is_cloudy

Typo in the code. Try replacing this line of code with: 

 

if (l.isConverted && l.convertedOpportunityId != null)

 

Also, please note that my code was meant for illustrative purposes only. It may not compile for you since I made some assumptions about your Field API Names (like 'opportunity__c', 'feedback__c' etc.). Please replace those API names as appropriate before you try and save this in your Org.

SunnyButCloudySunnyButCloudy

Thanks for the quick reply.  I altered the code to make it fit my verbiage (opportunity = related_opportunity__c), etc. 

 

However when I save in Eclipse, I get a new error regarding:

 

     feedback2OppId.put(l.feedback__c, l.convertedOpportunityId);

 

where it says "l.feedback__c" is not a field of the Sobject.  Well it's not a field on the lead object, it's a related list.

 

I can see how my first post was maybe a little confusing, so here's my retry :)

 

I have a feedback object that looks up to the opportunity.  On the opp record, i see it as a related list (could be more than 1 record).  When I convert, I want that feedback record(s) to get attached to the newly created opportunity.  

 

The lead object itself doesn't have a field that relates to Feedback object.  

 

I hope that makes sense.. Sorry for re-telling the story but I'm fairly new to this.  Thanks in advance for your help again-

 

Kenny

forecast_is_cloudyforecast_is_cloudy

Hmm. I'm not sure I follow v2 of the requirements :).

You said - "When I convert, I want that feedback record(s) to get attached to the newly created opportunity."

 

If there is no relationship between a Lead and Feedback object, how do you know which Feedback record(s) to associate with the converted Opp record?

SunnyButCloudySunnyButCloudy

haha...v2 

 

well there is a relationship between lead >> custom object.  in the sense that the custom object has a lookup field back to the lead.

 

its a not a field on the lead object, it's a custom object record(s) that are on the related list for a lead object.

 

I'm a visual type of person so here's a diagram:

 

Lead

--Related List: Feedback__c object/records

 

When I convert the lead to an account/opportunity/contact, I would like to have:

 

Opportunity

--Related List: Feedback__c object/records

 

 

The Feedback__c object has a lookup to both, Lead & Opportunity objects.  Currently, when I convert a lead, the feedback object dissappears and I have to manually find it and link it back to the opportunity.  Is there anyway to skip that extra manual step?

 

:)  Thanks again.  

 

 

forecast_is_cloudyforecast_is_cloudy

Aagh. Got it. You had reversed the order (Parent vs Child) of the relationship between Lead and Feedback in your original post and hence this confusion. Try this:

 

trigger populateFeedback on Lead (after update)

{

Map<Id, Id> feedback2OppId = new Map<Id, Id>();

For (Lead l : Trigger.new)

       {

             if (l.isConverted && l.convertedOpportunityId != null)

             {

                      feedback2OppId.put(l.Id, l.convertedOpportunityId);

             }

       }

 

      List<Feedback__c> fbs = [select lead__c, opportunity__c from Feedback__c where lead__c in :feedback2OppId.keySet()];

     for (Feedback__c f : fbs)

     {

            f.opportunity__c = feedback2OppId.get(f.lead__c);

     }

 

     update fbs;

}

This was selected as the best answer
SunnyButCloudySunnyButCloudy

This worked perfectly!  Thank You x 100 for your help!!!!!!!!!!  

 

I owe you a beer..cheers!

SunnyButCloudySunnyButCloudy

Thanks again for your help.  I was able to test the functionality and it works exactly as wanted!  I went ahead and did all my testing in sandbox and prepared the setup in production.  Now, when I try to deploy via eclipse IDE i get an error saying I don't have a test class (0% covered) and my overall is below the 75% threshold.  Would you happen to know how to create a test class? i looked around and seems a little rough.  I'm in the apex manual right now, and I can understand what it needs to do (in a sense) but i'm not sure how to get this going ( i really need to catch up on apex this weekend).  

 

you can email me if you'd like..or point me in the right direction that will be helpful.  thank you once again.

 

ken.girbaldi@gmail.com 

 

forecast_is_cloudyforecast_is_cloudy

The testing Apex section in the Apex Dev guide should be your starting point (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing.htm) for understanding the overall Apex testing framework. In addition here is a great article on the same subject - http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods .

 

In your case, you basically have to start by writing a test class that has the following basic steps

1) Create your test data. This would mean creating test Lead and Feedback records

2) Convert the test Lead record using the 'convertLead' Apex method

3) Assert to confirm that the resulting Opportunity record is related to the test Feedback record.

 

Hope this helps and best of luck! 

SunnyButCloudySunnyButCloudy

Thanks for the references! 

forecast_is_cloudyforecast_is_cloudy

You're most welcome. Best of luck with your testing....

SunnyButCloudySunnyButCloudy

So I tried creating a test class, got stuck.  I'm not sure how to do the converlead method and I asked support for help.  They put the trigger in a class and created a test class that does, pretty much, nothing.  Any way you can help me with this?  I hate to ask you for help but I'm mentally exhausted. 

SunnyButCloudySunnyButCloudy

lol..finally got it. i wasn't using "update lead;"  i was only using insert.  now live in production...thanks again for everything forecast!

lenstevenslenstevens

This solution works perfectly.  Thanks all!

robertstjohnrobertstjohn

Any chance you have your test class available and/or are willing to share?  I followed you up to the point of needing the test coverage...your help would be greatly appreciated. 

 

Cheers. 

 

MinderMinder

Yes it would be great to get a sample of the test coverage to cover this. 

salesforce_hoonigansalesforce_hoonigan
Hi @SunnyButCloudy,

Would you mind sharing your test class?

Hi @forecast_is_cloudy,

Would you also be able to create a sample test class? I am sorry and I am not a code guy so I would appreciate if you can provide us a sample so non-coder would also benefit. :) thanks
Steven Keker 3Steven Keker 3
Does anyone have a test class for this code? Very new to Apex but was able to use this code for my purpose; only issue is getting it from sandbox to prod. Any help is much appreciated. Thanks!
Daven Davis 3Daven Davis 3
I am new to creating triggers after years of writing formulas and have run into an issue with the 12 duplicates for this same process in process builder. More than 12 times the same account will be referenced. I have a similiar issue.  I have a custom object that is associated with the Account and Lead.  I would like the custom object to move to the Account record once the lead is converted.   Not Sure what the apex trigger is missing as I copied and pasted 
Custom Object : IATA__c 

trigger IATAKick on Lead (after update) {
{
Map<Id, Id> IATA = new Map<Id, Id>();
For (Lead l : Trigger.new)
       {
             if (l.isConverted && l.convertedOpportunityId != null)
             {
                      feedback2OppId.put(l.Id, l.convertedAccountId);
             }
       }
      List<IATA> fbs = [select Agency__c from IATA__C where lead__c in :feedback2OppId.keySet()];
     for (IATA__c f : fbs)
     {
            f.IATA__c = feedback2OppId.get(f.lead__c);

     }
     update fbs;
}
    
Problem : Expecting '}' but was: '<EOF>'

1. Can you assist me with this code