You need to sign in to do that
Don't have an account?
SunnyButCloudy
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
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
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;
}
Thanks Forecast! I will try this today and let you know how it goes.. Thanks again!
I'm getting an error when I try to save:
Variable does not exist: convertedOpportunityId
Any help?
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.
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
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?
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.
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 worked perfectly! Thank You x 100 for your help!!!!!!!!!!
I owe you a beer..cheers!
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
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!
Thanks for the references!
You're most welcome. Best of luck with your testing....
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.
lol..finally got it. i wasn't using "update lead;" i was only using insert. now live in production...thanks again for everything forecast!
This solution works perfectly. Thanks all!
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.
Yes it would be great to get a sample of the test coverage to cover this.
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
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