1

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
  • 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, 2019 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, 2019 at 16:47

1 Answer 1

1

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

You must log in to answer this question.

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