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
Eva TannenbaumEva Tannenbaum 

for loop not working

Hello~

I want to update records after an email with csv attachment is recived. The attachement is passed in the function in UpdateAvailableCGUnite class through a trigger. The for loop does not iterate, and I don't know why. Also, 

public class UpdateAvailableCGUnit {

    /*
      public PageReference UpdateCG() {
        PageReference pageRef = new PageReference('/apex/UpdateAvailableCGUnit');
        return null;
    }


    public Blob csvFileBody{get;set;}
    public string csvAsString{get;set;}
    public String[] csvFileLines{get;set;}
    //public String[] notification{get;set;}
    public List<ECS__Supplier_Product__c> updatelist{get;set;}
   
    
    public UpdateAvailableCGUnit(){
        csvFileLines = new String[]{};
        updatelist = New List<ECS__Supplier_Product__c>();
    }
 */   
     
  public static void importCSVFile(Attachment a){
       
      	List<ECS__Supplier_Product__c> updatelist ;
      	List<ECS__Supplier_Product__c> unupdatelist ;
      
     if(a != null){ 	
      	Blob csvFileBody = a.body;
        string csvAsString = csvFileBody.toString();
        

        String[] csvFileLines = csvAsString.split('\n');
      	String[] SKU = new string[]{''};
     	integer count = csvFileLines.size();
      
      
      //Query existing CG SKU, Qty, and price to existingCG list
		List<ECS__Supplier_Product__c> existingCG = [SELECT Name, ECS__Available_Units__c, ECS__Supplier_SKU__c  FROM ECS__Supplier_Product__c];
        ECS__Supplier_Product__c supp = new ECS__Supplier_Product__c(); 
         
         
      //map csv col. and add to updatelist 
        system.debug(count);
      	
      	for(Integer i=1; i<count; i++){
			system.debug(count);
            string[] csvRecordData = csvFileLines[i].split(',');
            supp.ECS__Supplier_SKU__c = csvRecordData[0];
      		supp.ECS__Available_Units__c = Integer.valueOf(csvRecordData[1].trim());
            
            if(existingCG[i].ECS__Supplier_SKU__c == csvRecordData[0]){
                updatelist.add(supp);
            }
            else{
                unupdatelist.add(supp);
            }
             system.debug(unupdatelist);
      		 system.debug(updatelist);
   			
          }
     
      }
    	
      try{
      	update updatelist;
      }
      catch(Exception e){
      	system.debug(e);
      }
	}
}

[45]|i|1
Then the loop is not looping and seems to go back to trigger and recalled the function again.

09:47:11:242 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object
User-added image

 

Tony TannousTony Tannous
Hello ,

Can you try this modify the i , from i=1 to i =0

 for(Integer i=0; i<count; i++){
           system.debug(count);
           string[] csvRecordData = csvFileLines[0].split(',');

....

Regards

 
Eva TannenbaumEva Tannenbaum

Hello Tony! 
Thank you for your idea!

I started i at 1 to skip the header in the csv file. I tried to make i=0 too, and it didn't work...