Say I have a custom object named Book__c
with fields Title__c
and a checkbox named New__c
, and another custom object named Author__c
with field Book__c
, can I use New__c
field directly with Author__c
object in a condition?
The requirement is to automatically create a new Obj_To_Insert__c
record when a new Author__c
record with Book__c
field having its field New__c
set to true.
For example, can I use it like this?
List<Author__c> authorList = new List<Author__c>
for(Author__c auth : authorList) {
if(auth.Book__r.New__c == true) {
//some code...
}
}
The code above is a snippet from this apex class:
public class ThisIsTheClass {
public static void theMethod(List<Author__c> authorList) {
Set<Id> id = new Set<Id>();
List<Obj_To_Insert__c> otiList = new List<Obj_To_Insert__c>();
authorList = [SELECT Id, Name, Book__c, Book__r.New__c FROM Author__C where Id IN: id];
for(Author__c auth : authorList) {
if(auth.Book__r.New__c == true) {
Obj_To_Insert__c oti = new Obj_To_Insert__c();
oti.Field1__c = auth.Id;
otiList.add(oti);
}
}
insert otiList;
}
}
Meanwhile, the apex class is called in the apex trigger below:
trigger thisIsTheTrigger on Author__c (after insert) {
ThisIsTheClass.theMethod(Trigger.New);
}
Author__c
record with that criteria is inserted, a new custom object record must be inserted also. When I use the condition like that, the new custom object is not inserted at all. – beep Jan 2 '17 at 16:03