+ Start a Discussion
jkcjkc 

Test Class: Insert always fails

I'm trying to create a test class but it always shows an error when inserting the sample data

@isTest
private class Test_UpdateJobTechnicalResource
{    
    static TestMethod void validate_TechResource() {   
        sked__Job_Allocation__c job = new sked__Job_Allocation__c();
        
        //sked__Job__c Object has a master-detail relationship with Job Allocation
        job.sked__Job__c = 'a0EN0000007ZpD8';
        //this one has a lookup relationship on Job Application
        job.sked__Resource__c='a0LN00000036fnH';

        insert job;
        
        //After Job Insert
        sked__Job__c afterTestJob = [SELECT Id, Technical_Resource_Assigned__c from sked__Job__c where Id = :'a0EN0000007ZpD8'];
        System.assertEquals('Test', afterTestJob.Technical_Resource_Assigned__c);  
    }
}

When I run this it returns an error

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateJobTechnicalResource: execution of AfterInsert
caused by: System.QueryException: List has no rows for assignment to SObject
Trigger.UpdateJobTechnicalResource: line 7, column 1: []
Class.Test_UpdateJobTechnicalResource.validate_TechResource: line 13, column 1

Best Answer chosen by jkc
Abhishek BansalAbhishek Bansal
Hi,

Since the data of your salesforce organisation is not visible by default in your test classes, So either you have to use seeAllData = true or you have to create each and every record in your test class.
Best practices does not suggest to use seeAlldata = true, so must create test data in your test class.
In your case, you can update the code as mentioned below :

@isTest
private class Test_UpdateJobTechnicalResource
{    
    static TestMethod void validate_TechResource() {

        //Create master record
        sked__Job__c testSkedJob = new sked__Job__c(Name = 'Test');//Please enter all required fields
        insert testSkedJob;
        
        sked__Resource__c testSkedRes = new sked__Resource__c(Name = 'Test Sked Resource');//Please enter all required fields
        insert testSkedRes;
        
        sked__Job_Allocation__c job = new sked__Job_Allocation__c();
        
        //sked__Job__c Object has a master-detail relationship with Job Allocation
        job.sked__Job__c = testSkedJob.id;
        
        //this one has a lookup relationship on Job Application
        job.sked__Resource__c = testSkedRes.id;

        insert job;
        
        //After Job Insert
        sked__Job__c afterTestJob = [SELECT Id, Technical_Resource_Assigned__c from sked__Job__c where Id = :testSkedJob.id];
        System.assertEquals('Test', afterTestJob.Technical_Resource_Assigned__c);  
    }
}

Please let me know if you need more help on this.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi,

Since the data of your salesforce organisation is not visible by default in your test classes, So either you have to use seeAllData = true or you have to create each and every record in your test class.
Best practices does not suggest to use seeAlldata = true, so must create test data in your test class.
In your case, you can update the code as mentioned below :

@isTest
private class Test_UpdateJobTechnicalResource
{    
    static TestMethod void validate_TechResource() {

        //Create master record
        sked__Job__c testSkedJob = new sked__Job__c(Name = 'Test');//Please enter all required fields
        insert testSkedJob;
        
        sked__Resource__c testSkedRes = new sked__Resource__c(Name = 'Test Sked Resource');//Please enter all required fields
        insert testSkedRes;
        
        sked__Job_Allocation__c job = new sked__Job_Allocation__c();
        
        //sked__Job__c Object has a master-detail relationship with Job Allocation
        job.sked__Job__c = testSkedJob.id;
        
        //this one has a lookup relationship on Job Application
        job.sked__Resource__c = testSkedRes.id;

        insert job;
        
        //After Job Insert
        sked__Job__c afterTestJob = [SELECT Id, Technical_Resource_Assigned__c from sked__Job__c where Id = :testSkedJob.id];
        System.assertEquals('Test', afterTestJob.Technical_Resource_Assigned__c);  
    }
}

Please let me know if you need more help on this.

Thanks,
Abhishek
This was selected as the best answer
jkcjkc

Hi Abhishek,

Thank you for the reply.

I fill in all the required fields for each object but I'm still gettting the same error. 

Abhishek BansalAbhishek Bansal
Hi,

Something might be wrong with your trigger code.
Can you please post your trigger code here, so that i can have a look into it.

Thanks,
Abhishek.
jkcjkc

Here it is:


trigger UpdateJobTechnicalResource on sked__Job_Allocation__c (after insert) {
    List<sked__Job__c> JobList = new List<sked__Job__c>();
    List<Id> listIds = new List<Id>();
    for (sked__Job_Allocation__c childObj : Trigger.new) {
        listIds.add(childObj.sked__Job__c);
    }
    sked__Job__c j = [SELECT Id, Technical_Resource_Assigned__c from sked__Job__c where Id = :listIds];
    for(sked__Job_Allocation__c JobAll : Trigger.new) {        
        sked__Resource__c resource = [SELECT Id, Name from sked__Resource__c where Id = :JobAll.sked__Resource__c];
        
        if(JobAll.sked__Resource__c != null){
            if(j.Technical_Resource_Assigned__c != null){
                j.Technical_Resource_Assigned__c = j.Technical_Resource_Assigned__c + ', ' + resource.Name;
            } else {
                j.Technical_Resource_Assigned__c = resource.Name;
            }            
            JobList.add(j);
        }
    }
    update JobList;
}

jkcjkc
I think that it has something to do with the fact that another update trigger will activate when this trigger is run
Abhishek BansalAbhishek Bansal
Hi,

SOQL queries are not allowed within the for loop and there is something also wrong with your code.
Please update your trigger code as mentioned below :

trigger UpdateJobTechnicalResource on sked__Job_Allocation__c (after insert) {
    List<sked__Job__c> JobList = new List<sked__Job__c>();
    Set<Id> listIds = new Set<Id>();
    
    Set<Id> resId = new Set<Id>();
    for (sked__Job_Allocation__c childObj : Trigger.new) {
        listIds.add(childObj.sked__Job__c);
        resId.add(childObj.sked__Resource__c)
    }
    
    Map<Id,sked__Job__c> j;
    if(listIds.size() > 0){
        j = new Map<Id,sked__Job__c>([SELECT Id, Technical_Resource_Assigned__c from sked__Job__c where Id = :listIds]);
    }
    
    Map<Id,sked__Resource__c> mapOfRes;
    
    if(resId.size() > 0){
        mapOfRes = new Map<Id,sked__Resource__c>([SELECT Id, Name from sked__Resource__c where Id = :resId]);
    }
    
    for(sked__Job_Allocation__c JobAll : Trigger.new) {
        if(mapOfRes != null && mapOfRes.containsKey(JobAll.sked__Resource__c)) {
            if(j != null && j.containsKey(JobAll.sked__Job__c)){
                if(j.get(JobAll.sked__Job__c).Technical_Resource_Assigned__c != null){
                    j.get(JobAll.sked__Job__c).Technical_Resource_Assigned__c = j.get(JobAll.sked__Job__c).Technical_Resource_Assigned__c + ', ' + mapOfRes.get(JobAll.sked__Resource__c).Name;
                }
                else{
                    j.get(JobAll.sked__Job__c).Technical_Resource_Assigned__c = mapOfRes.get(JobAll.sked__Resource__c).Name;
                }
            }
        }
    }
    if(j != null && j.size() > 0)
        update j.values();
}

Please take care of the best practices in future.

Thanks,
Abhishek.
jkcjkc

That did it. I also created the sample record on the other objects needed on the other trigger and it has passed the test.

Thank you for your help Abhishek.

Cheers,
jkc