You need to sign in to do that
Don't have an account?
trigger update objects
I have a custom field that will store the order of a related list off of the Opportunity Object. So if item gets udpated the trigger should run through and adjust the order. I am running into recursion errors. I have googled for a while and haven't found a pattern that will work for me. Any suggestion?
trigger updateTenantSortOrder on Tenant__c (before insert, before update, after delete) {
Set<Id> opportunityIds = new Set<Id>();
Integer order=1;
for (Tenant__c tenant : Trigger.new) {
// get opportunity id to be used as a key to get entire list.
opportunityIds.add(tenant.Opportunity__c);
}
// Order list appropriately
List<Tenant__c> tenants =
[select id,Name,TenantBySqFtSortOder__c from Tenant__c where Opportunity__c IN :opportunityIds order by Tenant_Square_Foot__c DESC LIMIT 10];
// Update sort order field
for (Tenant__c t : tenants) {
t.TenantBySqFtSortOder__c =order++;
}
// update objects
update tenants;
}
Here you are trying to using a BEFORE trigger, and in your trigger, you are trying to do a DML call on records that are in the trigger. In BEFORE triggers, any manipulation you do to the data happens before the records undergo their DML call, so you don't need to perform a DML call on the trigger records -- it's already gonna happen. However, you would need to do a DML call on AFTER triggers. They happen after the trigger, so anything you do AFTER the trigger needs to be "saved" via a DML call.
To note, I usually use BEFORE triggers if I need to alter data that is being fed into the trigger while I use AFTER triggers if I need to alter data that is related to the data being fed into the trigger.
Also, use separate Helper class to write the logic and put a static variable to stop the recursiveness there. You can not "update tenants" from Tenant Trigger; which in turn run the Trigger recursively.
To prevent a recursive call, you should make sure your trigger only executes one time. Add a class with a static boolean variable. In the trigger, have a condition that checks the value of the boolean. Once the trigger executes, change the value to false.
Let me know if this helps.
All Answers
Here you are trying to using a BEFORE trigger, and in your trigger, you are trying to do a DML call on records that are in the trigger. In BEFORE triggers, any manipulation you do to the data happens before the records undergo their DML call, so you don't need to perform a DML call on the trigger records -- it's already gonna happen. However, you would need to do a DML call on AFTER triggers. They happen after the trigger, so anything you do AFTER the trigger needs to be "saved" via a DML call.
To note, I usually use BEFORE triggers if I need to alter data that is being fed into the trigger while I use AFTER triggers if I need to alter data that is related to the data being fed into the trigger.
Also, use separate Helper class to write the logic and put a static variable to stop the recursiveness there. You can not "update tenants" from Tenant Trigger; which in turn run the Trigger recursively.
To prevent a recursive call, you should make sure your trigger only executes one time. Add a class with a static boolean variable. In the trigger, have a condition that checks the value of the boolean. Once the trigger executes, change the value to false.
Let me know if this helps.