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
Becka DBecka D 

Opportunity Create on Lead Convert

I am attempting to force creation of an opportunity on lead conversion of leads. I've attempted a trigger, but nothing I've tried so far seems to be working. This is what I've got so far:

 

trigger opptyCreationonConvert on Lead (before update) {     

List<Opportunity> o = new List<Opportunity>();
       for(Lead convertedLead: Trigger.new){

if(convertedLead.RecordTypeID == '012T00000000drN'&& convertedLead.IsConverted == TRUE && convertedLead.ConvertedOpportunityId == NULL){               

o.add (new Opportunity(

Name = convertedLead.Company,

CloseDate = date.today()+90,

StageName = '1 - Qualification'));

}

}

insert o;

}

Best Answer chosen by Becka D
Becka DBecka D

Thanks too all for the help. Turns out it was working, but creating orphan opportunities because I wasn't pushing a value into the Account field. Working on fixing that right now!

All Answers

Ritesh AswaneyRitesh Aswaney

The code looks okay, which makes me wonder about the config around lead conversion.

 

If you're trying to convert a lead manually, via the GUI, it displays a checkbox saying Do not create an Opportunity on Conversion. Wondering if you're trying to explicitly create an opportunity,coz you have this checkbox unchecked (unless this is mass lead conversion via a data upload)

 

The ConvertedOpportunityId  would not evaluate to null if salesforce is already creating an opportunity as part of the conversion. If you want to update the opportunity salesforce creates by defauly, you might want to grab the reference from ConvertedOpportunityId and update those Opportunity instances.

Becka DBecka D

What I'm trying to accomplish is if a user checks that box (and tries to convert without creating an opportunity), we will manually create one anyway. If they don'd check the box, we expect the standard process to occur with opportunity creation.

Ritesh AswaneyRitesh Aswaney

Cool, that makes sense then.

That code should work, in fact I just tried that in my org to be doubly sure, and as expected, an opportunity was created.

 

The only thing I would look at suspiciously is the RecordTypeId - double check that it is the correct record type id, or to test it, get rid of the recordtypeid and check that it works.

kevoharakevohara

How is this failing?  Exceptions?  Does it run but no Opportunity is created?

 

 

kevoharakevohara

I think you need to run this "After Update" but I'm not certain.  Have you tried that?

Becka DBecka D

Thanks too all for the help. Turns out it was working, but creating orphan opportunities because I wasn't pushing a value into the Account field. Working on fixing that right now!

This was selected as the best answer
Becka DBecka D

Errrm, too= to

Becka DBecka D

This is working, but now creating two opportunities

 

trigger opptyCreationonConvert on Lead (before update) {     
List<Opportunity> o = new List<Opportunity>();

       for(Lead convertedLead: Trigger.new){
    if(convertedLead.IsConverted == TRUE && convertedLead.ConvertedOpportunityId == NULL){               
        o.add (new Opportunity(
        Name = convertedLead.Company,
        CloseDate = date.today()+90,
        AccountId = convertedLead.ConvertedAccountId,
        StageName = '1 - Qualification'));
        }
    }
insert o;

}

knthornt1knthornt1

I would suggest trying to track this in the System Log Console. Are there any other triggers that might be firing on a lead convert? Convert a lead and see what is actually firing in addition to this trigger. I just tested this in my sandbox and it worked perfectly.

 

Also, did you try kevin80's suggestion to make this an after update?

garybgaryb

Do you have workflow on Lead? I've just setup a quick test in a dev org, just to test the theory, so apologies if the actions I've chosen don't make much sense:

 

- an after update trigger on lead (it just prints to the debug log)

- a workflow rule that fires after when record created or did not previously meet criteria, that checks "Is Converted" = true. If it is, then the email field is cleared.

 

I created a lead with a value in email, converted it, and the trigger fires twice (I see the debug statement twice). So: check your workflow on the Lead object to see if any updates are being made there.

 

Confusingly, I don't get any information in the debug log about the workflow, but I'm pretty sure it's the workflow - If I deactivate the workflow, I only see the trigger fire once. The fact that information doesn't appear makes the problem much more difficult to spot (if indeed this is the cause).

 

HTH