You need to sign in to do that
Don't have an account?
ag415
How to check values of fields of related records in an 'if' statement in Apex
Hello,
I originally posted this question in the Salesforce Answers Community, but I was told that it would be best to post this here instead.
I am working an Apex trigger that performs an action based on the condition that two fields equal a certain a value (or in this case, DON'T equal a certain value). One of the fields is a custom boolean field on the record that the trigger is firing for - which works fine - but the other is a field on a related record that is linked to that record via a lookup field.
The object the trigger is executing for is the Contacts object. I have an 'if' statement in the trigger that checks the value of the custom "Private__c" boolean field on the contact record as well as another custom boolean field (also called "Private__c") on the account record linked to the contact record via the Account lookup field.
Basically, I want my trigger to check if "Private__c" on the contact record and "Private__c" on the related account record are both false, and then execute some code if they are. Else, execute some different code.
So far, my code looks a little something like this:
I'd appreciate any help I can get on this one.
Thanks!
I originally posted this question in the Salesforce Answers Community, but I was told that it would be best to post this here instead.
I am working an Apex trigger that performs an action based on the condition that two fields equal a certain a value (or in this case, DON'T equal a certain value). One of the fields is a custom boolean field on the record that the trigger is firing for - which works fine - but the other is a field on a related record that is linked to that record via a lookup field.
The object the trigger is executing for is the Contacts object. I have an 'if' statement in the trigger that checks the value of the custom "Private__c" boolean field on the contact record as well as another custom boolean field (also called "Private__c") on the account record linked to the contact record via the Account lookup field.
Basically, I want my trigger to check if "Private__c" on the contact record and "Private__c" on the related account record are both false, and then execute some code if they are. Else, execute some different code.
So far, my code looks a little something like this:
for (Contact c : Trigger.new) { if(! c.Private__c && ! c.Account.Private__c) { "do stuff" } else { "do something else" } }But this doesn't seem to work, even though the developer console doesn't seem to complain about any unknown fields or other errors as I would expect it to if "c.Account.Private__c" wasn't a valid field. Am I missing something here? Is it not possible to reference fields belonging to the related record this way? And if so, what is the correct way to reference them so I can check their values in an 'if' statement?
I'd appreciate any help I can get on this one.
Thanks!
Instead, you will need to requery for Account.Private__c.
The following example will work if your trigger is *not* before insert:
Now, if you are doing this in a before insert trigger, you'll need a little bit more logic, because Trigger.newMap does not exist in this context, nor do the Contact record Ids exist yet. For before insert, your logic may look like:
All Answers
Please check make sure you are using valid field type of for Priveate__c custom field in your case.
Instead, you will need to requery for Account.Private__c.
The following example will work if your trigger is *not* before insert:
Now, if you are doing this in a before insert trigger, you'll need a little bit more logic, because Trigger.newMap does not exist in this context, nor do the Contact record Ids exist yet. For before insert, your logic may look like:
Thanks for the info. I tried your solution (and also Deepak's), but unfortunately those didn't work either. Your solution did give me an idea, however. I tried doing this instead:
This works! I guess I just needed to query the account record separately before the 'if' statement. Thanks for the help