You need to sign in to do that
Don't have an account?

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.
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.
1) https://developer.salesforce.com/forums/?id=906F0000000DBndIAG
Test class like below Sample test class with Assert as well
Let us know if this will help you
I tried the code mentioned above and Apex class works as expected, but while writing test class with Assert I am getting an error message:
System.AssertException: Assertion Failed: Expected: 50, Actual: 0
Any idea what could be the reason behind it.
Regards
Chetan
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;
}
}
}
------------------------------------------------
@isTest
public class AddPrimaryContactTest {
@testSetup
static void setup(){
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;
}
static testMethod void test(){
Contact con = new Contact(FirstName = 'Queueable', LastName = 'Apex');
insert con;
String state = 'CA';
Test.startTest();
AddPrimaryContact primContact = new AddPrimaryContact(con, state);
System.enqueueJob(primContact);
Test.stopTest();
System.assertEquals(50, [SELECT count() FROM Contact WHERE accountId IN (Select id from Account WHERE BillingState =:state)]);
}
}
I will share my code below, it works. Hople it's helpful to someone .
1 ,Create an Apex class called 'AddPrimaryContact' :
public class AddPrimaryContact implements Queueable {
private String st;
private Contact primecontact;
public AddPrimaryContact(Contact aContact, String aState) {
this.st=aState;
this.primecontact = aContact;
}
public void execute(QueueableContext context) {
List<Account> accounts = [select name from account where billingstate=:st LIMIT 200];
List<Contact> contacts = new List<Contact>();
for (Account acc : accounts) {
contact con=primecontact.clone(false,false,false,false);
contacts.add(con);
}
insert contacts;
}
}
2 ,Create an Apex test class called 'AddPrimaryContactTest':
@isTest
public class AddPrimaryContactTest {
@testSetup
static void setup() {
List<Account> accounts = new List<Account>();
// add 50 NY account
for (Integer i = 0; i < 50; i++) {
accounts.add(new Account(Name='NY'+i, billingstate='NY'));
}
// add 50 CA account
for (Integer j = 0; j < 50; j++) {
accounts.add(new Account(Name='CA'+j, billingstate='CA'));
}
insert accounts;
}
static testmethod void testQueueable(){
contact a=new contact(Lastname='mary', Firstname='rose');
Test.startTest();
AddPrimaryContact updater=new AddPrimaryContact(a, 'CA');
System.enqueueJob(updater);
Test.stopTest();
System.assertEquals(50, [SELECT count() FROM Contact WHERE Lastname='mary' AND Firstname='rose']) ;
}
}
Hi Guys,
I share my code, it's working fine. I hope it's helpful to someone.
1) Create an Apex class called AddPrimaryContact :
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;
}
}
}
2 ) Create an Apex test class called AddPrimaryContactTest:
@isTest
public class AddPrimaryContactTest {
static testmethod void TestList(){
List<Account> Teste = new List<Account>();
for(Integer i=0;i<50;i++){
Teste.add(new Account(BillingCountryCode = 'AD', BillingState='CA',Name='Test'+i));
}
for(Integer j=0;j<50;j++){
Teste.add(new Account(BillingCountryCode = 'AD',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();
}
}
The sObject clone() method four arguments are set by default to 'false'. Therefore, there is no need to explicitly specify that when you call that method.
Also, there is no need for having an inner query to get the Contacts for each Account.
Hope this helps!
- M.
Please understand use of this before using it. we do not need this here.
public class AddPrimaryContact implements Queueable {
private Contact contacts;
private String state;
public AddPrimaryContact(Contact newContact, String st) {
contacts = newContact;
state = st;
}
public void execute(QueueableContext qc) {
List<Account> ls = [select id,name,BillingState from Account where BillingState =:state limit 200];
List<Contact> listOfContacts = new List<Contact>();
for(Account acc: ls) {
Contact cont = contacts.clone(false,false,false,false);
cont.accountId = acc.Id;
cont.MailingState= acc.BillingState;
listOfContacts.add(cont);
}
insert listOfContacts;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@isTest
public class AddPrimaryContactTest {
@testSetup
static void setup() {
List<Account> accList = new List<Account>();
for(Integer i=0;i<50;i++) {
accList.add(new Account(Name='Test Acc'+i,BillingState='NY'));
}
for(Integer j=0;j<50;j++) {
accList.add(new Account(Name='Test2 Acc2'+j,BillingState='CA'));
}
insert accList;
Contact con = new Contact(lastName = 'Test Contact');
insert con;
}
static testMethod void test() {
Test.startTest();
Contact con =[SELECT id,name,lastName from Contact where lastName Like '%Test%' limit 1];
String state = 'CA';
AddPrimaryContact apc = new AddPrimaryContact(con,state);
System.enqueueJob(apc);
System.assertEquals(50, [Select count() from Contact where accountid in :[SELECT id from Account where name like '%Test2%']]);
Test.stopTest();
}
}
Hope this helps :) .....happy coding
System.LimitException: Too many SOQL queries: 101
please suggest me testclass.
Thanks in advance.
/* Apex code */
public class AddPrimaryContact implements Queueable {
private Contact con ;
private String state ;
public AddPrimaryContact(Contact con , String state){
this.con = con;
this.state = state;
}
public void execute(QueueableContext context) {
List<Account> lstAcc = [Select id , name , BillingState, (Select id , FirstName , LastName FROM Contacts)
FROM Account WHERE BillingState =:state LIMIT 200];
List<Contact> lstcon = new List<Contact>();
for(Account Acc:lstAcc){
Contact cnt = con.clone(false);
cnt.AccountId = Acc.Id;
lstcon.add(cnt);
}
if(lstcon.size()>0){
insert lstcon ;
}
}
}
/* test class */
@isTest
public class AddPrimaryContactTest {
@isTest
Public static void testContact(){
List<Account> Acc = New List<Account>();
for (integer i = 1; i<=50 ; i++){
Acc.add(new Account (BillingState = 'NY', name='Test'+i ));
}
for (integer j=1 ; j<=50 ; j++){
Acc.add(new Account (BillingState = 'CA', name='Test'+j ));
}
insert Acc ;
contact con = new contact();
con.FirstName = 'Satyam';
con.LastName = 'Pandey';
insert con;
String state = 'CA' ;
AddPrimaryContact AC = New AddPrimaryContact(con , state);
test.startTest();
system.enqueueJob(AC);
test.stopTest();
}
}
public class AddPrimaryContact implements Queueable {
private String st;
private Contact pc;
public AddPrimaryContact(Contact aContact, String aState) {
this.st=aState;
this.pc = aContact;
}
public void execute(QueueableContext context) {
List<Account> accounts = [select name from account where billingstate=:st LIMIT 200];
List<Contact> contacts = new List<Contact>();
for (Account acc : accounts) {
contact con=pc.clone(false,false,false,false);
contacts.add(con);
}
insert contacts;
}
}
and this is my test class:
@isTest
public class AddPrimaryContactTest {
@testSetup
static void setup() {
List<Account> accounts = new List<Account>();
// add 50 NY account
for (Integer i = 0; i < 50; i++) {
accounts.add(new Account(Name='NY'+i, billingstate='NY'));
}
// add 50 CA account
for (Integer j = 0; j < 50; j++) {
accounts.add(new Account(Name='CA'+j, billingstate='CA'));
}
insert accounts;
}
static testmethod void testQueueable(){
contact a=new contact(Lastname='mary', Firstname='rose');
Test.startTest();
AddPrimaryContact updater=new AddPrimaryContact(a, 'CA');
System.enqueueJob(updater);
Test.stopTest();
System.assertEquals(50, [SELECT count() FROM Contact WHERE Lastname='mary' AND Firstname='rose']) ;
}
}