I have a custom object which is having a relationship with the same object. I need to update the first child record of the custom object record.

enter image description here

As shown above, the Broker object's record is having self-relationship and having 2 records.

I have to update the first created child record's IsFisrt__c field. I am not sure how to update the same record using a trigger.

Can anyone please help with trigger how to update self-relation object.

  • 2
    There's no particular uniqueness about a trigger on an object with a self-relationship, but you will have to be careful with your logic. What have you tried so far? Where are you stuck? – David Reed Jul 7 '19 at 15:40
  • Hi David, I stuck at self-relation object query. How to get child records of own object?. I know how to write a trigger when there is lookup with other objects. we can query like List<Child_Object__c> l_co = [SELECT Id, Amount__c FROM Child_Object__c WHERE Parent_Lookup__c = :po.Id]; when there is a relationship with other objects. But how to query to get child records of the same same object record? – Frank Jul 7 '19 at 16:47

Self-lookup relationships can be queried in exactly the same way as any other Salesforce relationship.

Given a custom object My_Object__c with a self-lookup field Parent__c that has the relationship name Children, you can query child-to-parent:

SELECT Id, Name FROM My_Object__c WHERE Parent__c = :someParentRecordId

or parent-to-child:

SELECT Id, Name, (SELECT Id, Name FROM Children__r) FROM My_Object__c
| improve this answer | |

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.