+ Start a Discussion
farah sheriffarah sherif 

Queueable class ans test class help

Create an Apex class called 'AddPrimaryContact' that implements the Queueable interface.
Create a constructor for the class that accepts as its first argument a Contact sObject and a second argument as a string for the State abbreviation.
The execute method must query for a maximum of 200 Accounts with the BillingState specified by the State abbreviation passed into the constructor and insert the Contact sObject record associated to each Account. Look at the sObject clone() method.
Create an Apex test class called 'AddPrimaryContactTest'.
In the test class, insert 50 Account records for BillingState "NY" and 50 Account records for BillingState "CA". Create an instance of the AddPrimaryContact class, enqueue the job and assert that a Contact record was inserted for each of the 50 Accounts with the BillingState of "CA".
Best Answer chosen by farah sherif
abhishek singh 497abhishek singh 497
# Apex class

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> acctlist=[Select id,name,(Select id,firstname,lastname from Contacts) from Account where BillingState =:state limit 200];
        List<Contact> conlist = new List<Contact>();
        for(Account acc :acctlist){
            Contact con = c.clone(false,false,false,false);
            con.AccountId = acc.Id;
            conlist.add(con);
        }
        if(conlist.size()>0){
            insert conlist;
        }
    }

}

# Apex test Class

@isTest
public class AddPrimaryContactTest {
 
     @isTest static void TestList()
     {
         List<Account> Teste = new List <Account>();
         for(Integer i=0;i<50;i++)
         {
             Teste.add(new Account(BillingState = 'CA', name = 'Test'+i));
         }
         for(Integer j=0;j<50;j++)
         {
             Teste.add(new Account(BillingState = 'NY', name = 'Test'+j));
         }
         insert Teste;

         Contact co = new Contact();
         co.FirstName='demo';
         co.LastName ='demo';
         insert co;
         String state = 'CA';
      
          AddPrimaryContact apc = new AddPrimaryContact(co, state);
          Test.startTest();
            System.enqueueJob(apc);
          Test.stopTest();
      }
 }

Please let me know if you have any queries.
Please mark it as the best answer if solves your problem.

Thanks & Regards,
Abhishek Singh.

All Answers

Andrew GAndrew G
So, you would like us to do your Trailhead solution for you?

You would learn more by at least giving it a crack yourself, and then asking the questions to address your direct issues.

Happy coding.
abhishek singh 497abhishek singh 497
# Apex class

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> acctlist=[Select id,name,(Select id,firstname,lastname from Contacts) from Account where BillingState =:state limit 200];
        List<Contact> conlist = new List<Contact>();
        for(Account acc :acctlist){
            Contact con = c.clone(false,false,false,false);
            con.AccountId = acc.Id;
            conlist.add(con);
        }
        if(conlist.size()>0){
            insert conlist;
        }
    }

}

# Apex test Class

@isTest
public class AddPrimaryContactTest {
 
     @isTest static void TestList()
     {
         List<Account> Teste = new List <Account>();
         for(Integer i=0;i<50;i++)
         {
             Teste.add(new Account(BillingState = 'CA', name = 'Test'+i));
         }
         for(Integer j=0;j<50;j++)
         {
             Teste.add(new Account(BillingState = 'NY', name = 'Test'+j));
         }
         insert Teste;

         Contact co = new Contact();
         co.FirstName='demo';
         co.LastName ='demo';
         insert co;
         String state = 'CA';
      
          AddPrimaryContact apc = new AddPrimaryContact(co, state);
          Test.startTest();
            System.enqueueJob(apc);
          Test.stopTest();
      }
 }

Please let me know if you have any queries.
Please mark it as the best answer if solves your problem.

Thanks & Regards,
Abhishek Singh.
This was selected as the best answer
farah sheriffarah sherif
Thanks Abhishek Singh
abhishek singh 497abhishek singh 497
Welcome, Farah!!.