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
Luke Higgins 23Luke Higgins 23 

Updating a field on a custom object

Hello,

I am trying to update a custom number field on a custom object (placement) from a list of a seperate custom objects (timesheets). The following returns no errors, however when I run this through an anonymous window, it doesn't properly update the TCEndDateAudit__c on the Placement object (it turns up null). 

Here is my code so far: 
public class CamstimecardEndDateAudit{
public void auditTimeCards(){
    List<jstcl__TG_Timesheet__c> myList = new List<jstcl__TG_Timesheet__c>();

     myList = [SELECT jstcl__Placement__r.Name, Period__c, jstcl__Placement__r.TCEndDateAudit__c
                            FROM jstcl__TG_Timesheet__c 
                            WHERE jstcl__Placement__r.ts2__Status__c IN ('Active') 
                             AND jstcl__Week_Ending__c = LAST_N_DAYS:15
                             AND jstcl__Status__c = 'Pending' Limit 199];
    
    for(integer i=0 ; i < myList.size(); i++) {
        IF(myList[i].Period__c == 'Weekly Split'){
    myList[i].jstcl__Placement__r.TCEndDateAudit__c = myList[i].jstcl__Placement__r.TCEndDateAudit__c+ 0.5;}
       else{
    myList[i].jstcl__Placement__r.TCEndDateAudit__c = myList[i].jstcl__Placement__r.TCEndDateAudit__c+ 1;
    }
    }
    
  update myList;
}}

 
Best Answer chosen by Luke Higgins 23
jigarshahjigarshah
Luke,

It might we worthwhile to check a couple of things to verify the reason for the query not retrieving the desired result set.
  1. Check if the records satisfying the stated Where Clause in your SOQL actually exist on your org.
  2. Run the query through the SOQL Editor in Dev Console or using Workbench to check the results returned and verify if they are the expected ones.
  3. Check if the Users have access to those records by running the same Soql query through multiple User accounts to verify the results returned.
Also, I am curious to know if you have this class created in your Salesforce org and are just calling it through the dev console or do you have the above script written within the Execute Anonymous window of the Dev Console? If you already have the class written under Apex Classes within your org it might be worthwhile to call the same using the below code rather than rewriting it.
new CamstimecardEndDateAudit().auditTimeCards();

Also try updating the Soql query to use an '=' operator with Active status rather than using the IN operator as follows.
SELECT jstcl__Placement__r.Name, Period__c, jstcl__Placement__r.TCEndDateAudit__c
FROM jstcl__TG_Timesheet__c 
WHERE 
 jstcl__Placement__r.ts2__Status__c = 'Active' 
AND jstcl__Week_Ending__c = LAST_N_DAYS:15
AND jstcl__Status__c = 'Pending' 
Limit 199

All Answers

jigarshahjigarshah
Luke,
  • One thing to note is that when you run your Apex code through an Execute Anonymous window of the Developer Console, the code executes in a User Mode. This means that the running mode will account and respect the sharing settings of the respective User account through which the code is being run.
  • This means that it is quite possible that although the Apex Code above is trying to SOQL query over 199 records as per your Limit clause it is quite possible that there are lesser records returned because some of the records may not be shared with User account, through which the query is being executed. In case you intend to ignore the sharing settings and have the code run with elevated privileges or System Mode you need to explciitly specify the without sharing keyword before the class keyword. With this, your Apex code will ignore the sharing settings for the User account during execution and you should be able to retrieve all the records inspite of the User account executing them does not have access to those records. 
  • Also, you can update your code as follows.
public without sharing class CamstimecardEndDateAudit{
	
	public void auditTimeCards(){
		
        //Declare and initialize the Timesheet list in a single statement 
		List<jstcl__TG_Timesheet__c> myList = new List<jstcl__TG_Timesheet__c>(
			[SELECT jstcl__Placement__r.Name, Period__c, jstcl__Placement__r.TCEndDateAudit__c
		     FROM jstcl__TG_Timesheet__c 
			 WHERE jstcl__Placement__r.ts2__Status__c IN ('Active') 
				AND jstcl__Week_Ending__c = LAST_N_DAYS:15
				AND jstcl__Status__c = 'Pending' 
			 Limit 199]
		);

		if(myList.size() > 0){
		
			for(Integer i = 0; i < myList.size(); i++){
		
                //Use ternary operators instead of if-else for better performance
				myList[i].jstcl__Placement__r.TCEndDateAudit__c = 
					(myList[i].Period__c == 'Weekly Split') ?
					myList[i].jstcl__Placement__r.TCEndDateAudit__c + 0.5 :
					myList[i].jstcl__Placement__r.TCEndDateAudit__c + 1;				
			}

		    update myList;
       }//if
	}
}
Luke Higgins 23Luke Higgins 23
Hey jigarshah,

Thanks for the reply! This helped a ton. However when I hit 'Execute' in the  Execute Anonymous Window, it doesn't seem to run the query at all and the records come up unchanged. Am I missing something?
Here's the execution log
jigarshahjigarshah
Luke,

It might we worthwhile to check a couple of things to verify the reason for the query not retrieving the desired result set.
  1. Check if the records satisfying the stated Where Clause in your SOQL actually exist on your org.
  2. Run the query through the SOQL Editor in Dev Console or using Workbench to check the results returned and verify if they are the expected ones.
  3. Check if the Users have access to those records by running the same Soql query through multiple User accounts to verify the results returned.
Also, I am curious to know if you have this class created in your Salesforce org and are just calling it through the dev console or do you have the above script written within the Execute Anonymous window of the Dev Console? If you already have the class written under Apex Classes within your org it might be worthwhile to call the same using the below code rather than rewriting it.
new CamstimecardEndDateAudit().auditTimeCards();

Also try updating the Soql query to use an '=' operator with Active status rather than using the IN operator as follows.
SELECT jstcl__Placement__r.Name, Period__c, jstcl__Placement__r.TCEndDateAudit__c
FROM jstcl__TG_Timesheet__c 
WHERE 
 jstcl__Placement__r.ts2__Status__c = 'Active' 
AND jstcl__Week_Ending__c = LAST_N_DAYS:15
AND jstcl__Status__c = 'Pending' 
Limit 199
This was selected as the best answer