+ Start a Discussion
Luke Higgins 23Luke 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
        }
    }
}


 
Maharajan CMaharajan C
Hi Luke,

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
Luke Higgins 23Luke Higgins 23
Thanks Maharajan,

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.
public class contFlag implements Database.Batchable<sObject>, Database.Stateful {

      List<String> timecardApproverIds = new List<String>();
        Set<Id> setofIds = new Set<Id>();
        List<Contact> conListtoUpdate = new List<Contact>();

   	public Database.QueryLocator start(Database.BatchableContext bc) {

    for(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));
        }
    return Database.getQueryLocator('Select Id from Contact where ID IN: setofIds');
       }
     public void execute(Database.BatchableContext BC, List<Contact> a){
    for(Contact c : a){
        c.Current_Timesheet_Status__c = 'Pending';
        conListtoUpdate.add(c);
        }
        
    if(!conListtoUpdate.IsEmpty())    
        update conListtoUpdate;
    }
     public void finish(Database.BatchableContext BC){
   }
}