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 

How to write Trigger.isUpdate functionality for junction object ( in this there are 3 objects included in my code) i am not able to update custom fields for perticular object through trigger Any one help me please....??????????????

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==>');
        }
        
        
    }
}




User-added image
User-added image
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 records the fields on the position won't get update for that perticular position, Only related list is getting update for that perticular position.
In this case Update is not working..
[ There are totally 3 objects are there 1:jobPosting__c  2:  Employment_website__c 3 :Position__c ]


Thanks in Advance 

Prem Chauhan
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Prem Chauhan, hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
Prem ChauhanPrem Chauhan
Hi Rahul,

This link is not helpful for me, please suggest me some idea or else some other tips regarding Junction object. It's better you should just re-correct or modify my code and send me back to suit my requirmnents. :)
 Hope you got my requirment in the previous question with screenshot.. 

Thanks
Prem Chauhan.