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
SFDC_BigDogSFDC_BigDog 

Queueable Apex: Error in Bulk Pattern SOQL for loop

public class AddPrimaryContact implements Queueable {
Private Contact conl = new Contact();
Private String sty;
Public List<Contact> coninsert = new List<Contact>();

public AddPrimaryContact(Contact conlist, String str)
{
    this.conl = conlist;
    this.sty= str;
}


public void execute(QueueableContext context)
{
    for(List<Account> acclist: [Select id,name from Account where BillingState =:sty]);
    {
        for(Account accops:acclist)// Error in this statement
        {
            conl.lastname = 'testLast';
            conl.accountid = accops.id;
             coninsert.add(conl);
        }
    }

    if(coninsert.size()>0)
    {
        insert coninsert;
    }
}}


I am trying Queueable Apex challenge in trial head. In the code I am getting an error in the for loop. Correct me if I am wrong in the below code.
Error: Variable does not exist: acclist

 

Thanks for help!

Amit Chaudhary 8Amit Chaudhary 8
NOTE:- You added the ; After your for loop.

Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000DBndIAG
2)

Try to update your code like below
public class AddPrimaryContact implements Queueable
{
    private Contact c;
    private String state;
    public  AddPrimaryContact(Contact c, String state)
    {
        this.c = c;
        this.state = state;
    }
    public void execute(QueueableContext context) 
    {
         List<Account> ListAccount = [SELECT ID, Name ,(Select id,FirstName,LastName from contacts ) FROM ACCOUNT WHERE BillingState = :state LIMIT 200];
         List<Contact> lstContact = new List<Contact>();
         for (Account acc:ListAccount)
         {
                 Contact cont = c.clone(false,false,false,false);
                 cont.AccountId =  acc.id;
                 lstContact.add( cont );
         }
         
         if(lstContact.size() >0 )
         {
             insert lstContact;
         }
             
    }

}
Let us know if this will help you