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
AndrewNerneyAndrewNerney 

getSobject() generating error when pulling field value from parent record

We have a custom object Physician__c with a lookup to this object on the Account object. The relationship is 1:many, the field name on the Account object is Primary_Care_Physician__c, and the field is not required.

I'm working on a trigger on the Account object and need to reference some fields on the parent Physician__c object. I figure that the best approach is to use the getSobject() feature.

I'm attempting to reference the field Fax__c on the parent object but am getting the error Invalid relationship Physician__c for Account: Trigger.zUpdateFaxInfo_Account.

 As best I can tell my syntax is correct, but obviously something is not right. Help?
trigger zUpdateFaxInfo_Account on Account (after update) {

    for (Account acct : Trigger.new) {
    
        Account oldacct = Trigger.oldMap.get(acct.ID);
 
        String FaxNumber = (String) acct.getSobject('Physician__c').get('Fax__c');
 
//rest of code will go into here

        
        }

}



Best Answer chosen by AndrewNerney
James LoghryJames Loghry
Not sure why you're using the getSObject and get(String str) methods, there's probably a good use case for it, but another, safer approach is to do the following:

String faxNumber = (String) acct.Physician__r.Fax__c;


All Answers

pconpcon
You'll probably need to do the relationship name which is probably Physician__r that name may be different depending on how you configured it.
AndrewNerneyAndrewNerney
That's possible, though I could swear that I tried that already. I'll try it again when I get home.
James LoghryJames Loghry
Not sure why you're using the getSObject and get(String str) methods, there's probably a good use case for it, but another, safer approach is to do the following:

String faxNumber = (String) acct.Physician__r.Fax__c;


This was selected as the best answer
AndrewNerneyAndrewNerney
Simple reason: I'm basically an Apex noobie since I haven't touched it in years, and only touched it sporadically way back in the day.

James, I think that does it. I just gave it a quick debug test and it looks to do what I need. I'm marking this as the best answer and will work it into my trigger tomorrow.