Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## > 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.
- Trigger oppLineItemCountTrigger on Opportunity (After Insert , After Update){
- switch on Trigger.OperationType{
- when AFTER_INSERT{
- oppLineItemCountHelper.lineItemCheck(Trigger.new);
- }
- when AFTER_UPDATE{
- oppLineItemCountHelper.lineItemCheck(Trigger.new);
- }
- }
- }
- Public with sharing class oppLineItemCountHelper{
- public static void lineItemCheck(List<Opportunity> oppList){
- if(oppList.size() > 0){
- Set<Opportunity> oppSet = new Set();
- for(Opportunity opp: oppList){
- oppSet.add(opp.Id);
- }
- List<Opportunity> oppUpdated = new List<Opportunity>();
- List<Opportunity> oppData = [SELECT Id, firstName, LastName, Ammount, StageName, Serial_No__c from Opportunity where Id =: oppSet];
- for(Opportunity oppObj : oppData){
- for(int i=0; i < oppList.size() ; i++){
- oppObj.Serial_No__c = i;
- }
- oppUpdated.add(oppObj);
- }
- if(oppUpdated.size()> 0){
- INSERT oppUpdated;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement