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
Chitral ChaddaChitral Chadda 

test class trriger

trigger populateRivals on account(before insert,before update)
{    
  list<Rival__c> rvl = [select id,Name__c from rival__c ];
  map<string,id> rvlMap = new map<string,id>();
  for(Rival__c r:rvl)
  if(rvl!=null && rvl.size()>0)
  {
      {
          rvlMap.put(r.Name__c,r.id);
      }
  }      
 for(account acc: trigger.new)
 {  if(rvlMap!=null&&rvlMap.containsKey(acc.Rival_Picklist__c))
   {
     acc.Rival__c = rvlMap.get(acc.Rival_Picklist__c);
   }
  }  
}
here is the test class
@isTest
 public class TestpopulateRivals{
 public static testmethod void testpopulaterivals()
 {
     
     
     Rival__c rc = new Rival__c(Name ='Chitral');
     insert rc;
     
     account ac = new account(Name = 'Chitral Chadda');
     ac.Rival_Picklist__c = 'Chitral';
     
     insert ac;
     
    List<Account> acct =[Select id, Rival__c,Rival_Picklist__c FROM Account WHERE Rival__c =: rc.id];
     system.assertEquals(rc.id,acct[0].Rival__c);
    
  }
 }

i get error: System.ListException: List index out of bounds: 0
any idea?
Joel RichardsJoel Richards
The error is caused from the line: system.assertEquals(rc.id,acct[0].Rival__c);
The 'List Index' of  'acct[0]' mustn't exist. This means that the SOQL query preceding it isn't returning any values. Not sure why... your trigger looks ok, though a cleaner version would be:
trigger populateRivals on account(before insert,before update)
{    
    map<string,id> rvlMap = new map<string,id>();
    for(Rival__c r: [select id,Name__c from rival__c ]) {
        rvlMap.put(r.Name__c,r.id);
    }      
 
    for(account acc: trigger.new) {  
	
        if(!rvlMap.isEmpty() && rvlMap.containsKey(acc.Rival_Picklist__c))
             acc.Rival__c = rvlMap.get(acc.Rival_Picklist__c);
    }  
}
Are there any mandatory fields on the Account or Rival__c objects that are requried that you haven't included when inserting them in your Test Code (which would then cause the insertion to fail)?
 
Chitral ChaddaChitral Chadda
Heu thnku I vl check wd this triger Did u find anything conceptually wrong othrwise in test class