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
Sudipto Ghosh 12Sudipto Ghosh 12 

Code Issue while running of a Trigger

I have 2 Custom Objects 'Job_Posting__c' and 'Employment_Website__c'.
'Employment_Website__c' is the master object and 'Job_Posting__c' is the child object of the Master Detail Relationship.
 
I want to write a Trigger so that whenever there is a change in a Field 'Price_Per_Post__c' of the 'Employment_Website__c' Object, the 'City__c' Field of all the related (Child) records gets changed to 'Atlanta'.
 
This is the code I wrote:
 
trigger ChangingChildValue on Employment_Website__c (after insert, after update) {
    Job_Posting__c JP = new Job_Posting__c();
    for (Employment_Website__c EMP : Trigger.old)
    {
        System.debug('Price Per Post' + EMP.Price_Per_Post__c);
        if(EMP.Price_Per_Post__c > 50){         
            JP.City__c = 'Pune';
        }
       
    }
    update JP;
}
 
I am getting the below error when I change the Price Value and Save the parent record:
 
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ChangingChildValue caused an unexpected exception, contact your administrator: ChangingChildValue: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.ChangingChildValue: line 11, column 1

 
I am new Development, any help would be really great. Thanks.
 
SandhyaSandhya (Salesforce Developers) 
Hi Sudipto Ghosh,

Please see below code.

 
trigger ChangingChildValue on Employment_Website__c (after insert, after update) {
    List<Job_Posting__c> JP = new List<Job_Posting__c>();
        Set<ID> ids = new Set<ID>();
for( Employment_Website__c ew: Trigger.new){
        
        
            ids.add(ew.Id);
        }
    }

    List<Employment_Website__c> updatedew = [SELECT Id, EMP.Price_Per_Post__c,(Select Id,JP.City__c,from Job_Posting__c) FROM Employment_Website__c WHERE Id in :ids];

    for (Employment_Website__c EMP :updatedew )
    {
        System.debug('Price Per Post' + EMP.Price_Per_Post__c);
        if(EMP.Price_Per_Post__c > 50){  
        for( Job_Posting__c rd : EMP.Job_Posting__r){
       
            rd.City__c  ='Pune';
            JP.add(rd);       
            
        }
       
    }
    update JP;
}

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
Sudipto Ghosh 12Sudipto Ghosh 12

It is not working it showing an 'Unexpexted Token: List' error on the below line:

'List<Employment_Website__c> updatedew = [SELECT Id, EMP.Price_Per_Post__c,(Select Id,JP.City__c,from Job_Posting__c) FROM Employment_Website__c WHERE Id in :ids];

SandhyaSandhya (Salesforce Developers) 
Hi,

please replace that line with below.
 
List<Employment_Website__c> updatedew = [SELECT Id,Price_Per_Post__c,(Select Id,City__c,from Job_Posting__c) FROM Employment_Website__c WHERE Id in :ids];

Thanks and Regards
Sandhya
Sudipto Ghosh 12Sudipto Ghosh 12
Still the same Error.
SandhyaSandhya (Salesforce Developers) 
Hi,
 
List<Employment_Website__c> updatedew = List <Employment_Website__c>();

updatedew = [SELECT Id,Price_Per_Post__c,(Select Id,City__c,from Job_Posting__c) FROM Employment_Website__c WHERE Id in :ids];

Thanks and Regards
Sandhya


 
Sudipto Ghosh 12Sudipto Ghosh 12
Still not working

I modified my original code, now I am not gettiny any error in the below code but the city value is also not getting updated:


trigger ChildValueChange on Employment_Website__c (after insert, after update, before insert, before update) {
    
    List<Job_Posting__c> LJP = new List<Job_Posting__c> ();
    Set<ID> EmpID = new Set<ID> ();
    for(Employment_Website__c EW : Trigger.new){        
        EmpID.add(EW.Id);
    }
  
    LJP = [SELECT Price_Per_Post__c, City__c FROM Job_Posting__c WHERE Id in :EmpID];
    List<Job_Posting__c> JPUpdate = new List<Job_Posting__c> ();
    
     for(Job_Posting__c ejc : LJP){
        if ( ejc.Employment_Website__r.Price_Per_Post__c  > 50){
            ejc.City__c = 'Pune';
            JPUpdate.add(ejc);
       }
  }
    update JPUpdate;
}