Advertisement
f1lam3ntx0

create field on opportunity and check for increment value

Aug 21st, 2022
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. ## > Create a field on Opportunity Line item(Serial No (Text)) and populate increment values once an Opportunity Line Item is added. Let’s say if we add 3 products then the sequence would be “1,2,3”. Now if we delete 2 and again add one more product this must be 4 irrespective of the deleted sequence number.
  2.  
  3.  
  4.  
  5.  
  6.  Trigger oppLineItemCountTrigger on Opportunity (After Insert , After Update){
  7.   switch on Trigger.OperationType{
  8.     when AFTER_INSERT{
  9.       oppLineItemCountHelper.lineItemCheck(Trigger.new);
  10.     }
  11.     when AFTER_UPDATE{
  12.     oppLineItemCountHelper.lineItemCheck(Trigger.new);
  13.     }
  14.   }
  15.  }
  16.  
  17.  
  18.  Public with sharing class oppLineItemCountHelper{
  19.   public static void lineItemCheck(List<Opportunity> oppList){
  20.       if(oppList.size() > 0){
  21.         Set<Opportunity> oppSet = new Set();
  22.         for(Opportunity opp: oppList){
  23.         oppSet.add(opp.Id);
  24.         }
  25.        List<Opportunity> oppUpdated = new List<Opportunity>();
  26.        List<Opportunity> oppData = [SELECT Id, firstName, LastName, Ammount, StageName, Serial_No__c from Opportunity where Id =: oppSet];
  27.        for(Opportunity oppObj : oppData){
  28.         for(int i=0; i < oppList.size() ; i++){
  29.           oppObj.Serial_No__c = i;  
  30.         }
  31.         oppUpdated.add(oppObj);
  32.        }
  33.        if(oppUpdated.size()> 0){
  34.        INSERT oppUpdated;
  35.        }
  36.       }
  37.     }
  38.  }
Tags: Triggers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement