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
SpunkySpunky 

Help with test coverage on trigger please

Basically what I'm trying to do is ensure a few custom object records are created whenever an opportunity is created for a certain group of user.

 

I have with great effort created a trigger which I absolutely have to deploy urgently but I need test coverage on it. I would greatly appreciate any help on it because I don't know where to start. 

 

Can someone please help me get even part of the way there?

 

Thanks so much

 

trigger JobAidsForRDGAD on Opportunity (after insert) {
  
  List<AD_Job_Aid_NeedsAssess__c> needs = new List<AD_Job_Aid_NeedsAssess__c>();
    List<Job_Aid_Retail_AD__c> interviewers = new List<Job_Aid_Retail_AD__c>();
  
    for (Opportunity newopp: Trigger.New) {
        if (newopp.Id!= null) {
            interviewers.add(new Job_Aid_Retail_AD__c(
                        opportunity_name__c = newopp.Id,
                        recordtypeid = '01280000000G7PFAA0',
                        Name = 'Opportunity Assessment'));                            
                        
              interviewers.add           (new Job_Aid_Retail_AD__c(
                        opportunity_name__c = newopp.Id,
                        recordtypeid = '01280000000UFvqAAG',
                        Name = 'Risk Assessment'));
                        
              interviewers.add(new Job_Aid_Retail_AD__c(
                        opportunity_name__c = newopp.Id,
                        recordtypeid = '01280000000UFeoAAG',
                        Name = 'Compelling Event'));
                        
              needs.add(new AD_Job_Aid_NeedsAssess__c(
                        opportunity__c = newopp.Id,
                        Name = 'Solution Needs'));
        }
    }
    insert interviewers;
    insert needs;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Suresh RaghuramSuresh Raghuram

consider the following as code reference, I would like to remind you one more thing you given hardcoded values for the Record Type Id, when you deploy it into QA environment, Production environment,  these values will keep change so better dont hard code the values.

 

While writing the test class basic things to remember is define a class , write a static method and pass the input values which trigger is required and with the instance of the trigger or class call the method.

Most of the times if conditions, for loops, soql quries will cover automatically.

If this answers your question make this as a solution.

@isTest
private class testSAMTotals
{
static testMethod void UpdateTotals ()
{



State_Activity_Management__c st = new State_Activity_Management__c(Name = 'testOhio');
insert st;


State_Activity_Management__c state = [SELECT Id, Name FROM State_Activity_Management__c where Id =:st.Id];
system.debug('statename id ' + state.Id);



List<Product2> prodList = new List<Product2>();

for(Integer i = 0;i<200; i++){
Product2 prod = new Product2(name='test' +i,State_Activity_Management__c=state.Id);
prodList.add(prod);
}
insert prodList;

}
}

All Answers

Suresh RaghuramSuresh Raghuram

consider the following as code reference, I would like to remind you one more thing you given hardcoded values for the Record Type Id, when you deploy it into QA environment, Production environment,  these values will keep change so better dont hard code the values.

 

While writing the test class basic things to remember is define a class , write a static method and pass the input values which trigger is required and with the instance of the trigger or class call the method.

Most of the times if conditions, for loops, soql quries will cover automatically.

If this answers your question make this as a solution.

@isTest
private class testSAMTotals
{
static testMethod void UpdateTotals ()
{



State_Activity_Management__c st = new State_Activity_Management__c(Name = 'testOhio');
insert st;


State_Activity_Management__c state = [SELECT Id, Name FROM State_Activity_Management__c where Id =:st.Id];
system.debug('statename id ' + state.Id);



List<Product2> prodList = new List<Product2>();

for(Integer i = 0;i<200; i++){
Product2 prod = new Product2(name='test' +i,State_Activity_Management__c=state.Id);
prodList.add(prod);
}
insert prodList;

}
}

This was selected as the best answer
SpunkySpunky

Yay! I was able to use your code as a guide. Thank you so much!