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
manjunath vivekmanjunath vivek 

Not succeeded in writing trigger to update the field of master object when child record is created.

I have two custom objects Review__c and Job_application__c, they have master-detail relationship,Job_application__c is a master object and Review__c  is a child object.Iam trying to write a trigger to update thee field status as new for every new review__C record,
 
Iam not succeeded writing trigger

below is the code I tried
trigger child on Review__c(before insert){

List<id> parent=New list<id>();
For(Review__c R:trigger.new){
Parent.add(R.parentid);
}
for(Review__C R :[Select id,Total_Rating__c from Job_application__c where id IN: parentid]){
R.satus__c='0';
}
}

Can some one help me on this?
Best Answer chosen by manjunath vivek
@anilbathula@@anilbathula@
Hi manjunath vivek,

i think you want to update parent object satus__c field with 0 when ever a new child record is created.
if thats the case try this trigger:-
 
trigger child on Review__c(before insert){

List<id> parent=New list<id>();
list<Job_application__c>jp=new list<Job_application__c>();
For(Review__c R:trigger.new){
Parent.add(R.parentid);
}
if(!parent.isempty()){
  for(Job_application__c R :[Select id,Total_Rating__c,satus__c from Job_application__c where id IN: parentid]){
    R.satus__c='0';
    jp.add(r);
  }
update jp;
}
}

Thanks
Anil.B

All Answers

@anilbathula@@anilbathula@
Hi manjunath vivek,

i think you want to update parent object satus__c field with 0 when ever a new child record is created.
if thats the case try this trigger:-
 
trigger child on Review__c(before insert){

List<id> parent=New list<id>();
list<Job_application__c>jp=new list<Job_application__c>();
For(Review__c R:trigger.new){
Parent.add(R.parentid);
}
if(!parent.isempty()){
  for(Job_application__c R :[Select id,Total_Rating__c,satus__c from Job_application__c where id IN: parentid]){
    R.satus__c='0';
    jp.add(r);
  }
update jp;
}
}

Thanks
Anil.B
This was selected as the best answer
manjunath vivekmanjunath vivek
Hi Anil,
Thanks for reply,when I try this code , Iam getting the error message 
Error: Compile Error: Variable does not exist: parentid at line 15 column 103

 
@anilbathula@@anilbathula@
Hi manjunath

put the parent field (job_application__c something like this on child object)in the parentid.
There will be a realtion field which contains parent id on child object ,put that field in place of parent id.

Thanks
Anil.B
manjunath vivekmanjunath vivek
I got the error message Error: Compile Error: Invalid foreign key relationship: Review__c.Job_application__c at line 9 column 12
manjunath vivekmanjunath vivek
Thank you Anil, now it is working

Final working code is as below
trigger child on Review__c(before insert){

List<id> parent=New list<id>();

list<Job_application__c>jp=new list<Job_application__c>();

For(Review__c R:trigger.new){

Parent.add(R.Job_application__c);

}

if(!parent.isempty()){

  for(Job_application__c R :[Select id,Total_Rating__c,status__c from Job_application__c where id IN: parent]){

    R.status__c='New';

    jp.add(r);

  }

update jp;

}

}