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
Ramana123Ramana123 

what to write in anonymous block for this code to execute..? i am not getting please helpm me in this

code 
public class AddPrimaryContact implements Queueable {
    
    private Contact s;
    private String State;
    
    public AddPrimaryContact(Contact s, String State) {
        this.s = s;
        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> con = new List<Contact>();
          for(Account acc: ListAccount)
          {
          
          Contact cont = s.clone(false,false,false,false);

               cont.AccountId =  acc.id;

                 con.add( cont );
          
          }
        insert con;
   
    }
}







what to wrire here in anonymous block ..?

AddPrimaryContact obi = new AddPrimaryContact(s,usa);

Database.executeBatch(obj);




 
Agustin BAgustin B
Hi, almost there, you need to enqueue the job instead of execute batch.
System.enqueueJob(new AddPrimaryContact(s,usa));

if it helps please mark as correct, it may help others
Ramana123Ramana123
hii , tnx for the help but it's not worki g it's showing ----- Variable does not exist: s
Agustin BAgustin B
yes, you need to also create the contact and the state, for example
System.enqueueJob(new AddPrimaryContact(new Contact(LastName='test'),'usa'));


if it helps please like or mark as correct, it may help others