You need to sign in to do that
Don't have an account?
Apex trigger not updating all records
Hello awesome devs!
Need some help here as I am at a loss why the following Trigger is not setting values on all records but it setting a value on some of the records when doing a mass insert r or update. When you go to a single record and perform an update the trigger always fires correctly.
What is trying to be achieved:
When a record on Object A is created or updated and it includes a SKU in a field called SKU__c .
Then trigger is called on Object A to find a Product from our standard Product2 object that have the same value in the ProductCode field as the SKU__c fiel don Object A.
When found, a field (Product_Line__c) on Object A should be populated with the value form the found Product2 record and its Product_Line__c field.
Again this works when updating records one by one, but anythign in mass, will only update a few records. (Latest example: out of 1097 records in a mass update, only roughly 40 records got updated, the rest have nothign populated in the Product_Line__c field on Object A.
Can anyone look over my code, and help me out? :)
Thank you so much all,
Shawn
Trigger Code:
Need some help here as I am at a loss why the following Trigger is not setting values on all records but it setting a value on some of the records when doing a mass insert r or update. When you go to a single record and perform an update the trigger always fires correctly.
What is trying to be achieved:
When a record on Object A is created or updated and it includes a SKU in a field called SKU__c .
Then trigger is called on Object A to find a Product from our standard Product2 object that have the same value in the ProductCode field as the SKU__c fiel don Object A.
When found, a field (Product_Line__c) on Object A should be populated with the value form the found Product2 record and its Product_Line__c field.
Again this works when updating records one by one, but anythign in mass, will only update a few records. (Latest example: out of 1097 records in a mass update, only roughly 40 records got updated, the rest have nothign populated in the Product_Line__c field on Object A.
Can anyone look over my code, and help me out? :)
Thank you so much all,
Shawn
Trigger Code:
trigger PopulateProductLineOnCharge on Zuora__SubscriptionProductCharge__c (before insert, before update) { List<Zuora__SubscriptionProductCharge__c> spcToUpdate = new List<Zuora__SubscriptionProductCharge__c>(); String pLineText = ''; String SkuText = ''; Product2 P; For(Zuora__SubscriptionProductCharge__c s : Trigger.new){ If(s.Product_Line__c == null && s.SKU__c != null){ SkuText = s.SKU__c; } } If(SkuText != null){ P = [SELECT Id, Product_Line__c, ProductCode FROM Product2 WHERE ProductCode = : SkuText LIMIT 1]; For(Zuora__SubscriptionProductCharge__c s2 : Trigger.New){ If(SkuText.contains(s2.SKU__c)){ s2.Product_Line__c = p.Product_Line__c; spcToUpdate.add(s2); } } } }
Please check once following code, I have not tested code may you will get some syntactical errors.
Hope this helps you!
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
All Answers
Please check once following code, I have not tested code may you will get some syntactical errors.
Hope this helps you!
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com