0

I am trying to determine if the lookup field Book__c (a child record) exists on a Project__c object (parent record). If it doesn't I would like to create and then update the values on the new child record from values on Parent.

    public static void createPrice(Id newProj2) {
        List<Book__c> bookList = new List<Book__c>();
        bookList.add(new Book__c(Project__c = newProj2));
    }

    public static void updateBook(list<Project__c> newpj) {
        list<Book_c> newpbe = new List<Book_c>();
        for (Project__c pj : newpj) {
             if (pj.Book__r == null && pj.Zoning__c == 'Test') {
                createSubPriceBook(pj.id);
    }

It never makes it to the createSubPriceBook() method. In the debug I see the pj.Book__r is not equal to null.

Instead, I see this: pj.Book__r : () Is that blank?

The second thing is can I query the master and child record like this after the child record is created?

pjrectest = [SELECT Id,(Select Booktest, Project__c from Book__c) from Project__c]; I need to update a couple of fields on the newly created Book record.
Does the query need to be in a separate method?

P

1 Answer 1

0

Your first question—

Instead I see this: pj.Book__r : () Is that blank?

A1: Yes, it's blank. Always use field.isEmpty() for list like: if(!pj.Book__r.isEmpty())

Your second question:

A2: Yes you can query but after inserting/updating. Use insert Book; otherwise, the record will not be found as it is still not committed to the database.

3
  • Thanks for the reply. Trying to picture what the second question will look like. 'createSubPriceBook(pj.id); } pjrectest = [SELECT Id,admin__c(Select Booktest, Project__c,zone__c from Book__c) from Project__c]; if (pjrec.size() > 0) { for (Book__c pbe : pj.Book__r) {pbe.zone__c =pjrecc; bit confused on assignment. Or would it be pjrec.Book__r.zone__c=pjrec__c
    – Phuc
    May 5, 2020 at 0:18
  • if (!pj.Book__r.isEmpty() && pj.Zoning_Type__c == 'Test') still appears not to be working. Confirmed both values and if I remove the pj.Boo__r from the criteria continued to the 'createPrice' method. Any ideas why the Book__r condition is not working?
    – Phuc
    May 5, 2020 at 1:37
  • 1
    Then we need your whole code. Pretty sure, order of execution or events should be the issue.
    – Ayub
    May 5, 2020 at 10:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .