You need to sign in to do that
Don't have an account?
Rahul
Hello Friends, Need your help correcting this trigger
If we select a particular picklist value for a field status_of_candidate ='Selected' in "Interviewing candidates" object then the record should be automatically created in another object which is "selected candidates" using name,email and phone values. These both objects are not related to each other through look up or Master detail. Please find the below code.
trigger ifselectedcreaterecordonsc on Interviewing_Candidates__c (before insert, before update) {
list<Interviewing_Candidates__c > Sel = [select id,name, Candidate_Email__c,Status_Of_Candidate__c,c.Candidate_Phone__c from Interviewing_Candidates__c ];
list<selected_candidates__C> lst = new list<selected_candidates__C>();
for(Interviewing_Candidates__c c : sel){
if(c.Status_Of_Candidate__c == 'Selected'){
selected_candidates__C ss = new selected_candidates__C();
ss.name = c.name;
ss.Email__c =c.Candidate_Email__c;
ss.phone__C =c.Candidate_Phone__c;
insert c;
}
}
}
Need your help
trigger ifselectedcreaterecordonsc on Interviewing_Candidates__c (before insert, before update) {
list<Interviewing_Candidates__c > Sel = [select id,name, Candidate_Email__c,Status_Of_Candidate__c,c.Candidate_Phone__c from Interviewing_Candidates__c ];
list<selected_candidates__C> lst = new list<selected_candidates__C>();
for(Interviewing_Candidates__c c : sel){
if(c.Status_Of_Candidate__c == 'Selected'){
selected_candidates__C ss = new selected_candidates__C();
ss.name = c.name;
ss.Email__c =c.Candidate_Email__c;
ss.phone__C =c.Candidate_Phone__c;
insert c;
}
}
}
Need your help
The trigger will not need an explicit insert statement again to insert objects for which it is written.
The insert will occur, and that is what caused the trigger to fire. Also, the Apex gives us trigger context variables like Trigger.New and Trigger.Old, which contains state of objects that started the transactions in ther BEFORE and AFTER state. So there is no need to query for records again, you can just use Trigger.New variable to get all the records that are part of the DML.
Also, you are not inserting the selected_candidates__C records created at all. They have been instantiasted and assigned, but not inserted. You can add them in a List<selected_candidates__C> and insert all the records in one DML operation.
Check out some resources:
Get Started with Apex Triggers
https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro
Apex Triggers
https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers
Check out the below trigger
Hope this helps!
There is no link between the Interviewing candidates object and selected candidates object. First point would be to create a lookup field on selected candidates object to lookup to Interviewing candidates object.
Next, you will have to query the selected candidates record linked to the Interviewing candidates record getting updated and set the new values to the fields.