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
bhanu_prakashbhanu_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
VamsiVamsi
Hi,

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 

 
Amil_AbdallahAmil_Abdallah
You are attempting to put text into a lookup field.  A lookup field requires an id.  To get an id, you need to create an instance of Region and Company and insert those records.  Before you create a new expense report you need to do something like this (I don't know your object/fields so I'm just showing a simple example):

  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);