2

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);     

}
  • Yes you are you facing any issue – Tushar Sharma Jan 2 '17 at 15:57
  • Yes, actually my aim really is to create a new custom object based on that condition I've posted. Each time a 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
  • If you are doing this in trigger then in trigger we don't get parent field like this you need to query them. – Tushar Sharma Jan 2 '17 at 16:05
  • @AdrianLarson yes it is. I am just confirming if it's possible to use it directly like that. – beep Jan 2 '17 at 16:06
  • @AdrianLarson I've just modified my post, added the codes I am currently working and also some brief description about the problem I am encountering right now. – beep Jan 2 '17 at 16:41
2

Your Author__c query cannot possibly return any records. You instantiate a Set<Id> and never populate it with anything. Basically, your filter is equivalent to:

WHERE Id IN ()

This filter will never return any results. Instead of using this Set, simply filter on the List of records you already have:

WHERE Id IN :authorList

If you really want to get the Set<Id> corresponding to authorList, the correct methodology is:

Set<Id> recordIds = new Map<Id, Author__c>(authorList).keySet();
  • 1
    I followed your simpler solution: filter the list of records I have. It's working now. Thank you very much. – beep Jan 2 '17 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.