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
Kiran PandiyanKiran Pandiyan 

What could be the error here ?

Control Processes with Queueable Apex challenge of Asynchronous Apex module 
I wrote the bellow queueable class
public class AddPrimaryContact implements Queueable{
    public List<Contact> contacts = new List<Contact>();
    List<Account> accs = [Select Id, Name, BillingState from Account];
    public void AddPrimaryContact(List<Account> accs){
    
      for(Account a : accs){
        Contact con = new Contact();
        con.AccountID = a.Id;
        con.LastName = 'Contact of '+a;
        contacts.add(con);
        system.debug('con check'+con);
      }
       insert contacts; 
    }
  public void execute(QueueableContext context){
        for (Contact cont : contacts) {
        cont.MailingState = cont.Account.BillingState;
        }
        update contacts;
    }
}

And then wrote the below test class
@isTest
public class AddPrimaryContactTest {
    
    @testSetup
    static void setup(){
        List<Account> accs = new List<Account>();
        for (Integer j=0; j<2; j++){
            if(j==0){
                for (Integer i=0; i<50; i++){
                    accs.add(new Account( name='TestC Account'+i, BillingState='NY'));
                }
            }
            else{
                 for (Integer i=0; i<50; i++){
                    accs.add(new Account( name='TestD Account'+i, BillingState='CA'));
                }   
                    
             }
            
        }
        insert accs;
    }
    
    static testMethod void testQueueable(){
        List<Account> accts = [Select Id, Name, BillingState from Account where BillingState = 'NY' OR BillingState = 'CA'];
    	AddPrimaryContact addressput = new AddPrimaryContact(accts);
        Test.startTest();
        System.enqueueJob(addressput);
        Test.stopTest();
        
        System.assertEquals(50, [select count() from Contact where MailingState = 'NY']);
        System.assertEquals(50, [select count() from Contact where MailingState = 'CA']);
    }

}

The error I'm getting is "constructor not defined [AddPrimaryContact].(list). And when I run the queueable class in Anonymous execution I don't see SOQL querries running. What could be the issue here ?
Best Answer chosen by Kiran Pandiyan
{!pramod_nishane}{!pramod_nishane}
Hi Kiran,

Here is the code for your challenge :-
1) AddPrimaryContact Class:-

public class AddPrimaryContact implements Queueable {
    public Contact cont;
    public String state;
    public AddPrimaryContact(Contact Con, String state){
        this.cont = con;
        this.state = state;
    }
    public void execute(QueueableContext qc){
        List<Contact> cList = new List<Contact>();
        List<Account> aList = new List<Account>([select id,name,Billingstate from Account where Billingstate = :state limit 200]);
        for(Account acc: aList){
            Contact c = new Contact();
            c = this.cont.clone(false,false,false,false);
            c.AccountId = acc.Id;
            cList.add(c);
        }
        if(cList != null && cList.size() > 0)
            insert cList;
    }
}


1) AddPrimaryContactTest Class:-

@IsTest
public class AddPrimaryContactTest {
    
    @IsTest
    public static void testing() {
        List<account> acc_lst = new List<account>();
        for (Integer i=0; i<50;i++) {
            account a = new account(name=string.valueOf(i),billingstate='NY');
            system.debug('account a = '+a);
            acc_lst.add(a);
        }
        for (Integer i=0; i<50;i++) {
            account a = new account(name=string.valueOf(50+i),billingstate='CA');
            system.debug('account a = '+a);
            acc_lst.add(a);
        }      
        insert acc_lst;
        Test.startTest();
        contact c = new contact(lastname='alex');
        AddPrimaryContact apc = new AddPrimaryContact(c,'CA');
        system.debug('apc = '+apc);
        System.enqueueJob(apc);
        Test.stopTest();
        List<contact> c_lst = new List<contact>([select id from contact]);
        Integer size = c_lst.size();
        system.assertEquals(50, size);
    }

}


Let me know in case of any concerns.

Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com

All Answers

{!pramod_nishane}{!pramod_nishane}
Hi Kiran,

You are getting problem on line no.26 - AddPrimaryContact addressput = new AddPrimaryContact(accts);
here you are allocating memory to 'addressput' instance of AddPrimaryContact class.
Which will assume that you are calling constructor of "AddPrimaryContact" class.
but in your mentioned code above constructor is not defined for "AddPrimaryContact" class.

i have added constructor in your code below hieligited in bold :-

public class AddPrimaryContact implements Queueable{
    public List<Contact> contacts = new List<Contact>();
    List<Account> accs = [Select Id, Name, BillingState from Account];
    //Added this new constructor defination
    public AddPrimaryContact(List<Account> acc){
        accs.addAll(acc); // assigning all incomming account list to accs variable
    }
    //Added this new constructor defination

    public void AddPrimaryContact(List<Account> accs){
    
      for(Account a : accs){
        Contact con = new Contact();
        con.AccountID = a.Id;
        con.LastName = 'Contact of '+a;
        contacts.add(con);
        system.debug('con check'+con);
      }
       insert contacts; 
    }
  public void execute(QueueableContext context){
        for (Contact cont : contacts) {
        cont.MailingState = cont.Account.BillingState;
        }
        update contacts;
    }
}

Let me know in case of any concerns.

Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
Kiran PandiyanKiran Pandiyan
@Pramod Nishane 17

 User-added image
Thanks. I did the same as you said and it got saved. The test class also got saved. But the test class didn't cover anything and the challenge could not be completed.
Attaching the error it showed
{!pramod_nishane}{!pramod_nishane}
Hi Kiran,

Here is the code for your challenge :-
1) AddPrimaryContact Class:-

public class AddPrimaryContact implements Queueable {
    public Contact cont;
    public String state;
    public AddPrimaryContact(Contact Con, String state){
        this.cont = con;
        this.state = state;
    }
    public void execute(QueueableContext qc){
        List<Contact> cList = new List<Contact>();
        List<Account> aList = new List<Account>([select id,name,Billingstate from Account where Billingstate = :state limit 200]);
        for(Account acc: aList){
            Contact c = new Contact();
            c = this.cont.clone(false,false,false,false);
            c.AccountId = acc.Id;
            cList.add(c);
        }
        if(cList != null && cList.size() > 0)
            insert cList;
    }
}


1) AddPrimaryContactTest Class:-

@IsTest
public class AddPrimaryContactTest {
    
    @IsTest
    public static void testing() {
        List<account> acc_lst = new List<account>();
        for (Integer i=0; i<50;i++) {
            account a = new account(name=string.valueOf(i),billingstate='NY');
            system.debug('account a = '+a);
            acc_lst.add(a);
        }
        for (Integer i=0; i<50;i++) {
            account a = new account(name=string.valueOf(50+i),billingstate='CA');
            system.debug('account a = '+a);
            acc_lst.add(a);
        }      
        insert acc_lst;
        Test.startTest();
        contact c = new contact(lastname='alex');
        AddPrimaryContact apc = new AddPrimaryContact(c,'CA');
        system.debug('apc = '+apc);
        System.enqueueJob(apc);
        Test.stopTest();
        List<contact> c_lst = new List<contact>([select id from contact]);
        Integer size = c_lst.size();
        system.assertEquals(50, size);
    }

}


Let me know in case of any concerns.

Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
This was selected as the best answer
Kiran PandiyanKiran Pandiyan
@Pramod
The test class is not covering the queueable class 
{!pramod_nishane}{!pramod_nishane}
Hi Kiran,

If you are using code that i shared in above comment then it should cover everything.
For me test class covering 100% code coverage.
can you post your queueable class and test class again.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
Kiran PandiyanKiran Pandiyan
The code I used 
public class AddPrimaryContact implements Queueable {
    public Contact cont;
    public String state;
    public AddPrimaryContact(Contact Con, String state){
        this.cont = con;
        this.state = state;
    }
    public void execute(QueueableContext qc){
        List<Contact> cList = new List<Contact>();
        List<Account> aList = new List<Account>([select id,name,Billingstate from Account where Billingstate = :state limit 200]);
        for(Account acc: aList){
            Contact c = new Contact();
            c = this.cont.clone(false,false,false,false);
            c.AccountId = acc.Id;
            cList.add(c);
        }
        if(cList != null && cList.size() > 0)
            insert cList;
    }
}

The test class I used .
@IsTest
public class AddPrimaryContactTest {
    
    @IsTest
    public static void testing() {
        List<account> acc_lst = new List<account>();
        for (Integer i=0; i<50;i++) {
            account a = new account(name=string.valueOf(i),billingstate='NY');
            system.debug('account a = '+a);
            acc_lst.add(a);
        }
        for (Integer i=0; i<50;i++) {
            account a = new account(name=string.valueOf(50+i),billingstate='CA');
            system.debug('account a = '+a);
            acc_lst.add(a);
        }      
        insert acc_lst;
        Test.startTest();
        contact c = new contact(lastname='alex');
        AddPrimaryContact apc = new AddPrimaryContact(c,'CA');
        system.debug('apc = '+apc);
        System.enqueueJob(apc);
        Test.stopTest();
        List<contact> c_lst = new List<contact>([select id from contact]);
        Integer size = c_lst.size();
        system.assertEquals(50, size);
    }

}

And it is failing to run now too.​
{!pramod_nishane}{!pramod_nishane}
Hi Kiran,

I took your code from above comment and tried running in my org.
I am able to run test class with 100% code coverage.
Please find details below:-

User-added image

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
Kiran PandiyanKiran Pandiyan
Hi Pramod,

it worked in my Playground. But did not work in Developer Org. Will check why It didn't work later.

Thanks for your help :)