我有一个运行的触发器,我将分享一段影响功能的代码
for(Pos_Item__c posItem:[SELECT Id,InProcess_Inventory_Stock__c FROM Pos_Item__c WHERE Id IN :posItemsWithInprocessInventory.keySet()])
{
if(posItem.InProcess_Inventory_Stock__c==null)
posItem.InProcess_Inventory_Stock__c=0;
posItem.InProcess_Inventory_Stock__c-=posItemsWithInprocessInventory.get(posItem.Id);
posItems.add(posItem);
}
我尝试在 for 循环之外查询
List<Pos_Item__c> posItemsList = [SELECT Id, InProcess_Inventory_Stock__c FROM Pos_Item__c WHERE Id IN :posItemsWithInprocessInventory.keySet()];
for(Pos_Item__c posItem:posItemsList)
{
if(posItem.InProcess_Inventory_Stock__c==null)
posItem.InProcess_Inventory_Stock__c=0;
posItem.InProcess_Inventory_Stock__c-=posItemsWithInprocessInventory.get(posItem.Id);
posItems.add(posItem);
}
但仍然是同样的问题,请帮助我了解如何克服这个问题
如果我评论这段代码,一切都会正常工作。
但这部分非常重要
提前致谢
也尝试使用地图和列表
1. Trigger Recursion: Is it possible that this trigger is firing more than once due to updates to the same records in the same transaction? If so, it might cause undesired changes to the InProcess_Inventory_Stock__c field, especially if the trigger is recursively updating Pos_Item__c records. You can add a recursion prevention mechanism to check if the trigger has already run for a particular record.
2. Order of Execution: Ensure that there are no other triggers, workflows, or processes that might be modifying InProcess_Inventory_Stock__c at the same time. This could cause unintended behavior.