• Siddharth Khambe
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I am new to APEX and would need some direction as where I am wrong. In the following I am just trying to update the description of the Opportunity when it is Closed Lost, Closed Won or when it is in any other status
trigger OpportunityTrigger on Opportunity (before insert,after insert) {
    
    if(trigger.isInsert){
        if(trigger.isBefore){
        OpportunityTriggerHandler.UpdateLatestOpportunityAmt(trigger.new);
        OpportunityTriggerHandler.UpdateDescriptionWhenOpenOrLost(trigger.new, null);
        }
    } 
    
    if(trigger.isUpdate){
        if(trigger.isbefore){
        OpportunityTriggerHandler.UpdateDescriptionWhenOpenOrLost(trigger.new, trigger.oldMap);
      }
    }

}
Trigger handler
 
public class OpportunityTriggerHandler {
    
    
    public static void UpdateLatestOpportunityAmt(list<opportunity> OppList){
        list<account> accListTemp = new list<account>();
        
        for(Opportunity Op : OppList){
            account acc = new account();
            acc.id=Op.AccountId;
            acc.Latest_Opportunity_Amount__c = Op.Amount;
            accListTemp.add(acc);
        }
        if(accListTemp!=null){
        update accListTemp;    
        }
        
    }//------ end 
    
    public static void UpdateDescriptionWhenOpenOrLost(list<opportunity> oppList, Map<id,Opportunity> oldMap){
        
        for(Opportunity opp:oppList){
            //if insert operation was done 
            if (oldMap ==null || opp.StageName != oldMap.get(opp.id).StageName){
                if(opp.StageName=='Closed Won'){
                    opp.Description ='Opportunity is Closed Won';
                    system.debug('Opportunity is Closed Won');
                
                } else if (opp.StageName=='Closed Lost'){
                    opp.Description ='Opportunity is Closed Lost';
                    system.debug('Opportunity is Closed Lost');
                
                }else{
                    opp.Description ='Opportunity is Open';
                    system.debug('Opportunity is open');
                }
            }
            
        }
        
    }

}

Thanks, 
Gunwinder
 

I am new to apex, I am writing an apex class and I have the salesforce id, how do I update the contact record if I have the salesforce id.

Thanks