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
David SteeleDavid Steele 

Controlling Processes with Queueable Apex

The solution to this trailhead challenge is inside of the reading of the trailhead material, and the challenge references the LeadProcessor class from the Database.Batchable trailhead challenge which does not apply to this challenge.
Amit Chaudhary 8Amit Chaudhary 8
Try below code. I hope that will help you
global class LeadProcessor implements    Database.Batchable<Sobject> 
{
    global Database.QueryLocator start(Database.BatchableContext bc) 
    {
        return Database.getQueryLocator([Select LastName From Lead ]);
    }

    global void execute(Database.BatchableContext bc, List<Lead> scope)
    {
            for (Lead Leads : scope) 
            {
                Leads.LeadSource = 'Dreamforce';
            }
        update scope;
    }    

    global void finish(Database.BatchableContext bc){   }    
}
@isTest 
public class LeadProcessorTest 
{
    static testMethod void testMethod1() 
    {
        List<Lead> lstLead = new List<Lead>();
        for(Integer i=0 ;i <200;i++)
        {
            Lead led = new Lead();
            led.FirstName ='FirstName';
            led.LastName ='LastName'+i;
            led.Company ='demo'+i;
            lstLead.add(led);
        }
        
        insert lstLead;
        
        Test.startTest();

            LeadProcessor obj = new LeadProcessor();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}

Let us know if this will help you

Thanks
Amit Chaudhary

 
Manjunath S 3Manjunath S 3
Can anyone give the solution to the AddPrimaryContact challenge? It talks abouta Clone() method, but I'm not sure how to implement that in the execute method. Thanks in advance!
Manjunath S 3Manjunath S 3
It is for the 'Controlling Processes with Queueable Apex' challenge.
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for solution
1) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DBndIAG
 
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();
      }
 }
Please let us know if this will help you


 
Manjunath S 3Manjunath S 3
@Amit

Used your code. Worked perfectly. Thanks mate!
Amit Chaudhary 8Amit Chaudhary 8
I am glad that's helped you Manjunath ,

Hi David Steele,

Please let us know if above solution will helped you or you need more help

 
James GoreJames Gore

@Amit

Thank you for your help. That definitely got me on the right track.

I don't see any assertions in your test class, though. As per the instructions, we must "assert that a Contact record was inserted for each of the 50 Accounts with the BillingState of 'CA'."

Mihail Iancovoi 7Mihail Iancovoi 7
Working code sample created by me... Hope help to somebody.
 
public class AddPrimaryContact implements Queueable {

    private Contact cont;
    private String state;
    
    public AddPrimaryContact(Contact record, String sta) {
        this.cont = record;
        this.state = sta;
    }
    
    
    public void execute(QueueableContext context) {
        List<Account> accounts = [select id from account where BillingState = :state LIMIT 200];
        for (Account account : accounts) {
            Contact ct = cont.clone();
            ct.AccountId=account.id;
            upsert ct;
        }
    }    
}
 
@IsTest
public class AddPrimaryContactTest {
    @testSetup 
    static void setup() {
        List<Account> accounts = new List<Account>();
        for (Integer i = 0; i < 50; i++) {
            accounts.add(new Account(name='Test Account'+i, BillingState='NY'));
            accounts.add(new Account(name='Test Account'+i+50, BillingState='CA'));
        }
        insert accounts;
    }
    
    static testmethod void testQueueable() {
        
        Contact co = new Contact(FirstName='Bill',LastName='Gates');
        String st= 'CA';
        
        AddPrimaryContact upd = new AddPrimaryContact(co, st);
        
        Test.startTest();        
        System.enqueueJob(upd);
        Test.stopTest();        
        
        System.assertEquals(50, [select count() from account where BillingState = :st]);
    }    
}


 
Jamal Warren 8Jamal Warren 8
Hello everyone. I'm having an issue with this one in trailhead and was wondering if someone could provide assistance. Specifically, the test class is failing
public class AddPrimaryContact implements Queueable {
    
    private Contact con;
    private String ab;
    
    public AddPrimaryContact (Contact cont, String abb){
        this.con= cont;
        this.ab = abb;
    }
    public void execute(QueueableContext qc){
        List<Account> acc = [select id from account where billingstate = :ab limit 200];
        for(Account a : acc){
            Contact cc = con.clone();
            cc.accountId = a.Id;
            insert cc;
        }
        
    
    }

}
@isTest
private class AddPrimaryContactTest{

@testSetup

static void Starting(){
    List<Account> acc = new List<Account>();
    for(Integer i=1; i< 51; i++){
        acc.add(new Account(name='Sample Name' + i, billingstate = 'NY'));
        acc.add(new Account(name='Sample Name' + i, billingstate = 'CA'));
    }
    insert acc;
    }
    static testMethod void testQueueable(){
    
        Contact ct = new Contact(lastname='something');
        String st = 'CA';
        
        AddPrimaryContact apc = new AddPrimaryContact (ct,st);
        
        Test.startTest();
            System.enqueueJob(apc);
        Test.stopTest();
        
        System.assertEquals(50,[select count() from account where billingstate = :st]);
    }
}


 
Jamal Warren 8Jamal Warren 8
Well the test class isn't failing, more that the trailhead check is
Calin Fintzi 13Calin Fintzi 13
Maybe it's just me but I don't see how the query that's been written in all these system.assertEqulas statements confirm that all the 50 accounts with billingstate = CA in fact have contacts that they are associated with???
My understanding is that count() is only counting the accounts what fit the WHERE condition but there's nothing about the number of contacts with one related to one of these accounts?
Josiah Anderson 2Josiah Anderson 2
This is what you need Calin:
 
System.assertEquals(50, [ SELECT count() FROM Contact WHERE AccountId IN (SELECT Id FROM Account WHERE BillingState = 'CA') ]);

 
voleti venkateshvoleti venkatesh
Can someone help. I used same code as amitUser-added image
Prathmesh GadkariPrathmesh Gadkari
Hi,
Please check the following solution. It works!!!!!I passed the challenge with 100% code coverage and Assert statement passed!!!!!!!!


Class: AddPrimaryContact 

public class AddPrimaryContact implements Queueable{
    private Contact c;
    private String state;
    public AddPrimaryContact(Contact contact, String state){
        this.c = contact;
        this.state = state;
    }
    
    public void execute(QueueableContext context){
        List<Account> accounts = [Select ID, Name From Account WHERE BillingState =:state LIMIT 200];
        List<Contact> contacts = new List<Contact>();
        for(Account account: accounts){
            Contact con = c.clone(false, false,false, false);
            con.AccountId = account.Id;
            contacts.add(con);
            
        }
        if(contacts.size()>0){
            insert contacts;
        }
    }

}

Test Class: AddPrimaryContactTest
@isTest
public class AddPrimaryContactTest {
    static testmethod void testQueueable(){
        List<Account> testAccounts = new List<Account>();
        for(Integer i=0; i<50; i++){
            testAccounts.add(new Account(BillingState = 'NY', name = 'QueueTest ' + i));
        }
        for(Integer j=0; j<50; j++){
            testAccounts.add(new Account(BillingState = 'CA', name = 'QueueTest ' + j));
        }
        insert testAccounts;
        Contact con = new Contact(FirstName = 'Queueable', LastName = 'Apex');
        insert con;
        AddPrimaryContact primContact = new AddPrimaryContact(con, 'CA');
        Test.startTest();
        System.enqueueJob(primContact);
        Test.stopTest();
        System.assertEquals(50, [SELECT count() FROM Contact WHERE accountId IN (Select id from Account WHERE BillingState ='CA')]);
    }

}