1

During a batch apex class, we are copying one field value from one field to another field. Using the following code below we got the error message. How can we resolve this issue?

Steps we have tried to resolve issue but that didn't work:

  1. Set field level security to edit for all profiles
  2. validated api name is correct

Error message:

Invalid Field New_text__c from Lead

Code

// execute anonymous

string fromField = 'Old_text__c';
string toField = 'New_text__c';
string query = 'select id, Old_Text__c, New_Text__c from Lead';
Id batchId = Database.executeBatch(new myBatchClass(fromField, toField, query);

// batch class

global class myBatchClass implements Database.Batchable<sObject>{

   global final String Query;
   global final String fromFieldValue;
   global final String toFieldValue;

   global SearchAndReplace(String fromField, String toField,String qry){

      Query=qry; fromFieldValue=fromField; toFieldValue=toField;
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     for(sobject sObj : scope){
         // this line below is causing the error
         sObj.put(toFieldValue, sObj.get(fromFieldValue)); 
     }
     update scope;
    }

   global void finish(Database.BatchableContext BC){
   }
}
  • Check Field Level Security. – Adrian Larson Jun 25 at 17:13
  • @AdrianLarson I set the fields to be edit for all profiles. When i click set field level security on the field settings page, it has the Visible=true for all profiles. Any other thought? – usernameabc Jun 25 at 17:14
  • Your fromField value is "Old_Text__c" but your select statement uses Old_Id__c. – David Cheng Jun 25 at 19:35
  • @DavidCheng sorry that was a typo. It should say Old_Text__c for both – usernameabc Jun 25 at 19:46
0

To resolve my issue, I had to navigate to the class that was calling the Database.executeBatch(new myBatchClass()); and remove with sharing on the class

| improve this answer | |

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.