You need to sign in to do that
Don't have an account?
bhanu_prakash
test class for lookup
i have trigger i tried to write trigger iam stuck with how to installlize values into lookup field
trigger:
trigger updatecompany on Expense_Report__c(after insert){
map<Id,Region__c> mapIds = new map<Id,Region__c>();
for(Expense_Report__c er : trigger.new){
if(er.Company__c != null || er.Company__c != ''){
Region__c reg = new Region__c(id= er.Region__c);
reg.Company__c= er.Company__c;
mapIds.put(er.Company__c,reg);
}
}
if(!mapIds.isEmpty()){
update mapIds.values();
}
}
and test class
@isTest
public with sharing class updatecompanyTestClass {
public static testmethod void updatecompanyTest(){
List<Expense_Report__c> PERlist = new List<Expense_Report__c>();
Expense_Report__c PERObj = new Expense_Report__c(Name='Travel',Region__c='Russia',Company__c='Genpact');
insert PERObj;
test.starttest();
update PERObj;
test.stopTest();
system.assertEquals(PERObj.Company__c, PERObj.Company__c);
}
}
FATAL_ERROR System.StringException: Invalid id: Russia here region (Russia) is lookup field and
FATAL_ERROR System.StringException: Invalid id: XYZ here Comany(XYZ) is lookup field
Iam facing above issues, please help me out
trigger:
trigger updatecompany on Expense_Report__c(after insert){
map<Id,Region__c> mapIds = new map<Id,Region__c>();
for(Expense_Report__c er : trigger.new){
if(er.Company__c != null || er.Company__c != ''){
Region__c reg = new Region__c(id= er.Region__c);
reg.Company__c= er.Company__c;
mapIds.put(er.Company__c,reg);
}
}
if(!mapIds.isEmpty()){
update mapIds.values();
}
}
and test class
@isTest
public with sharing class updatecompanyTestClass {
public static testmethod void updatecompanyTest(){
List<Expense_Report__c> PERlist = new List<Expense_Report__c>();
Expense_Report__c PERObj = new Expense_Report__c(Name='Travel',Region__c='Russia',Company__c='Genpact');
insert PERObj;
test.starttest();
update PERObj;
test.stopTest();
system.assertEquals(PERObj.Company__c, PERObj.Company__c);
}
}
FATAL_ERROR System.StringException: Invalid id: Russia here region (Russia) is lookup field and
FATAL_ERROR System.StringException: Invalid id: XYZ here Comany(XYZ) is lookup field
Iam facing above issues, please help me out
In test class you need to create look up records for Region and Company then you provide those ID's while creation of new expense report
Region__c region = new Region(Name = 'Russia');
insert region;
Company__c company = new Company (Name = 'Genpact');
insert company;
Expense_Report__c PERObj = new Expense_Report__c(Name='Travel',Region__c=region.Id,Company__c=company.Id);