1

I am trying to create a class where I can input the field I want to update as a parameter.

Problem:

I am receiving the following error in my process method. 'Variable does not exist: fieldToUpdate'

This error makes sense, but I am struggling to figure out how to get that specific field on the specific user to update.

    global class SDREfficiencyMetricsBySDRSched implements Schedulable  
{

    global void execute(SchedulableContext sc)  
    {
    List<User> sdrs = [SELECT ID FROM USER WHERE UserRoleId = '00E6A000000UW7gUAG'];

    List<String> lastWeek_queries = new List<String>();


    String contactsWorked_lastWeek  = 'SELECT count(id) results, account.SDR_Owner__c SDR results from contact where LastActivityDate = last_week AND account.RecordTypeid != \'0126A000000ytERQAY\' group by account.SDR_Owner__c';        
    lastWeek_queries.add(contactsWorked_lastWeek);


    String demosSet_lastWeek = 'SELECT count(id) results, SDR_Owner__c SDR from form__c where CreatedDate = last_week and RecordTypeId = \'0126A000000aFrAQAU\' group by SDR_Owner__c ';
    lastWeek_queries.add(demosSet_lastWeek);

    String demosCompleted_lastWeek = 'SELECT count(id) results, SDR_Owner__c SDR from opportunity where (stagename = \'Unqualified Demo Completed\'  OR stagename = \'Qualified Demo Completed\' ) and SDR_Owner__c != null and RecordTypeId = \'0126A000000yudDQAQ\' and CloseDate = last_week GROUP BY SDR_Owner__c';
    lastWeek_queries.add(demosCompleted_lastWeek);

    String qualifiedDemos_lastWeek  = 'select count(id) results, SDR_Owner__c SDR  from opportunity where RecordTypeId = \'0126A0000004Ao6QAE\' and isclosed = false and  SDR_Owner__c != null and CreatedDate = last_week GROUP BY SDR_Owner__c';
    lastWeek_queries.add(qualifiedDemos_lastWeek);

    String closedWon_lastWeek = 'select count(id) results, SDR_Owner__c SDR  from opportunity where RecordTypeId = \'0126A0000004Ao6QAE\' and iswon = true and  SDR_Owner__c != null and CloseDate = last_week GROUP BY SDR_Owner__c';
    lastWeek_queries.add(closedWon_lastWeek);


    // SDREfficiencyLastWeek(lastWeek_queries, salesOps);

}

    public void SDREfficiencyLastWeek(List<String> queries, List<User> sdrs )
{   
    String objectName = 'User';

    // Get the SObject Type
    Schema.SObjectType objectType = Schema.getGlobalDescribe().get(objectName);


    // Get the fields on the object
    Map<String, SObjectField> fieldMap = objectType.getDescribe().fields.getMap();


    List<AggregateResult> contactsWorked_lastWeek = Database.query(queries[0]);
    String fieldName = 'Contacts_Worked_Last_Week__c';
    Schema.SobjectField fieldToUpdate = fieldMap.get(fieldName);
    process(contactsWorked_lastWeek, sdrs, fieldToUpdate);

}


public void process(List<AggregateResult> ars, List<User> sdrs, Schema.sObjectField fieldToUpdate)
{
    for(User sdr: sdrs)
    {
        for(AggregateResult ar : ars )
        {
            if(sdr.ID == (ID)ar.get('SDR'))
            {
                sdr.fieldToUpdate = ((decimal)ar.get('results')).intValue();
            }
        }
    }
}

}

1 Answer 1

1

You need to use the sObject#put method:

sdr.put(fieldToUpdate, ((decimal)ar.get('results')).intValue());
2
  • I am curious how this works? Does the key always have to be of type Schema.SObjectField? I am looking for documentation on this, but am struggling to find anything. Thank you for the quick and accurate responses! Apr 29, 2019 at 14:57
  • @MatthewMetros It can be a sObjectField or a String. Both methods are listed in the documentation I linked in my answer.
    – sfdcfox
    Apr 29, 2019 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .