if(e.No__c==3 && e.Contact__r.Completed__c==true){            

I am working on a apex code: Adding a portion of the code. Here e is my first object and a lookup is given to contact.

e.No gets update through a approval process(counter value) each time it is approved. So if it becomes 3 and a checkbox of the related contact is true then it should be overwritten to false according to my condition.

But for some reasons it is not happening.In the debug logs I am receiving the value false for the varcheck02.

2 Answers 2


It looks like you're doing this in a trigger. Note that parent/child relationships are not loaded in a trigger, so you'll have to query the contact records first:

Map<Id, Contact> contactsById = new Map<Id, Contact>();
for(MyObject__c e: Trigger.new) {
  contactsById.put(e.Contact__c, null);
contactsById.putAll([SELECT Completed__c FROM Contact WHERE Id = :contactsById.keySet()]);
Map<Id, Contact> updatedContacts = new Map<Id, Contact>();
for(MyObject__c e: Trigger.new) {
  Contact relatedContact = contactsById.get(e.Contact__c);
  if(e.No__c == 3 && relatedContact?.Completed__c == true) {
      new Contact(Id=relatedContact.Id, Completed__c=false)
updated updatedContacts.values();

Some additional work may be necessary if you expect errors, but this should get you pretty close.


The Contact record is not updated with DML update. You need to execute

update new Contact(Id = e.Contact__r.Id, Completed__c = false);

This one will work 100%.
I would also try to just run

update e.Contact__r;

but I don't know if this one will work. Try and write if this one works too.

Even if it is in before insert/update trigger you can't modify related's record fields. You can, but they won't be saved because transaction is performed on another record.

  • Yes, the latter works, as a relationship field (e.g. Contact__r) is a full sObject that you can perform DML on.
    – sfdcfox
    Commented Jan 31, 2023 at 14:04

You must log in to answer this question.

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