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
lakshya agrawallakshya agrawal 

trigger for 1.2??

Question 1:- Create Trigger to fulfil below Scenarios?
1.1:- If AFAO Type Opportunity is Closed Won, Create new AFAO Opportunity.

Answer:- 

Apex Class:

public class TriggerHandlerOpportunity {
  public static Boolean isFirstTime = true;     
    public static void createNewAFAO(List<Opportunity> oppList,map<id,Opportunity> oldMap){
        List<Opportunity> OpportunityList = new List<Opportunity>();
        for(Opportunity op:oppList){
            if(op.Opportunity_Type__c == 'AFAO' &&  (op.StageName == 'Closed Signed Up (Won)' &&  oldMap.get(op.id).StageName != op.StageName)){
                Opportunity newOpportunity = new Opportunity();
                newOpportunity.Name = op.name;
                newOpportunity.StageName = op.Stagename;
                newOpportunity.CloseDate = op.CloseDate;
                newOpportunity.AccountId = op.AccountId;
                newOpportunity.Opportunity_Type__c = op.Opportunity_Type__c;
                 OpportunityList.add(newOpportunity);
                    
            }
        }
        insert OpportunityList;
        
    } 
}

Trigger:

trigger TriggerOnOpportunity on Opportunity (after update) {
       if(trigger.isafter && trigger.isupdate){
         if(TriggerHandlerOpportunity.isFirstTime ){
        TriggerHandlerOpportunity.isFirstTime = false;
        TriggerHandlerOpportunity.createNewAFAO(trigger.new,trigger.oldmap);
                  }
            } 
    }

1.2  Move all the existing USM Records with the status “Pending” to another AFPO, and "Convo started”/ “Open" to newly created AFAO.

Hint:

 (a) USM Object -Picklist Field “CAC Status”- (Values - Open, Convo started, Closed Won, Closed Lost, Pending) 

(b) Opportunity Object - Piclklist Field "Opportunity Type"-(Values-AFAO,NAFS,AFPO)

(c) Opportunity and Usm have lookup relationship between them.

(d) Opportunity is parent and USM is child.

Can anyone help by writing trigger for it 

Please see question 1.1 first i.e done but 1.2 can be done with help of 1.1.

please help me.
SwethaSwetha (Salesforce Developers) 
HI Lakshya ,
The developer community recommends providing any attempts/code you've started, any errors you're getting, or where exactly you're struggling in achieving this while posting a question. Thanks