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
ChuckchoChuckcho 

convert lead to a custom object in addition to account,contact & oppurtunity

when a lead is converted we want to create a new record in a custom object  in addition to account, contact and oppurtunity. Can anyone advise how to achieve this?? We also want to use a logic for account type. When the account type is "A" we want to create account,contact and oppurtunity, If the account type is "B" we want to create account, contact, oppurtunity and a custom object

 

Thanks

scottj2scottj2

I would do this in a trigger on the opportunity record (on insert).  From the trigger, look up using soql a Lead that was just converted using the convertedOpportunityId field, and if 1 exists, you know that the opportunity is just being converted.  From there, create and insert your new Object:

 

    List<ID> opportunityIds = new List<ID>();
    for (Opportunity opp : Trigger.new) {
    	System.debug('opp.Id: ' + opp.Id);
if(opp.account.recordtype.name == 'B'){ opportunityIds.add(opp.Id);
} } List<CustomObject> objs = new List<CustomObject>(); for(Lead lead : [SELECT ID, ConvertedOpportunityId, ConvertedAccountId, CreatedById FROM Lead where ConvertedOpportunityId IN :opportunityIds ]){ CustomObject obj = new CustomObject(); //populate your custom obj from your lead fields } insert objs;

 

The funniest thing is, cause I've just posted a question regarding very similar code right here:

 

http://boards.developerforce.com/t5/Apex-Code-Development/Strange-trigger-test-error-on-Lead-conversion/td-p/344571

 

In my example, instead of also creating a new record, i'm simply adding a record to the EntitySubscription table so that the creator of the Lead auto-follows the account/opportunity.  Your code will be almost the same, except you just create a new custom object as well.

 

Hope this helps you, and I hope you don't run into the same problem when writting a unit test for it!!

 

Edit:  Added your condition for RecordType on the account.