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
Reshmi SmijuReshmi Smiju 

How to Refrence Lookup Field in AssetEquals Method.

Hi.
I have a custome object called Rival_c which is having  a lookup relation with account on the field Rival_c.  While writing a test class for a trigger , SOQL Query (mensioned below)has been written following with assertEquals method. But i am not sure, how to reference the lookup fields value in the assertEquals method.

@isTest
public class TestGetRivalValue {
    static testMethod void TestGetRivalValuemthd(){
        Rival__c  riv = new Rival__c();
        riv.Name = 'Best';
        insert riv;
        
        Account acc1 = new Account();
        acc1.Name= 'test rivalss';
        acc1.Rival_Picklist__c ='Best';
        insert acc1;
        
        List<Account> accs =  new List<Account>();
        for (Integer i=0;i<200;i++){
        Account acc= new Account();
        acc.Name= 'test rivalss';
        acc.Rival_Picklist__c ='Best';
        accs.add(acc);
        }
        insert accs;
        List <Account> newaccs = new List<Account>();
        newaccs = [SELECT Id, Rival_Picklist__c, Rival__r.Name FROM Account]; ******** Hope this line is correct.
        for (Account a:newaccs){
        System.assertEquals(a.Rival__r.Name,a.Rival__c); ******* This is i am not sure, how to incoporate the lookup value.
            }
    }
}
Expected value should be the Name (System field) in the Rival_c Object.
Actual value should be the Rival_c Lookup field in the Account Object.

Pls let me know.
Thanks  In Advance
Reshmi
 
Best Answer chosen by Reshmi Smiju
AbdelhakimAbdelhakim
hi,

Try to use this
@isTest
public class TestGetRivalValue {
    static testMethod void TestGetRivalValuemthd(){
        Rival__c  riv = new Rival__c();
        riv.Name = 'Best';
        insert riv;
        
        Account acc1 = new Account();
        acc1.Name= 'test rivalss';
        acc1.Rival_Picklist__c ='Best';
		/* add relation */
		acc1.Rival__c = riv.id;
        insert acc1;
        
        List<Account> accs =  new List<Account>();
        for (Integer i=0;i<200;i++){
			Account acc= new Account();
			acc.Name= 'test rivalss';
			acc.Rival_Picklist__c ='Best';
			
			/* add relation */
			acc.Rival__c = riv.id;
			
			accs.add(acc);
        }
        insert accs;
		
        List <Account> newaccs = [SELECT Id, Rival_Picklist__c, Rival__r.Name FROM Account];
        for (Account a:newaccs){
			System.assertEquals(a.Rival__r.Name,'Best');
        }
    }
}

 

All Answers

AbdelhakimAbdelhakim
hi,

Try to use this
@isTest
public class TestGetRivalValue {
    static testMethod void TestGetRivalValuemthd(){
        Rival__c  riv = new Rival__c();
        riv.Name = 'Best';
        insert riv;
        
        Account acc1 = new Account();
        acc1.Name= 'test rivalss';
        acc1.Rival_Picklist__c ='Best';
		/* add relation */
		acc1.Rival__c = riv.id;
        insert acc1;
        
        List<Account> accs =  new List<Account>();
        for (Integer i=0;i<200;i++){
			Account acc= new Account();
			acc.Name= 'test rivalss';
			acc.Rival_Picklist__c ='Best';
			
			/* add relation */
			acc.Rival__c = riv.id;
			
			accs.add(acc);
        }
        insert accs;
		
        List <Account> newaccs = [SELECT Id, Rival_Picklist__c, Rival__r.Name FROM Account];
        for (Account a:newaccs){
			System.assertEquals(a.Rival__r.Name,'Best');
        }
    }
}

 
This was selected as the best answer
Reshmi SmijuReshmi Smiju
Thank you very much  Abdelhakim :)
Forgot to add the Relationship Id. :(