function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shawn Reichner 29Shawn Reichner 29 

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:
 
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);
        }
        }
    }
 
}

 
Best Answer chosen by Shawn Reichner 29
v varaprasadv varaprasad
Hi Shawn,

Please check once following code, I have not tested code may you will get some syntactical errors.
 
trigger PopulateProductLineOnCharge on Zuora__SubscriptionProductCharge__c (before insert, before update) {

    
    List<Zuora__SubscriptionProductCharge__c> spcToUpdate = new List<Zuora__SubscriptionProductCharge__c>();
    
    list<Product2> P;
    
    list<string> SkuText = new list<string>();
    For(Zuora__SubscriptionProductCharge__c s : Trigger.new){
        If(s.Product_Line__c == null && s.SKU__c != null){
            SkuText.add(s.SKU__c);
        }
    }
    
    If(SkuText != null){
    P = [SELECT Id, Product_Line__c, ProductCode FROM Product2 WHERE ProductCode in : SkuText];
	}
	
	Map<string,string> proddetails = new Map<string,string>();
	
	for(Product2 pr : p){
	   if(pr.Product_Line__c != null){
	      proddetails.put(pr.ProductCode,pr.Product_Line__c);
	   
	   }
	
	}
	system.debug('==proddetails=='+proddetails);
    
    For(Zuora__SubscriptionProductCharge__c s2 : Trigger.New){
        If(proddetails.containskey(s2.SKU__c)){
            s2.Product_Line__c = proddetails.get(s2.SKU__c);
            
        }
        }
    }

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com


 

All Answers

v varaprasadv varaprasad
Hi Shawn,

Please check once following code, I have not tested code may you will get some syntactical errors.
 
trigger PopulateProductLineOnCharge on Zuora__SubscriptionProductCharge__c (before insert, before update) {

    
    List<Zuora__SubscriptionProductCharge__c> spcToUpdate = new List<Zuora__SubscriptionProductCharge__c>();
    
    list<Product2> P;
    
    list<string> SkuText = new list<string>();
    For(Zuora__SubscriptionProductCharge__c s : Trigger.new){
        If(s.Product_Line__c == null && s.SKU__c != null){
            SkuText.add(s.SKU__c);
        }
    }
    
    If(SkuText != null){
    P = [SELECT Id, Product_Line__c, ProductCode FROM Product2 WHERE ProductCode in : SkuText];
	}
	
	Map<string,string> proddetails = new Map<string,string>();
	
	for(Product2 pr : p){
	   if(pr.Product_Line__c != null){
	      proddetails.put(pr.ProductCode,pr.Product_Line__c);
	   
	   }
	
	}
	system.debug('==proddetails=='+proddetails);
    
    For(Zuora__SubscriptionProductCharge__c s2 : Trigger.New){
        If(proddetails.containskey(s2.SKU__c)){
            s2.Product_Line__c = proddetails.get(s2.SKU__c);
            
        }
        }
    }

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com


 
This was selected as the best answer
SULEMAN ZIASULEMAN ZIA
You need to create a List<String> for s.SKU__c and store it there. As of now you only have one string variable which is inside the first loop named SkuText which always gets overriden with the last item in trigger.new
Shawn Reichner 29Shawn Reichner 29
Thank you very much!  Thsi fixed my issue!