+ Start a Discussion
SFineSFine 

Modifying Opportunity Field on lead Conversion

Hello everyone,

 

I'm currently trying to get a field to change on the opportunity when it's converted from a lead, using a custom field from the lead itself. However, I can't seem to get it change properly. My code is below. Any advice would be appreciated

 

trigger Configero_Lead_OpportunityConversion on Lead (before update) {

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

if(convertedLead.IsConverted == TRUE && convertedLead.ConvertedOpportunityId != NULL){               
oids.add(convertedLead.ConvertedOpportunityId);

}

o=[select id from opportunity where id IN: oids];

for(Lead convertedLead: Trigger.new){
    for(opportunity op:o)
    {
        if(op.id==convertedlead.ConvertedOpportunityId)
            op.name+=convertedLead.Proposed_Opportunity_Name__c;
    }
}

upsert o;

}

 

rvkrvk

 

trigger Configero_Lead_OpportunityConversion on Lead (before update) {
List<opportunity> o = new List<opportunity>();
Map<ID,ID> oids=new Map<Id,id>();
       for(Lead convertedLead: Trigger.new){

if(convertedLead.IsConverted == TRUE && convertedLead.ConvertedOpportunityId != NULL){               
oids.put(convertedLead.Id,convertedLead.ConvertedOpportunityId);

}

Map<Id,Opportunity> oppMap= new Map<Id,opportunity>([select id,name from opportunity where id IN: oids.values()]);

for(Lead convertedLead: Trigger.new){
   Opportunity op = o.get(oids.get(convertedlead.ConvertedOpportunityId));
   op.name+=convertedLead.Proposed_Opportunity_Name__c; o.add(op)}

upsert o;
 
you forgot to add the name field in the query.
AshanAshan

I think convertedLead.ConvertedOpportunityId will be Null in a before update trigger while converting.