0

I am trying to write an Apex class that could be run on a schedule. The aim is to iterate through objects and identify NULLs on a particular field and populate that field. So far I've written the below code but it is throwing the "Error: Compile Error: Variable does not exist: Auto_ID__c at line 16 column 56"

public class fixPersistentID {


List<String> objs = new List<String>{'ICP__c','Invoice_Number__c'};

        void populate_NULL_PID(){

                for(String s : objs){

                   String query = 'Select Auto_ID__c FROM '+ s +' WHERE Persistent_ID__c = null';

                   List <sObject> sObj = database.query(query);

                       for(sObject x : sObj){

                                x.Persistent_ID__c = x.Auto_ID__c;

                     }

                   update x;                                        
                }

        }

}

Please suggest if this is the right algorithm or do i need to approach this problem differently.

Thanks,

  • 1
    Why is this field not being populated by a trigger, workflow rule, process, or other synchronous automation? – David Reed Jan 16 at 21:23
  • Hi @DavidReed , I am already using Process Builder to populate the field when a record is created but the Process Builder does not trigger when users insert data via Import Wizard and forget to tick the "Trigger workflow rules.." so I am trying to figure a work around. – Avinash Jan 16 at 21:39
1

You will want to use the put() method on the sObject class to set values to generic sObjects. Conversely also use the get() method to retrieve values.

So something like this:

sObject[] sObjList = Database.query('Select CustomField__c From CustomObject__c');

System.debug(sObjList[0].get('CustomField__c'));

sObject sObj = new sObject();
sObj.put('Custom_Field__c',value);

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.