+ Start a Discussion
Robert_forcedRobert_forced 

Trigger to update related object's fields on custom object

 

trigger RelatedItems on Order__c (after insert, after update) {
Set<ID> OrderIDs = new Set<ID>();
for(Order__c o: Trigger.new)
    OrderIDs.add(o.id);
List<OrderLine__c> ProductsToUpdate = new List<OrderLine__c>([SELECT ID,text__c, Order__c FROM OrderLine__c WHERE OrderLine__c.Order__c = : OrderIDs]);
for(OrderLine__c ol: ProductsToUpdate)
{ol.text__c = 'WHOOOA';}
update ProductsToUpdate;
}

 This is the code for the Trigger, that I'm getting the error: 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RelatedItems: maximum trigger depth exceeded 

I would like to update the text__c field (first with with 'Whooa') of related object  of a custom object named Order__c. The Related object is in a master-detail relationship, and named: OrderLine__c

The lookup field on the OrderLine__c named: Order__c

Thanks for the help!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma

is there any workflw for the OrderLine__c or Order__c, which is doing any field update ?

All Answers

Bhawani SharmaBhawani Sharma

Do you have any trigger on OrderLine__c object ?

Robert_forcedRobert_forced

No, I dont have any Triggers on Orderline__c object. (I know, what you mean, infinite loop)

Bhawani SharmaBhawani Sharma

You can use the before insert and before update to get rid from this issue.

Robert_forcedRobert_forced

Unfortunatly it didn't work out, I tried it before:

The error code is:

 

SELF_REFERENCE_FROM_TRIGGER, Object (id = a01A0000006FMRI) is currently in trigger RelatedItems, therefore it cannot recursively update itself: 

Bhawani SharmaBhawani Sharma

is there any workflw for the OrderLine__c or Order__c, which is doing any field update ?

This was selected as the best answer
Robert_forcedRobert_forced

That was it! Well done, thank you very much!!