• Scotty Force
  • NEWBIE
  • 80 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 7
    Replies
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(); 

}

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

 
Want to build a visualforce page which shows table with few records in it. Testing it out in developer org, so no preference which object i use. One of the column should display numbers. Want to see if it's possible to sort those records based on that column. Can you please help me, by giving a small example. Thanks!!

 
I've written a basic trigger, which has recordtypeid as hard coded value, need help switching criteria to recordtype.name. So i can clean up the code. Please help. Thanks!!


trigger AccountCurrencyTrigger on Account (before insert,before update) 
{
   
    for(account acc:trigger.new)
    {
        if(acc.Currency_Test__c < 0 && acc.RecordTypeID == '018f4567348MBvb')
        {
            System.debug('Record Type ID: '+acc.RecordTypeID);
            
            acc.adderror('Sorry, Currency cannot have negative value');
            
        }  
    }
}
Hello, Have two custom objects Obj1 and Obj2, there is a lookup relationship between them. Obj1 has a field 'Quantity'. Looking for help on trigger, to pass quantity values to Obj2 object (Field: Amount). Want to achieve this using Trigger and not process builder. Any help? Thanks!!
I've a junction object Pharma_Product_Invoice__c (M-D relation is on Pharma_Product__c and Invoice__c objects) which has a field called Quantity__c. Now i've added a Quantity__c field on Pharma_Product__c and want the Quantity values to pass from Pharma_Product_Invoice__c to Pharma_Product__c. I've tried using formula, but it doesn't work. Any suggestions?
Have two custom objects Book__c and Order__c. They have a lookup relationship between them. Created a new field 'Quantity' on Book__c. Want to write a trigger to update Quantity value on Order__c. I know this can be done via process builder. Any help?
Have a custom field Weight on Contact object. There is a validation rule which prevents this field from editing after Pet-ID (custom field) is saved. We have an apex controller which lets only Profile1 Team to edit it. However now, we want System Administrators to edit this field as well. If i changes SOQL to SELECT Id from Profile where Name = 'Profile1' or 'System Administrator', end up getting two results. Have to change data type which i do not want. Is there an easier way to do this? 

Initial logic was,
Id setProfileId = [SELECT Id FROM Profile WHERE Name = 'Profile1 Team' LIMIT 1].Id;

this.isSetUser = UserInfo.getProfileId() == setProfileId;
Using Global picklist, to create a custom field on Account and Contact object. So whenever i save a picklist value in Account object, it should show similar value in Contact object. Any suggestions on how do i accomplish this? 

Global Picklist = Subjects Test

Picklist Values = English, French, Japanese
Looking for some help writing visual force page to sign in and sign out of my ORG? It can have custom buttons.
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(); 

}

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

 
I've written a basic trigger, which has recordtypeid as hard coded value, need help switching criteria to recordtype.name. So i can clean up the code. Please help. Thanks!!


trigger AccountCurrencyTrigger on Account (before insert,before update) 
{
   
    for(account acc:trigger.new)
    {
        if(acc.Currency_Test__c < 0 && acc.RecordTypeID == '018f4567348MBvb')
        {
            System.debug('Record Type ID: '+acc.RecordTypeID);
            
            acc.adderror('Sorry, Currency cannot have negative value');
            
        }  
    }
}
Hello, Have two custom objects Obj1 and Obj2, there is a lookup relationship between them. Obj1 has a field 'Quantity'. Looking for help on trigger, to pass quantity values to Obj2 object (Field: Amount). Want to achieve this using Trigger and not process builder. Any help? Thanks!!
I've a junction object Pharma_Product_Invoice__c (M-D relation is on Pharma_Product__c and Invoice__c objects) which has a field called Quantity__c. Now i've added a Quantity__c field on Pharma_Product__c and want the Quantity values to pass from Pharma_Product_Invoice__c to Pharma_Product__c. I've tried using formula, but it doesn't work. Any suggestions?
Have a custom field Weight on Contact object. There is a validation rule which prevents this field from editing after Pet-ID (custom field) is saved. We have an apex controller which lets only Profile1 Team to edit it. However now, we want System Administrators to edit this field as well. If i changes SOQL to SELECT Id from Profile where Name = 'Profile1' or 'System Administrator', end up getting two results. Have to change data type which i do not want. Is there an easier way to do this? 

Initial logic was,
Id setProfileId = [SELECT Id FROM Profile WHERE Name = 'Profile1 Team' LIMIT 1].Id;

this.isSetUser = UserInfo.getProfileId() == setProfileId;
Looking for some help writing visual force page to sign in and sign out of my ORG? It can have custom buttons.