Advertisement
f1lam3ntx0

opportunity more than 30 days via account

Aug 21st, 2022 (edited)
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. // ? 2. Write a trigger on the Account when the Account is updated check all opportunities related to the account. Update all Opportunities Stage to close lost if an opportunity created date is greater than 30 days from today and stage not equal to close won.
  2.  
  3.  
  4.  
  5. Trigger oppUpdateOnAcc on Account (After Update){
  6.  
  7.   switch on Trigger.operationType{
  8.     when AFTER_INSERT{
  9.       oppUpdateOnAccHelper.checkOppStage(Trigger.new);
  10.     }
  11.   }
  12. }
  13.  
  14. public with sharing oppUpdateOnAccHelper{
  15.   public static void checkOppStage(List<Account> accList){
  16.   Set<Account> aCheck = new Set<Account>();
  17.   if(accList != null){
  18.     for(Account a : accList){
  19.     aCheck.add(a.Id);
  20.     }
  21.     //calc data--
  22.     DateTime LastDay = system.now() + 30;
  23.     List<opportunity> oppUpdate = new List<opportunity>();
  24.     List<Opportunity> oppLst = [SELECT id, name , stagename, ammount , description from opportunity ];
  25.     if(oppLst.size() > 0){
  26.       for(Opportunity opp : oppLst){
  27.         if( opp.createdDate > LastDay && opp.stagename == 'Close Wom'){
  28.         opp.Stagename == 'Close Lost';
  29.         opp.CloseDate = system.now();
  30.         opp.description = 'closed by Trigger oppUpdateOnAcc';
  31.         oppUpdate.add(opp);
  32.         }
  33.        
  34.       }
  35.       if(oppUpdate.size() > 0){
  36.       INSERT oppUpdate;
  37.       }
  38.     }
  39.  
  40.   }
  41.   }
  42. }
Tags: Triggers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement