You need to sign in to do that
Don't have an account?
Sandeep M 1
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
I have a trigger which has test coverage upto 39% but while deploying it is giving error as my trigger coverage is only 0% . could anybody help me out.
below is my error.
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
Stack Trace: Class.TestTrigger.validateTrigger: line 7, column 1
my trigger is
trigger employeedaysoffTrigger on SFDC_PTO_Request__c (after update,after insert) {
Set<ID> Ids=new Set<Id>();
Set<ID> bIds=new Set<Id>();
Decimal d,e,f;
List<SFDC_Employee__c> emp= new LIST<SFDC_Employee__c>();
List<SFDC_PTO_Request__c> bbq=new LIST<SFDC_PTO_Request__c>();
List<SFDC_PTO_Request__c> lq=new LIST<SFDC_PTO_Request__c>();
//To avoid recursion using static boolean variable
private static boolean run = true;
if(TriggerContextUtility.isFirstRun())
{
run=false;
//Take the ids of all newly inserting or updating records
for(SFDC_PTO_Request__c b:Trigger.New)
{
if(b.Employee__c!=null)
{
Ids.add(b.Employee__c);
bIds.add(b.Id);
}
}
//list out all values from leave request
lq= [SELECT id,employee__r.id,employee__r.total_casual_days_off__c,employee__r.total_sick_days_off__c,
total_casual_leaves__c,total_sick_leaves__c,available_casual_leaves__c,available_sick_leaves__c,
status__c,type_of_leave__c from SFDC_PTO_Request__c where id =:bIds];
//Check the status of the leave request after record is approved/rejected
if(lq[0].status__c == 'Approved')
{
for(AggregateResult ar:[select Employee__c em,sum(days_off__c) s,sum(casual_days_off__c) c,
sum(sick_days_off__c) dos from SFDC_PTO_Request__c
where Employee__c in : Ids Group BY Employee__c])
{
d= (Decimal) ar.get('s');
e= (Decimal) ar.get('c');
f= (Decimal) ar.get('dos');
}
//Update fields of total casual days off and total sick days off in employee object
for(SFDC_Employee__c bac:[select id,total_days_off__c,total_casual_days_off__c,total_sick_days_off__c
from SFDC_Employee__c where id=:Ids])
{
if(lq[0].type_of_leave__c=='Casual Leave')
{
bac.total_casual_days_off__c=e;
emp.add(bac);
}
else if(lq[0].type_of_leave__c=='Sick Leave')
{
bac.total_sick_days_off__c=f;
emp.add(bac);
}
}
//update available casual and sick leaves fields in leave request object
for(SFDC_PTO_Request__c l: lq)
{
if(l.type_of_leave__C=='Casual Leave')
{
l.Available_Sick_Leaves__c=l.Total_Sick_Leaves__c-f;
l.available_casual_leaves__c=l.total_casual_leaves__c-e;
bbq.add(l);
}
else if(l.type_of_leave__c=='sick leave')
{
l.available_sick_leaves__c=l.total_sick_leaves__c-f;
l.available_casual_leaves__c=l.total_casual_leaves__c-e;
bbq.add(l);
}
}
TriggerContextUtility.setFirstRunFalse();
update bbq;
update emp;
}
}
}
my test class for the above trigger is
@isTest
public class TestTrigger
{
static testmethod void validateTrigger()
{
SFDC_PTO_Request__c spreq= new SFDC_PTO_Request__c(Employee__c='a0EK00000092v59',available_casual_leaves__c=20,available_sick_leaves__c=10);
insert spreq;
spreq=[select id,Employee__c,available_casual_leaves__c,Available_sick_Leaves__c,status__c,type_of_leave__c from SFDC_PTO_Request__c where Employee__c='a0EK00000092v59'];
System.assertEquals(20,spreq.Available_Casual_Leaves__c);
System.assertEquals(10,spreq.Available_sick_Leaves__c);
SFDC_Employee__c emp=new SFDC_Employee__c();
emp.Total_casual_days_off__c=20;
emp.Total_sick_days_off__c=10;
emp.Pan_Number__c='bmplm8889m';
insert emp;
emp=[select id,total_days_off__c,total_casual_days_off__c,total_sick_days_off__c
from SFDC_Employee__c where id='a0EK00000092v59'];
System.assertEquals(20,emp.Total_casual_days_off__c);
System.assertEquals(10,emp.Total_sick_days_off__c);
}
}
below is my error.
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
Stack Trace: Class.TestTrigger.validateTrigger: line 7, column 1
my trigger is
trigger employeedaysoffTrigger on SFDC_PTO_Request__c (after update,after insert) {
Set<ID> Ids=new Set<Id>();
Set<ID> bIds=new Set<Id>();
Decimal d,e,f;
List<SFDC_Employee__c> emp= new LIST<SFDC_Employee__c>();
List<SFDC_PTO_Request__c> bbq=new LIST<SFDC_PTO_Request__c>();
List<SFDC_PTO_Request__c> lq=new LIST<SFDC_PTO_Request__c>();
//To avoid recursion using static boolean variable
private static boolean run = true;
if(TriggerContextUtility.isFirstRun())
{
run=false;
//Take the ids of all newly inserting or updating records
for(SFDC_PTO_Request__c b:Trigger.New)
{
if(b.Employee__c!=null)
{
Ids.add(b.Employee__c);
bIds.add(b.Id);
}
}
//list out all values from leave request
lq= [SELECT id,employee__r.id,employee__r.total_casual_days_off__c,employee__r.total_sick_days_off__c,
total_casual_leaves__c,total_sick_leaves__c,available_casual_leaves__c,available_sick_leaves__c,
status__c,type_of_leave__c from SFDC_PTO_Request__c where id =:bIds];
//Check the status of the leave request after record is approved/rejected
if(lq[0].status__c == 'Approved')
{
for(AggregateResult ar:[select Employee__c em,sum(days_off__c) s,sum(casual_days_off__c) c,
sum(sick_days_off__c) dos from SFDC_PTO_Request__c
where Employee__c in : Ids Group BY Employee__c])
{
d= (Decimal) ar.get('s');
e= (Decimal) ar.get('c');
f= (Decimal) ar.get('dos');
}
//Update fields of total casual days off and total sick days off in employee object
for(SFDC_Employee__c bac:[select id,total_days_off__c,total_casual_days_off__c,total_sick_days_off__c
from SFDC_Employee__c where id=:Ids])
{
if(lq[0].type_of_leave__c=='Casual Leave')
{
bac.total_casual_days_off__c=e;
emp.add(bac);
}
else if(lq[0].type_of_leave__c=='Sick Leave')
{
bac.total_sick_days_off__c=f;
emp.add(bac);
}
}
//update available casual and sick leaves fields in leave request object
for(SFDC_PTO_Request__c l: lq)
{
if(l.type_of_leave__C=='Casual Leave')
{
l.Available_Sick_Leaves__c=l.Total_Sick_Leaves__c-f;
l.available_casual_leaves__c=l.total_casual_leaves__c-e;
bbq.add(l);
}
else if(l.type_of_leave__c=='sick leave')
{
l.available_sick_leaves__c=l.total_sick_leaves__c-f;
l.available_casual_leaves__c=l.total_casual_leaves__c-e;
bbq.add(l);
}
}
TriggerContextUtility.setFirstRunFalse();
update bbq;
update emp;
}
}
}
my test class for the above trigger is
@isTest
public class TestTrigger
{
static testmethod void validateTrigger()
{
SFDC_PTO_Request__c spreq= new SFDC_PTO_Request__c(Employee__c='a0EK00000092v59',available_casual_leaves__c=20,available_sick_leaves__c=10);
insert spreq;
spreq=[select id,Employee__c,available_casual_leaves__c,Available_sick_Leaves__c,status__c,type_of_leave__c from SFDC_PTO_Request__c where Employee__c='a0EK00000092v59'];
System.assertEquals(20,spreq.Available_Casual_Leaves__c);
System.assertEquals(10,spreq.Available_sick_Leaves__c);
SFDC_Employee__c emp=new SFDC_Employee__c();
emp.Total_casual_days_off__c=20;
emp.Total_sick_days_off__c=10;
emp.Pan_Number__c='bmplm8889m';
insert emp;
emp=[select id,total_days_off__c,total_casual_days_off__c,total_sick_days_off__c
from SFDC_Employee__c where id='a0EK00000092v59'];
System.assertEquals(20,emp.Total_casual_days_off__c);
System.assertEquals(10,emp.Total_sick_days_off__c);
}
}
You just need to create separate data for you test class for example create an Employee__c records in test class like Employee__c emp = new Employee__c(Name = 'Test employee' ) ; and provide some other fields information which is mandatory for records to be inserted then insert the record and use emp.Id instead of "a0EK00000092v59" .
Hope it will solve you problem.
Please mark my answer as a solution to your question if it solves your problem.
thanks,
www.forcexplore.com/2014/01/salesforce-interview-questions-part-7.html