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
RahulRahul 

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
Manjunath S 75Manjunath S 75
Hi Sumit,

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
trigger ifselectedcreaterecordonsc on Interviewing_Candidates__c (before insert, before update) {

    List<selected_candidates__C> selectedCandidatesList = new List<selected_candidates__C>();

    for(Interviewing_Candidates__c c : Trigger.New){
        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;
        selectedCandidatesList.add(ss);        
        }
    }
    insert selectedCandidatesList;
}


Hope this helps!
RahulRahul
Hi Manjunath, Thanks for the Reply. This trigger is creating the record in selected candidates object when the record is created for the first time from "Interviewing candidates" object but when the record was updated, it's creating another record in selected candidates object, instead it should update the previous record and not create a new record on update
Manjunath S 75Manjunath S 75
Hi Sumit,

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.