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
Prem ChauhanPrem Chauhan 

i have created 3 fields on Opportubity which is a type of currrency and i want to calculte the min and max budget of these fields ? How should i calculate in custom fields??? ( i am writing this code in Trigger )

Best Answer chosen by Prem Chauhan
AvaneeshAvaneesh
Hii

You don't need to add trigger.isUpdate if you don't have any specific requirement 
this all code you can be replaced with mine 


    if(Trigger.isInsert){
        for(OpportunityLineItem objOppLineItem : Trigger.new){
            setOfOppLineItems.add(objOppLineItem.OpportunityId);
            oppIdToSet = objOppLineItem.OpportunityId;
        }
    }
    
    if(trigger.isDelete){
        
        for(OpportunityLineItem objOppLineItem : Trigger.old){
            setOfOppLineItems.add(objOppLineItem .OpportunityId);
            oppIdToSet = objOppLineItem .OpportunityId;
            
        }
    }
    
    //In this isUpdate only i am not getting what to do..
    if(trigger.isupdate){
        
        for(OpportunityLineItem objOppLineItem : Trigger.old){
          //  OpportunityLineItem objOppLineItemOldValue = Trigger.oldMap.get(objOppLineItem.Id);
            setOfOppLineItems.add(objOppLineItem .OpportunityId);
            oppIdToSet = objOppLineItem .OpportunityId;
            
        }
    }
 
// Here is My code 

List<OpportunityLineItem ​> allbulkdata = trigger.isdelete ? trigger.old : trigger.new ;

for(OpportunityLineItem objOppLineItem : allbulkdata ){
            setOfOppLineItems.add(objOppLineItem.OpportunityId);
            oppIdToSet = objOppLineItem.OpportunityId;
        }​


 I hope this will work fine if still, you have any problem let me know else mark as best 

Thank you 
Avaneesh Singh

All Answers

AvaneeshAvaneesh
Hii Prem Chauhan
I write a trigger for you according to your scenarios you have to copy the code and use it 
Here I used three fields as per your requirements 
name as  
Price1__c, Price2__c, Price2__c
and for their value Max_Price__c and Min_price__c
 
trigger OpporutinityPriceUpdate on Opportunity (after insert) {
    
    List<Opportunity> bulkOpp = new List<Opportunity>();
  
          for(Opportunity opp: Trigger.new)
          {
            if(opp.Price1__c !=NULL && opp.Price2__c !=NULL && opp.Price3__c !=NULL)
            {  
              Opportunity o = new Opportunity();
              o.Id = opp.Id;
              o.Name = opp.Name;  
              o.CloseDate  = opp.CloseDate;
              o.StageName =opp.StageName;
              
              if(opp.Price1__c >= opp.Price2__c && opp.Price1__c >= opp.Price3__c )
              {
                   o.Max_Price__c = opp.Price1__c;
                   if(opp.Price2__c <= opp.Price3__c )
                   {
                      o.Min_price__c = opp.Price2__c;
                   }else
                   {
                      o.Min_price__c = opp.Price3__c;
                   }
              }else if(opp.Price2__c >= opp.Price1__c && opp.Price2__c >= opp.Price3__c)
              {
                  o.Max_Price__c = opp.Price2__c;
                  if(opp.Price1__c <= opp.Price3__c )
                   {
                       o.Min_price__c = opp.Price1__c;
                   }else
                   {
                       o.Min_price__c = opp.Price3__c;
                   }
              }else
              {
                 o.Max_Price__c = opp.Price3__c;
                  if(opp.Price1__c <= opp.Price2__c )
                   {
                       o.Min_price__c = opp.Price1__c;
                   }else
                   {
                       o.Min_price__c = opp.Price2__c;
                   }
              }
            bulkOpp.add(o);
          }
        }
    
       if(bulkOpp.size()>0)
       {
           update bulkOpp;
       }
    
    
    
}

if you still have any problem then let me know and if this was good please don't forget to mark as best answer 

Thank you 
Avaneesh Singh
AvaneeshAvaneesh
Hii Prem 
You can also use this trigger they both are the same working .Chose any one you want cheer's
trigger OpporutinityPriceUpdate on Opportunity (after insert) {
    
    List<Opportunity> bulkOpp = new List<Opportunity>();
    List<Double> priceList = new List<Double>();
  
          for(Opportunity opp: Trigger.new)
          {
            if(opp.Price1__c !=NULL && opp.Price2__c !=NULL && opp.Price3__c !=NULL)
            {  
              Opportunity o = new Opportunity();
              o.Id = opp.Id;
              o.Name = opp.Name;  
              o.CloseDate  = opp.CloseDate;
              o.StageName =opp.StageName;
               priceList.add(opp.Price1__c);
               priceList.add(opp.Price2__c);
               priceList.add(opp.Price3__c); 
               priceList.sort();
              o.Min_price__c = priceList[0];// after sort first index will be minmum 
              o.Max_Price__c = priceList[2];  // and last index will be maximum
              
            bulkOpp.add(o);
          }
        }
    
       if(bulkOpp.size()>0)
       {
           update bulkOpp;
       }
    
    
    
}

Please don't forget to mark as best answer
Thank You
Avaneesh Singh 
Prem ChauhanPrem Chauhan

Hello Avaneesh !

Actually my requirment is when i update my child record then automatically the other related objects fields should be updated in this junction object is also involved .. overall I'm writing trigger for one object but only update operation is not working both insert and delete working ?

i want solution for after isUpdate ..

If you want me send my code then i am ready to send ..
 

AvaneeshAvaneesh
Hii Prem 

Send me your code with comments, please 

 
Prem ChauhanPrem Chauhan
trigger Opportunity_Product_Trigger on OpportunityLineItem (after insert, after delete, after update) {
     
    //Below four are my Custom fields on Opportunity
    Decimal totPrice = 0;
    Decimal totQuantity = 0;
    Decimal maxQuantity = 0;
    Decimal MinQuantity = 0;
    
    List<Opportunity> listOfUpdatedOpp = new   List<Opportunity>();
     // i took Set for Storing Multiple OpportunityLineItem id's
    set<String> setOfOppLineItems = new set<String>();
   // Created 3 Lists on OpportunityLineItem for Min and max (Standard object)
    List<OpportunityLineItem> ListForOpportunityLineItem = new List<OpportunityLineItem>();
    List<OpportunityLineItem> ListForOpportunityLineItemMax = new List<OpportunityLineItem>();
    List<OpportunityLineItem> ListForOpportunityLineItemMin = new List<OpportunityLineItem>();
    String oppIdToSet = '';
    
    if(Trigger.isInsert){
        for(OpportunityLineItem objOppLineItem : Trigger.new){
            setOfOppLineItems.add(objOppLineItem.OpportunityId);
            oppIdToSet = objOppLineItem.OpportunityId;
        }
    }
    
    if(trigger.isDelete){
        
        for(OpportunityLineItem objOppLineItem : Trigger.old){
            setOfOppLineItems.add(objOppLineItem .OpportunityId);
            oppIdToSet = objOppLineItem .OpportunityId;
            
        }
    }
    
    //In this isUpdate only i am not getting what to do..
    if(trigger.isupdate){
        
        for(OpportunityLineItem objOppLineItem : Trigger.old){
          //  OpportunityLineItem objOppLineItemOldValue = Trigger.oldMap.get(objOppLineItem.Id);
            setOfOppLineItems.add(objOppLineItem .OpportunityId);
            oppIdToSet = objOppLineItem .OpportunityId;
            
        }
    }
    
    if(setOfOppLineItems.size() > 0){
        ListForOpportunityLineItem = [SELECT id,name,TotalPrice, Quantity, OpportunityId  FROM OpportunityLineItem  Where OpportunityId IN : setOfOppLineItems];      
        
        ListForOpportunityLineItemMax = [SELECT id,name,TotalPrice, Quantity, OpportunityId  FROM OpportunityLineItem  Where OpportunityId IN : setOfOppLineItems  Order By Quantity desc];
        
        if(setOfOppLineItems.size() > 0){            
            maxQuantity = ListForOpportunityLineItemMax[0].Quantity;   
        }
        
        ListForOpportunityLineItemMin = [SELECT id,name,TotalPrice, Quantity , OpportunityId FROM OpportunityLineItem  Where OpportunityId IN : setOfOppLineItems  Order By Quantity Asc];
        
        if(setOfOppLineItems.size() > 0){            
            MinQuantity = ListForOpportunityLineItemMin[0].Quantity;   
        }
        
        for(String opp : setOfOppLineItems){           
            for(OpportunityLineItem objOppLineItem : ListForOpportunityLineItem){
                
                if(opp == objOppLineItem.OpportunityId){
                    totPrice = totPrice + objOppLineItem.TotalPrice ;
                    totQuantity = totQuantity + objOppLineItem.Quantity;
                    
                    
                }
            }
            
        }
        
        Opportunity objOpp = new Opportunity();
        objOpp.Id = oppIdToSet;
        objOpp.Total_Price__c = totPrice;
        objOpp.Total_Quantity__c = totQuantity;
        objOpp.Max_Quantity__c = maxQuantity;
        objOpp.Min_Quantity__c = MinQuantity;
        
        listOfUpdatedOpp.add(objOpp);
        
        if(setOfOppLineItems.size() > 0){
          update listOfUpdatedOpp;
        }
        
    }
}

working of this code is :- when i add Product to their related object Opportunity then added product details will be shown in the below which is in Opportunity related List.
Have a look once for your  reference.
insert and delete operation is working Fine but m lacking with isUpdate..
Thanks in Aadvance.
AvaneeshAvaneesh
Hii

You don't need to add trigger.isUpdate if you don't have any specific requirement 
this all code you can be replaced with mine 


    if(Trigger.isInsert){
        for(OpportunityLineItem objOppLineItem : Trigger.new){
            setOfOppLineItems.add(objOppLineItem.OpportunityId);
            oppIdToSet = objOppLineItem.OpportunityId;
        }
    }
    
    if(trigger.isDelete){
        
        for(OpportunityLineItem objOppLineItem : Trigger.old){
            setOfOppLineItems.add(objOppLineItem .OpportunityId);
            oppIdToSet = objOppLineItem .OpportunityId;
            
        }
    }
    
    //In this isUpdate only i am not getting what to do..
    if(trigger.isupdate){
        
        for(OpportunityLineItem objOppLineItem : Trigger.old){
          //  OpportunityLineItem objOppLineItemOldValue = Trigger.oldMap.get(objOppLineItem.Id);
            setOfOppLineItems.add(objOppLineItem .OpportunityId);
            oppIdToSet = objOppLineItem .OpportunityId;
            
        }
    }
 
// Here is My code 

List<OpportunityLineItem ​> allbulkdata = trigger.isdelete ? trigger.old : trigger.new ;

for(OpportunityLineItem objOppLineItem : allbulkdata ){
            setOfOppLineItems.add(objOppLineItem.OpportunityId);
            oppIdToSet = objOppLineItem.OpportunityId;
        }​


 I hope this will work fine if still, you have any problem let me know else mark as best 

Thank you 
Avaneesh Singh
This was selected as the best answer
Prem ChauhanPrem Chauhan
Hi,

Avaneesh, it's working fine... thanks :) 
Prem ChauhanPrem Chauhan
Hii Avaneesh,

If you don't mind still i am having some doubts may i ?
AvaneeshAvaneesh
Obviously Prem
 
sure You can ask whatever you want knowledge sharing are always good I really feel good 

Thank you
Avaneesh Singh
Prem ChauhanPrem Chauhan
trigger Job_Posting_Trigger_To_Update_Position_Fields on Job_Posting__c (after insert, after delete, after update) {   
 
//These 4 are my customn fields on my Custom object Position__c 
 //here Job_Posting__c  is the junction Object between " Position__c And Employment_Website__c "


    Decimal totPrice = 0;
    Decimal TotalBudget = 0;
    Decimal Totalpost = 0;
    Decimal max = 0;
    Decimal min = 0;
    
    
    List<Position__c> lstOfposupdate = new List<Position__c>();
    //Position__c objPosition = new Position__c();
    // Set<string> SetofOfEmployeeWebSiteIds = new Set<String>();
    
    Set<string> SetOfJobPosting = new Set<String>();
    // List<string> LstOfpostIds = new List<String>();
    
    List<Job_Posting__c> lstForJobPosting = new List<Job_Posting__c>();
    List<Job_Posting__c> lstForJobPostingMax = new List<Job_Posting__c>();
    List<Job_Posting__c> lstForJobPostingMin = new List<Job_Posting__c>();
    
    // List<Employment_Website__c> lstForEmpWeb = new List<Employment_Website__c>();
    string posIdToSet = '';
    
     List<Job_Posting__c > allbulkdata = trigger.isdelete ? trigger.old : trigger.new ;
       for(Job_Posting__c JobPostingObj : allbulkdata) {
            SetOfJobPosting.add(JobPostingObj.Position__c);
            posIdToSet = JobPostingObj.Position__c;
        }
    
   /* if(Trigger.isInsert){ 
        for(Job_Posting__c JobPostingObj : Trigger.new) {
            SetOfJobPosting.add(JobPostingObj.Position__c);
            posIdToSet = JobPostingObj.Position__c;
        }
    }
    
    if(Trigger.isDelete){ 
        for(Job_Posting__c JobPostingObj :Trigger.old) {
            SetOfJobPosting.add(JobPostingObj.Position__c);
            posIdToSet = JobPostingObj.Position__c;
        }
    }
    
    if(Trigger.isUpdate){ 
        for(Job_Posting__c JobPostingObj : Trigger.old) {
            Job_Posting__c  objJobPostingOldVal = Trigger.oldMap.get(JobPostingObj.Id);
            SetOfJobPosting.add(JobPostingObj.Employment_Website__c);
            posIdToSet = JobPostingObj.Employment_Website__c;
        }
    }*/
    
    If(SetOfJobPosting.size()>0){
        
        lstForJobPosting = [select Id,Name,Position__c,Employment_Website__r.Price_Per_Post__c,
                            Employment_Website__r.Maximum_Budget__c
                            from Job_Posting__c 
                            where Position__c IN:SetOfJobPosting] ;       
        
        lstForJobPostingMax = [select Id,Name,Position__c,Employment_Website__r.Price_Per_Post__c,
                               Employment_Website__r.Maximum_Budget__c
                               from Job_Posting__c 
                               where Position__c IN:SetOfJobPosting
                               Order By Employment_Website__r.Price_Per_Post__c desc] ; 
        
        if(lstForJobPostingMax.size()>0){            
            max = lstForJobPostingMax[0].Employment_Website__r.Price_Per_Post__c;   
        }
        
        
        lstForJobPostingMin = [select Id,Name,Position__c,Employment_Website__r.Price_Per_Post__c,
                               Employment_Website__r.Maximum_Budget__c
                               from Job_Posting__c 
                               where Position__c IN:SetOfJobPosting
                               Order By Employment_Website__r.Price_Per_Post__c Asc]; 
        
        if(lstForJobPostingMin.size()>0){            
            min = lstForJobPostingMin[0].Employment_Website__r.Price_Per_Post__c; 
        }
        
        for(String posId : SetOfJobPosting){   
            Totalpost = lstForJobPosting.size();
            for(Job_Posting__c jpObj : lstForJobPosting){ 
                
                if(posId == jpObj.Position__c){    
                    
                    totPrice = totPrice + jpObj.Employment_Website__r.Price_Per_Post__c ;
                    TotalBudget = jpObj.Employment_Website__r.Maximum_Budget__c + TotalBudget;                    
                }
            }
        }
        
        Position__c posUpdate = new Position__c();
        posUpdate.Id = posIdToSet;
        posUpdate.Total_Budget__c = TotalBudget;
        posUpdate.Total_Price_of_post__c = totPrice;
        posUpdate.Min_Price_of_Post__c = min ;
        posUpdate.Max_Price_of_Post__c = max;
        posUpdate.Total_Post__c = Totalpost;
        
        lstOfposupdate.add(posUpdate);
        
        if(lstOfposupdate.size()>0){
            update lstOfposupdate;
            System.debug('<==Updated succesfully==>');
        }
        
        
    }
}
This is Position__CJob_Posting__c
When i click on new job Posting button it will ask me to select employment website when i select any one among multiple website then Price Per Post Maximum Budget  is added Automatically, when i do update for inserted job posting the fields on the position won't get update for that perticular position,
In this case also Update is not working.. i had tried previous code but dosen't work .. Don't know why ? 
AvaneeshAvaneesh
Hii
I think it should work but you can use debug Why this was not working but seems to look correct 
did you get any error 
Prem ChauhanPrem Chauhan
I din't get any error but fields on Position__c are not getting update,
Only related list is getting updated ..
Prem ChauhanPrem Chauhan
Hii Avaneesh,
Will you able to solve my problem or not ?? 


 
AvaneeshAvaneesh
Hi Prem,
i tried but I'm Stuck with your code didn't  get any solution post your code as a new Question somebody will definitely be resolved 
Prem ChauhanPrem Chauhan
ok fine .. thnks