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
jboessojboesso 

Lead to Opportunity Trigger

Guys, i'm pretty new to apex and development on Salesforce, I'm trying to build a trigger where it populates the field Fase_Ciclo_vida__c on lead with current the stagename on Opportunity.

 

Anything i'm missing?

 

 

trigger OpportunityAfter on Opportunity (after insert, after update) {
    
    if (LibCommonFunctions.isTriggerStatusActive()) {
        
        if (trigger.isUpdate) {
            
            if (trigger.new.size() == 1) {
                
                if (trigger.new[0].IsWon) {
                    
                    Opportunity ls = [Select Id, StageName from Opportunity where IsWon=True];
                    
                    if (trigger.new[0].ConvertedOpportunityId != null) {
                        
                        Lead o = [Select Id From Lead Where Id =: trigger.new[0].Id];
                        o.Fase_Ciclo_de_vida__c = ls.Stagename;
                        update o;
                        
                    }
                    
                }
                
            }
            
        }
        
    }

}

 

davidjgriffdavidjgriff
I think there is a bit of confusion here. Opportunities do not have a "ConvertedOpportunityId" field. Is this supposed to be a trigger on Lead or on Opportunity? Are you trying to updated Leads that were converted to the Opportunity in question?
Sonu Sharma.ax1610Sonu Sharma.ax1610

Hi, Your question is seems to be incomplete. Since ConvertedOpportunityId is available on Lead when you are converting the lead into Account, opportunity and contact. I think you want to update opportunity field based on some field on lead. In that case you can write trigger on lead object after update and check for field isleadconverted on lead records.

 

Like : trigger leadconvert on Lead(after update) {

 

for(Lead led:trigger.new) {

if(led.isconverted==true){

// perform operation here

} } }

 

 

Regards -S