+ Start a Discussion
farah sheriffarah sherif 

need help with queueable apex

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
Raj VakatiRaj Vakati
Use this code
 
public class AddPrimaryContact implements queueable{
   public Contact c;
    public string state;
    public AddPrimaryContact( Contact c,String State){
        this.c = c;
        this.state = state;
    }
    public void execute(QueueableContext context){
        List<Account>acc = [select id ,Name ,(select id ,FirstName,LastName from Contacts) From Account where
                             BillingState = :state LIMIT 200];
        List <Contact>conlst = new List<Contact>();
        for(Account account :acc){
            
            Contact cont = c.clone(false,false,false,false);
            cont.AccountId = account.Id;
            conlst.add(cont);
        }
        insert conlst;
    }
}

test class
 
@isTest
public class AddPrimaryContactTest {
    @isTest static void testMethod1() {
        // setup
        List<Account> testAcctList = new List<Account>();
        for (Integer i = 0; i < 50; i++) {
            testAcctList.add(new Account(BillingState = 'OR', name = 'TestAccount' + i));
        }
        for (Integer j = 0; j < 50; j++) {
            testAcctList.add(new Account(BillingState = 'WA', name = 'TestAccount' + j));
		}
        insert testAcctList;
        
        Contact c = new Contact(FirstName='Test', LastName='test');
        String state = 'OR';
        AddPrimaryContact apc = new AddPrimaryContact(c, state);
        
        // execution
        Test.startTest();
        	System.enqueueJob(apc);
        Test.stopTest();
        
        // result
        System.assertEquals(50, [SELECT count() FROM Contact WHERE accountId IN (SELECT Id FROM Account WHERE BillingState = :state)]);
    }
}

​​​​​​​

All Answers

Raj VakatiRaj Vakati
Use this code
 
public class AddPrimaryContact implements queueable{
   public Contact c;
    public string state;
    public AddPrimaryContact( Contact c,String State){
        this.c = c;
        this.state = state;
    }
    public void execute(QueueableContext context){
        List<Account>acc = [select id ,Name ,(select id ,FirstName,LastName from Contacts) From Account where
                             BillingState = :state LIMIT 200];
        List <Contact>conlst = new List<Contact>();
        for(Account account :acc){
            
            Contact cont = c.clone(false,false,false,false);
            cont.AccountId = account.Id;
            conlst.add(cont);
        }
        insert conlst;
    }
}

test class
 
@isTest
public class AddPrimaryContactTest {
    @isTest static void testMethod1() {
        // setup
        List<Account> testAcctList = new List<Account>();
        for (Integer i = 0; i < 50; i++) {
            testAcctList.add(new Account(BillingState = 'OR', name = 'TestAccount' + i));
        }
        for (Integer j = 0; j < 50; j++) {
            testAcctList.add(new Account(BillingState = 'WA', name = 'TestAccount' + j));
		}
        insert testAcctList;
        
        Contact c = new Contact(FirstName='Test', LastName='test');
        String state = 'OR';
        AddPrimaryContact apc = new AddPrimaryContact(c, state);
        
        // execution
        Test.startTest();
        	System.enqueueJob(apc);
        Test.stopTest();
        
        // result
        System.assertEquals(50, [SELECT count() FROM Contact WHERE accountId IN (SELECT Id FROM Account WHERE BillingState = :state)]);
    }
}

​​​​​​​
This was selected as the best answer
farah sheriffarah sherif
the test class has no code coverage 
Raj VakatiRaj Vakati
Can u check the test class is running successfully or not . if any validation rules are there deactive them and try
farah sheriffarah sherif
ok thanks it worked but I wanted to ask you what does the clone method copy from exactly?
Deepali KulshresthaDeepali Kulshrestha
Hi Farah,

Please refer to these links to solve queueable apex trailhead challenge:
http://theblogreaders.com/create-queueable-apex-class-inserts-contacts-accounts-queueable-apex/
http://sfdccodepractices.blogspot.com/2017/07/create-queueable-apex-class-that.html
https://gist.github.com/mannharleen/8c2df70927777d1d5a27b64dd4d4a61b
https://www.youtube.com/watch?v=m1pWT8f22bY

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha