You need to sign in to do that
Don't have an account?
Luke Higgins 23
How to get List of Contacts from querying a custom object
I am querying timesheets__c and looking to update the contact associated to the timesheet (timecardApprover__c) that is held in the parent object placement__c. I am able to get a list of ids of the contact I need to update but I can't figure out how to to update the field current_timesheet_status__c associated with the contact.
public with sharing class contFlag { public contFlag() { List<String> timecardApproverIds = new List<String>(); Set<Id> setofIds = new Set<Id>(); for(jstcl__TG_Timesheet__c ts : [SELECT Placement__r.TimecardApprover__c FROM Timesheet__c WHERE Placement__r.Status__c = 'Active' AND Status__c = 'Pending']){ timecardApproverIds.add(String.valueOf(ts)); } for(String a : timecardApproverIds){ setofIds.add(Id.valueOf(a)); } for(Id c : setofIds){ c.Current_Timesheet_Status__c = 'Pending'; // returns error of variable does not exist due to it not being for the contact object } } }
Try the below changes in your class:
public with sharing class contFlag {
public contFlag() {
List<String> timecardApproverIds = new List<String>();
Set<Id> setofIds = new Set<Id>();
List<Contact> conListtoUpdate = new List<Contact>();
for(jstcl__TG_Timesheet__c ts : [SELECT Placement__r.TimecardApprover__c FROM Timesheet__c WHERE Placement__r.Status__c = 'Active' AND Status__c = 'Pending']){
timecardApproverIds.add(String.valueOf(ts));
}
for(String a : timecardApproverIds){
setofIds.add(Id.valueOf(a));
}
for(Contact c : [Select Id from Contact where ID IN: setofIds]){
c.Current_Timesheet_Status__c = 'Pending';
conListtoUpdate.add(c);
}
if(!conListtoUpdate.IsEmpty())
update conListtoUpdate;
}
}
Thanks,
Maharajan,C
Do you happen to know if this is the proper way to make this a batch class? I keep getting the error "invalid id: timesheet__c" when I run it through a scheduled class in the execute anonymous window.