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
Phuc Nguyen 18Phuc Nguyen 18 

Compile Error: Invalid loop variable type expected SObject

Hello,
Having an issue saving my class.  getting 
Compile Error: Invalid loop variable type expected SObject was Notice__c

I am passing the parameters from another Class and I am trying to update a field on the Notice__c object with teh vatQueVatValue value
If you see any other issues with the batch class please also advise.
public class UpdateNoticeFromAccountQueueableBatch implements Database.Batchable <sObject>, Database.Stateful{
    public List<Notice__c> noticeList = new List<Notice__c>();
    public String vatQueVatValue;
    public UpdateNoticeFromAccountQueueableBatch ( List<Notice__c> records ,String vatValueFromQue ) {
        noticeList = records;
        vatQueVatValue = vatValueFromQue;
        system.debug('noticeList value in batch' + noticeList );
        system.debug('vatQueVatValue value in batch' + vatQueVatValue );
    }
    public List<SObject> start(Database.BatchableContext bc){
        return noticeList;
    }
    public void execute(Database.BatchableContext bc, List<SObject> scope){
         List< notice>  NoticeToUpdateFromAccount = new List<Notice__c>();   
         system.debug('scope value ' + scope);
         system.debug('vatQueVatValue value batch' + vatQueVatValue);
         if(!scope.isEmpty()){
             For ( Notice__c noticess : scope){  ---- Compile Error: Invalid loop variable type expected SObject was notice Notice__c 
               noticess.Any_other_Lessor_Info_VAT_Number__c  = vatQueVatValue;
              system.debug('notices.Any_other_Lessor_Info_VAT_Number__c ' + notices.Any_other_Lessor_Info_VAT_Number__c);
             }
         }   
         Database.update(scope, false);
    }
    public void finish(Database.BatchableContext bc){
    }
}

Cheers,
P
 
Best Answer chosen by Phuc Nguyen 18
mukesh guptamukesh gupta
Hi,

Please use below code:-
 
public class UpdateNoticeFromAccountQueueableBatch implements Database.Batchable <sObject>, Database.Stateful{
    public List<Notice__c> noticeList = new List<Notice__c>();
    public String vatQueVatValue;
    public UpdateNoticeFromAccountQueueableBatch ( List<Notice__c> records ,String vatValueFromQue ) {
        noticeList = records;
        vatQueVatValue = vatValueFromQue;
        system.debug('noticeList value in batch' + noticeList );
        system.debug('vatQueVatValue value in batch' + vatQueVatValue );
    }
    public List<SObject> start(Database.BatchableContext bc){
        return noticeList;
    }
    public void execute(Database.BatchableContext bc, List<Notice__c> scope){
         List< notice>  NoticeToUpdateFromAccount = new List<Notice__c>();   
         system.debug('scope value ' + scope);
         system.debug('vatQueVatValue value batch' + vatQueVatValue);
         if(!scope.isEmpty()){
             For ( Notice__c noticess : scope){  ---- Compile Error: Invalid loop variable type expected SObject was notice Notice__c 
               noticess.Any_other_Lessor_Info_VAT_Number__c  = vatQueVatValue;
              system.debug('notices.Any_other_Lessor_Info_VAT_Number__c ' + notices.Any_other_Lessor_Info_VAT_Number__c);
             }
         }   
         Database.update(scope, false);
    }
    public void finish(Database.BatchableContext bc){
    }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Phuc,

on this line  public void execute(Database.BatchableContext bc, List<SObject> scope){

Try replacing  SObject with Notice__c  

or

you can also try

replacing this line For ( Notice__c noticess : scope){

with for(Notice__c noticess :(List<Notice__c >) scope)

Try either of them.

If it helps mark it as best answer.

Thanks!
 
mukesh guptamukesh gupta
Hi,

Please use below code:-
 
public class UpdateNoticeFromAccountQueueableBatch implements Database.Batchable <sObject>, Database.Stateful{
    public List<Notice__c> noticeList = new List<Notice__c>();
    public String vatQueVatValue;
    public UpdateNoticeFromAccountQueueableBatch ( List<Notice__c> records ,String vatValueFromQue ) {
        noticeList = records;
        vatQueVatValue = vatValueFromQue;
        system.debug('noticeList value in batch' + noticeList );
        system.debug('vatQueVatValue value in batch' + vatQueVatValue );
    }
    public List<SObject> start(Database.BatchableContext bc){
        return noticeList;
    }
    public void execute(Database.BatchableContext bc, List<Notice__c> scope){
         List< notice>  NoticeToUpdateFromAccount = new List<Notice__c>();   
         system.debug('scope value ' + scope);
         system.debug('vatQueVatValue value batch' + vatQueVatValue);
         if(!scope.isEmpty()){
             For ( Notice__c noticess : scope){  ---- Compile Error: Invalid loop variable type expected SObject was notice Notice__c 
               noticess.Any_other_Lessor_Info_VAT_Number__c  = vatQueVatValue;
              system.debug('notices.Any_other_Lessor_Info_VAT_Number__c ' + notices.Any_other_Lessor_Info_VAT_Number__c);
             }
         }   
         Database.update(scope, false);
    }
    public void finish(Database.BatchableContext bc){
    }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
This was selected as the best answer
ravi soniravi soni
Hy Phuc,
try below class and resolve your issue.
public class UpdateNoticeFromNotice__cQueueableBatch implements Database.Batchable <sObject>, Database.Stateful{
    public List<Notice__c> noticeList = new List<Notice__c>();
    public String vatQueVatValue;
    public UpdateNoticeFromNotice__cQueueableBatch ( List<Notice__c> records ,String vatValueFromQue ) {
        noticeList = records;
        vatQueVatValue = vatValueFromQue;
        system.debug('noticeList value in batch' + noticeList );
        system.debug('vatQueVatValue value in batch' + vatQueVatValue );
    }
    public List<SObject> start(Database.BatchableContext bc){
        return noticeList;
    }
    public void execute(Database.BatchableContext bc, List<Notice__c> scope){ 
         List< Notice__c>  NoticeToUpdateFromNotice__c = new List<Notice__c>();   
         system.debug('scope value ' + scope);
         system.debug('vatQueVatValue value batch' + vatQueVatValue);
         if(!scope.isEmpty()){
             For ( Notice__c noticess : scope){  //---- Compile Error: Invalid loop variable type expected SObject was notice Notice__c 
               noticess.Any_other_Lessor_Info_VAT_Number__c    = vatQueVatValue;
              system.debug('notices.Any_other_Lessor_Info_VAT_Number__c ' +  noticess.Any_other_Lessor_Info_VAT_Number__c );
             }
         }   
         Database.update(scope, false);
    }
    public void finish(Database.BatchableContext bc){
    }
}

let me know if it helps you and don't forget to mark it as best answer.
Thank you
Phuc Nguyen 18Phuc Nguyen 18
Thank you all for your time and replies.  I went from top to bottom of the replies and tested.
Abhinav I tried your example but it could not find the Any_other_Lessor_Info_VAT_Number__c field.
mukesh gupta I then tried your changes.  This corrected the error and the update completed as required.
veer soni thank you for your time but since mukesh gupta example worked I stopped there.
Thansk again to the three of you  for the help.
Cheers,
P