+ Start a Discussion
Cameron SeitzCameron Seitz 

Updating Record Fields - Variable Does Not Exist Error

I currently am working on creating an Apex Class that initiially pulls a specific set of Placements (jstcl__Placement__c) and puts them in a list. 
It then takes that list of Placement Ojects and edits a relevant field called TimecardEnddateAudit__c field. I keep getting errors saying that the field jstcl__Timesheet_Period__c that exists in some of my if logic does not exist. Logically it should edit fields according to the logic:" If the jstcl__Timesheet_Period__c field is equal to 'Weekly Split' then add .5, else add 1. 
public class CamstimecardEndDateAudit{
public static void auditTimeCards(){

List<sObject> placements = [SELECT jstcl__Placement__r.Name
                            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'];
                             
    for(List<sObject> A : placements){

        if(A.jstcl__Timesheet_Period__c = 'Weekly Split'){
        
        TimecardEndDateAudit__c = TimecardEndDateAudit__c + 0.5;}
        
        else{
        
        TimecardEndDateAudit__c = TimecardEndDateAudit__c ++;
}
}
}}

 
GhanshyamChoudhariGhanshyamChoudhari
public class CamstimecardEndDateAudit{
public static void auditTimeCards(){
	list<jstcl__TG_Timesheet__c> jc = new jstcl__TG_Timesheet__c();

List<sObject> placements = [SELECT jstcl__Placement__r.Name, jstcl__Timesheet_Period__c ,TimecardEndDateAudit__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'];
                             
    for(List<sObject> A : placements){

        if(A.jstcl__Timesheet_Period__c = 'Weekly Split'){
        
    jc.Add(A.TimecardEndDateAudit__c + 0.5);
	}
        
        else{
           

        A.TimecardEndDateAudit__c = A.TimecardEndDateAudit__c ++;
		 jc.Add(A.TimecardEndDateAudit__c);
}
}
update jc;
}}

 
Sridhar NarayansaSridhar Narayansa
Your Select statement should is missing the field jstcl__Timesheet_Period__c. Add that under Select and the error will goaway. 
Raj R.Raj R.
When you need to perform conditionals you need to query for the fields that you need. 

Update the SOQL
List<jstcl__Placement__c> placements = [SELECT jstcl__Placement__r.Name, jstcl__Timesheet_Period__c ,TimecardEndDateAudit__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'];
If you still want to use List<sobject> see https://developer.salesforce.com/forums/?id=906F00000008waAIAQ for other ways to query what you need. 
 
Rishabh Agrawal 18Rishabh Agrawal 18
Add jstcl__Timesheet_Period__c  firld on you query, then only you will be able to perform any operation on that field.