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
JJJenkinsJJJenkins 

Trigger returning null as a the string value

I have this trigger:

trigger MultiAssignPO on Assigned_To__c (after insert, after update) {
   set<id> PoIds = new set<id>();
   
   //lets get all of the IDs of the POs we will work with
   for (Assigned_To__c AsToObj :Trigger.new)
       {
           PoIds.add(AsToObj.Purchase_Order__c);
       }
   //now lets get a list of POs we will work with
   List<Purchase_Order__c> relatedPOs = [SELECT id, Assigned_To_Multi__c
                                            FROM Purchase_Order__c
                                            WHERE id in :PoIds]; 
       //iterate over the list of POs                                     
       for(Purchase_order__c p :relatedPOs)
           {
               string name ='';
               
               //iterate over the assigned to
               for(Assigned_To__c a :trigger.new)
                   {
                       //now we check if the assigned to belongs to the PO we are iterating over in the first loop, if it is, add it to the string name
                       if(a.Purchase_Order__c == p.id)
                           {
                               if(a.Assigned_To__r.Full_Name__c !=null)
                                   {
                                       name += a.Assigned_To__r.Full_Name__c + ' ; ';
                                   }    
                           }
                   }
                //set the value of the multi-assigned in the PO   
                p.Assigned_To_Multi__c = name;
           }
           
           //make a DML statement to update the POs
           update relatedPOs;

}

 But when it does the update it is inserting "null" as the value - it isn't pulling the name value from the user lookup field called Assigned_To__c.

 

Thoughts?

SidharthSidharth

For best results always follow this:

 

1. use debug statements inside every for/if loop, to make sure if your actions is actually entering the code or not.

ex. System.debug('Entered FOR loop 1'); 

      System.debug('Entered FOR loop 2'); etc

 

2. If its entering the loop but not producing correct results, than debug the values at every point, to see which code line is producing the unexpected values. And change it and debug again.

ex. System.debug('purchase order Set Values ' + PoIds); 

      System.debug(a.Assigned_To__r.Full_Name__c); etc

 

3. create a Test class, create sample data, do insert/update/delete and use System.assert to make sure your code is 100% covered and producing correct results.