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
Scotty ForceScotty Force 

Queueable apex trailhead question?

Stuck on Queueable apex trailhead question? Getting error, System.LastException Before Insert or Upsert must not have two identical Class AddPrimaryContact error on test class. Please help. Posting the trailhead module question and my answer.

Create an Queueable Apex class that inserts Contacts for Accounts.
Create a Queueable Apex class that inserts the same Contact for each Account for a specific state. Write unit tests that achieve 100% code coverage for the class.

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".
The unit tests must cover all lines of code included in the AddPrimaryContact class, resulting in 100% code coverage.
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.

--------------------------------------------------------------------------------------------

public class AddPrimaryContact implements Queueable{
private contact con;
private string state;

public AddPrimaryContact(Contact con1, string state1)
{
this.con = con1;
this.state = state1; 
}

public void execute(QueueableContext context)
{
List<Account> accList = [select id, name, (select id, lastname from Contacts) from Account where BillingState =: state limit 200];

List<Contact> conList = new List<Contact>();

for(Account acc:accList)
{
// clone(preserve id, deep clone, random timestamp, preserve autonumber);
Contact c = con.clone(false, false, false, false);
con.accountid = acc.id;
conList.add(con); 
}

insert conList;

}

---------------------------------------------------------------------------------------------

@isTest
public class AddPrimaryContactTest 
{
@isTest static void demo()
{
List<Account> accList = new List<Account>();

for(integer i=0;i<=50;i++)
{
//50 Account records with BillingState = CA
accList.add(new Account(Name='Test'+i, BillingState='CA')); 
}

for(integer j=0;j<=50;j++)
{
//50 Account records with BillingState = NY
accList.add(new Account(Name='Test'+j, BillingState='NY')); 
}

insert accList;

Contact c1 = new Contact(LastName='Test');
insert c1;

string state1 = 'CA';


AddPrimaryContact apc = new AddPrimaryContact(c1, state1);
Test.startTest();
System.enqueueJob(apc);
Test.stopTest(); 

}

----------------------------------------------------------------------------------------------

 
Best Answer chosen by Scotty Force
Raj VakatiRaj Vakati
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;
         }
             
    }

}
 
@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();
      }
 }

 

All Answers

Raj VakatiRaj Vakati
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;
         }
             
    }

}
 
@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();
      }
 }

 
This was selected as the best answer
Scotty ForceScotty Force
Thanks!! The solution works.