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
Christina TomaselliChristina Tomaselli 

Variable does not exist: ID - Contact Object

I wrote an apex class to update a series of fields with the results from the SOQL query in the class. The fields I am updating are on the contact object and I am trying to match using contact ID, but am getting an error that variable ID does not exist. Any suggestions? Here is my code:
public class AttributionRollup {
    @InvocableVariable (required=true)
    public List<SObject> distinctprovider;
 
    @InvocableMethod(label='Attribution Rollup' description='upserts attribution totals into Contact records.' category='Contract Practice Junction')
    public static list<contact> attributiontoupsert(List<String> contact){
        //populate the distinctprovider list with the most recent attribution records 
               List<SObject> distinctprovider = [SELECT Contact__c, Health_Plan_Contract__c, Run_As_Of_Date__c, Max(Attributed_Lives__c)  
                                                 FROM Contract_Practice_Junction__c 
                                                 WHERE Contact__c IN:contact 
                                                 GROUP BY Run_As_Of_Date__c, Contact__c, Health_Plan_Contract__c
                                                 ORDER BY Run_As_Of_Date__c DESC
                                                 LIMIT 1];
        AggregateResult[] groupedResults = distinctprovider;

         List<Contact> ContactPlanList = new List<Contact> ();
         for (AggregateResult ar : groupedResults)  {
        
            Contact c = new Contact();
            
             Switch on String.valueOf(ar.get('Health_Plan_Contract__c')){
                 
                 when 'aetna comm ID'{
                     c.Aetna_Commercial__c =(Double)ar.get('expr0');
                     System.debug('Aetna_Commercial__c' + ar.get('expr0'));
                 }
                 
                 when 'Affinity Medicaid ID'{
                     c.Affinity_Medicaid__c =(Double)ar.get('expr0');
                     System.debug('Affinity_Medicaid__c' + ar.get('expr0'));
                 }
                 
                 when 'Emblem comm ID'{
                     c.Emblem_Commercial__c =(Double)ar.get('expr0');
                     System.debug('Emblem_Commercial__c' + ar.get('expr0'));
                 }
                 
                 when 'Emblem Medicaid ID'{
                     c.Emblem_Medicaid__c =(Double)ar.get('expr0');
                     System.debug('Emblem_Medicaid__c' + ar.get('expr0'));
                 }
                                  
                 when 'Empire comm ID'{
                     c.Empire_Commercial__c =(Double)ar.get('expr0');
                     System.debug('Empire_Commercial__c' + ar.get('expr0'));
                 }
                                  
                 when 'Fidelis Medicaid ID'{
                     c.Fidelis_Medicaid__c =(Double)ar.get('expr0');
                     System.debug('Fidelis_Medicaid__c' + ar.get('expr0'));
                 }
                                  
                 when 'Healthfirst Medicaid ID'{
                     c.HEALTHFIRST_Medicaid__c =(Double)ar.get('expr0');
                     System.debug('HEALTHFIRST_Medicaid__c' + ar.get('expr0'));
                 }
                 
                 when 'Healthfirst Medicare ID'{
                     c.HEALTHFIRST_Medicare__c =(Double)ar.get('expr0');
                     System.debug('HEALTHFIRST_Medicare__c' + ar.get('expr0'));
                 }
                 
                 when 'OSCAR Medicare ID'{
                     c.OSCAR_Medicare__c =(Double)ar.get('expr0');
                     System.debug('OSCAR_Medicare__c' + ar.get('expr0'));
                 }
                 
                 when 'Wellcare Medicare ID'{
                     c.Wellcare_Medicare__c =(Double)ar.get('expr0');
                     System.debug('Wellcare_Medicare__c' + ar.get('expr0'));
                 }
                 
             }
                        
            //c.Run_Date__c =Date.valueOf(ar.get('Run_As_Of_Date__c'));
            //System.debug('Run_Date__c' + ar.get('Run_As_Of_Date__c'));
            
            ContactPlanList.add(c);
        }        
     
        Schema.SObjectField f = Contact.ID;
        
        Database.UpsertResult[] results = database.upsert(ContactPlanList,f,false);
        
        for (Database.UpsertResult result : results) {
            if (!result.isSuccess()) {
                Database.Error[] errs = result.getErrors();
                for(Database.Error err : errs)
                    System.debug(err.getStatusCode() + ' - ' + err.getMessage());
            }
        }
       System.debug('distinctprovider'+distinctprovider) ;       
        return ContactPlanList;
    }
}
Best Answer chosen by Christina Tomaselli
David Zhu 🔥David Zhu 🔥
It is because Contact is also the parameter of the method attributiontoupsert.

public static list<contact> attributiontoupsert(List<String> contact){

At the line in question, it uses the List<String> data type instead of the standard object.
I would suggest two changes:

1. Change the parameter name of the method to Contacts
public static list<contact> attributiontoupsert(List<String> contacts){
And change the places reference the parameter accoridngly.

2.Use Schema.SObjectField f = Contact.Fields.ID;

All Answers

vishal-negandhivishal-negandhi

Hello Christina, 

Can you try changing the statement to 
 

Schema.SObjectField f = Contact.Fields.ID;
Does that make any difference?
Christina TomaselliChristina Tomaselli
I tried that and still get the same error variable does not exist: fields
David Zhu 🔥David Zhu 🔥
It is because Contact is also the parameter of the method attributiontoupsert.

public static list<contact> attributiontoupsert(List<String> contact){

At the line in question, it uses the List<String> data type instead of the standard object.
I would suggest two changes:

1. Change the parameter name of the method to Contacts
public static list<contact> attributiontoupsert(List<String> contacts){
And change the places reference the parameter accoridngly.

2.Use Schema.SObjectField f = Contact.Fields.ID;
This was selected as the best answer
Christina TomaselliChristina Tomaselli
Thanks that worked!