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
lakshmi salesforcelakshmi salesforce 

how to write a code in trigger to change the master field through detail field

hi everyone
             Actually i have an issue like i have two objects 1)position(master),and 2)job applications(detail).I have fileds in both the objects as

position-------------status(pick list data type(open,new,closed))
job applications-----------------status(picklist(open,new,closed))
                      ------------------ position(master detail relationship field)

             now i have need to write a trigger on job apllication object such that  whenever the job applications status has been changed to closed,the trigger should chage the status of position to closed automatically.i have written the code as following ,but it is showing the following error

code    
trigger jobApplicationAssociatePositionClose on Job_Application__c (after insert,after update) {
    for(Job_Application__c jobApp:trigger.new){
   if(jobApp.Status__c=='closed')
      jobApp.position__c.Status__c='closed';
}
}      

error:invalid foreign key relationship :job_application__c.position__c

        anybody  please help me on this issue
Best Answer chosen by lakshmi salesforce
BALAJI CHBALAJI CH
Hi Lakshmi,

You can fetch the record, before updating the field of master object and then do the changes to required fields.
I have just revised your code, pleaselet me know if it works for you.
 
trigger jobApplicationAssociatePositionClose on Job_Application__c (after insert,after update) {
    
    for(Job_Application__c jobApp:trigger.new){
        if(jobApp.Status__c == 'closed')
        {
            //Querying parent record of jobApp
            position__c pos = [select id, Status__c from position__c where id=:jobApp.ParentId];
            pos.Status__c = 'closed';
            update pos;
        }
    }
}

Best Regards,
BALAJI

All Answers

BALAJI CHBALAJI CH
Hi Lakshmi,

You can fetch the record, before updating the field of master object and then do the changes to required fields.
I have just revised your code, pleaselet me know if it works for you.
 
trigger jobApplicationAssociatePositionClose on Job_Application__c (after insert,after update) {
    
    for(Job_Application__c jobApp:trigger.new){
        if(jobApp.Status__c == 'closed')
        {
            //Querying parent record of jobApp
            position__c pos = [select id, Status__c from position__c where id=:jobApp.ParentId];
            pos.Status__c = 'closed';
            update pos;
        }
    }
}

Best Regards,
BALAJI
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Issue in your code
1) In your code are try to update position__c object from Job_Application__c object trigger in that case you need to query Position object.

TRy below code.
trigger jobApplicationAssociatePositionClose on Job_Application__c (after insert,after update) 
{

	Set<Id> setPositionID = new Set<ID>();
	
    for(Job_Application__c jobApp:trigger.new)
	{
		if(jobApp.Status__c=='closed' && jobApp.position__c != null)
		{
			setPositionID.add(jobApp.position__c)
		}
	}	

	Map<id,position__c> mapPosit = new Map<id,position__c>([select id ,Status__c from position__c where id in:setPositionID]);
	List<position__c>	lstpositionToUpdate = new List<position__c>();
	
    for(Job_Application__c jobApp:trigger.new)
	{
		if(jobApp.Status__c=='closed' && jobApp.position__c != null)
		{
			If(mapPosit.containsKey(jobApp.position__c) )
			{
				position__c p = mapPosit.get(jobApp.position__c);
				p.Status__c='closed';
				
				lstpositionToUpdate.add(p);
			}
		}
	}
	if(lstpositionToUpdate.size() > 0 )
	{
		update lstpositionToUpdate;
	}	

}
Let us know if this will help you


 
Rahul KhilwaniRahul Khilwani
Hi Lakshmi,

You need to revise your trigger code a bit to make it work.
 
trigger jobApplicationAssociatePositionClose on Job_Application__c (after insert,after update) {
    if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate)){
        list<Position__c> updatePositionList = new list<Position__c>();
        for(Job_Application__c jobApp:trigger.new){
            if(jobApp.Status__c == 'closed'){
                 Position__c pos = new Position__c();
                 pos.Id = jobApp.Position__c;
                 pos.Status__c = 'closed';
                 updatePositionList.add(pos);
            }
        }
        update updatePositionList;
    }
}

 
lakshmi salesforcelakshmi salesforce
thanks everyone i have solved it thanks for your response
lakshmi salesforcelakshmi salesforce
i got a solution before i read your suggestions thanks for your response