+ Start a Discussion
Chitral ChaddaChitral Chadda 

test class help

here is the trigger
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);
   }
  }  
}
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;
     
     ac =[Select id, Rival__c,Rival_Picklist__c FROM Account WHERE Rival__c =: rc.id];
     system.assertEquals(rc.id,ac.Rival__c);
    
  }
 }
error:
System.QueryException: List has no rows for assignment to SObject



 
Best Answer chosen by Chitral Chadda
Chitral ChaddaChitral Chadda
thnks mithun
trigger works
but still error in 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,ac.Rival__c);
    //system.assertEquals(rc.id,ac.Rival__c);
  }
 }

error: System.AssertException: Assertion Failed: Expected: a049000000Uj9hcAAB, Actual: null

All Answers

Frédéric TrébuchetFrédéric Trébuchet
Hi,

Your trigger is designed to fire before insert and before update but your test class doesn't do any update.
Also, you should use a list to store the query result un place of a single sObjetc.
Try to replace
ac = [SELECT....
by
List<Account> accts = [SELECT...
Here is a post about the same subject http://salesforce.stackexchange.com/questions/41484/understanding-list-has-no-rows-for-assignment-to-sobject-in-a-trigger-creating.

Hope this helps,
Fred
 
Chitral ChaddaChitral Chadda
thnks .
i get error
System.ListException: List index out of bounds: 0
@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);
    
  }
 }

 
Frédéric TrébuchetFrédéric Trébuchet
Field Rival__c of Account is never updated
Chitral ChaddaChitral Chadda
i didnt get
Rival__c is a look up field on account to Rival object
Frédéric TrébuchetFrédéric Trébuchet
So, you have to pass Rival__c id to the account's lookup field.
Something like this:
ac.Rival__c = rc.id;
Here is an other example http://stackoverflow.com/questions/20162852/how-to-insert-data-into-a-custom-object-with-lookup-field-from-apex-controller.

Fred
MithunPMithunP
Hi Chitral,

Update your trigger like below.
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)
  {
      {
        string rvname = ((r.Name__c.toUpperCase()).trim()).replaceAll('[^\\x00-\\x7F]', '');

          rvlMap.put(rvname,r.id);
      }
  } 
}     
 for(account acc: trigger.new)
 { 
string acpickval = ((acc.Rival_Picklist__c.toUpperCase()).trim()).replaceAll('[^\\x00-\\x7F]', '');


 if(rvlMap!=null&&rvlMap.containsKey(acpickval))
   {
     acc.Rival__c = rvlMap.get(acpickval);
   }
  }  
}



Best Regards,
Mithun.
Chitral ChaddaChitral Chadda
hi mithun..
why we did this at line number -- ->> 17,9
string acpickval = ((acc.Rival_Picklist__c.toUpperCase()).trim()).replaceAll('[^\\x00-\\x7F]', '');
Chitral ChaddaChitral Chadda
also, when i cud save ur trigger
bt wen i insert account record i get error
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CHITRALPACKAGE.populateRivals caused an unexpected exception, contact your administrator: CHITRALPACKAGE.populateRivals: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CHITRALPACKAGE.populateRivals: line 9, column 1
MithunPMithunP
Hi Chitral,

Because map key values are case sensitive, you can try below updated code.
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>();
  
  if(rvl!=null && rvl.size()>0)
  {
  for(Rival__c r:rvl){
      {
       if(r.Name__c != null){
             string rvname = ((r.Name__c.toUpperCase()).trim()).replaceAll('[^\\x00-\\x7F]', '');

              rvlMap.put(rvname,r.id);
       }
      }
  } 
}     
 for(account acc: trigger.new)
 { 
 if(acc.Rival_Picklist__c != null){
string acpickval = ((acc.Rival_Picklist__c.toUpperCase()).trim()).replaceAll('[^\\x00-\\x7F]', '');


 if(rvlMap!=null&&rvlMap.containsKey(acpickval))
   {
     acc.Rival__c = rvlMap.get(acpickval);
   }
}
  }  
}

Best Regards,
Mithun.
Chitral ChaddaChitral Chadda
thnks mithun
trigger works
but still error in 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,ac.Rival__c);
    //system.assertEquals(rc.id,ac.Rival__c);
  }
 }

error: System.AssertException: Assertion Failed: Expected: a049000000Uj9hcAAB, Actual: null
This was selected as the best answer
MithunPMithunP
Hi Chitral,

Try this test class
 
@isTest (SeeAllData = true)
 public class TestpopulateRivals{
 public static testmethod void testpopulaterivals()
 {
     
     
     Rival__c rc = new Rival__c(Name ='Chitral', name__c = 'Chitral');
     insert rc;
     
     account ac = new account(Name = 'Chitral Chadda');
     ac.Rival_Picklist__c = 'Chitral';
     
     insert ac;
        
     system.assertEquals(rc.id,ac.Rival__c);
    
  }
 }



Best Regards,
Mithun.
Chitral ChaddaChitral Chadda
thnx  Mithun
i used this test class only same error:
System.AssertException: Assertion Failed: Expected: a049000000UjHMcAAN, Actual: null
Chitral ChaddaChitral Chadda
i used the test class u gave i.e after insert without quering..
i got above error

when i insert and then query the result 
i got 100 % coverage
Chitral ChaddaChitral Chadda

Hi mithun,
in reply to my question  :
why we did this at line number -- ->> 17,9
string acpickval = ((acc.Rival_Picklist__c.toUpperCase()).trim()).replaceAll('[^\\x00-\\x7F]', '');

as u told me --
Because map key values are case sensitive, you can try below updated code.

i understand that whenevr we use anything other than id in key  we shud use trim . is it 

as it us case sensitive.
or is there anything else

cz in my trigger i m using a field (Name ) as a key ..thats why u used this trim function ?
 

MithunPMithunP
Hi Chitral,

Yes, there is other issue i faced in one of my project , If you use string as a key in Map, some times there may be some invisible characters will add to your strings due to various keyboards. These situations we can't identify the issue so better to write difense coding. 

Best Regards,
Mithun.