You need to sign in to do that
Don't have an account?
Chitral Chadda
test class help
here is the trigger
System.QueryException: List has no rows for assignment to SObject
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
trigger works
but still error in test class
error: System.AssertException: Assertion Failed: Expected: a049000000Uj9hcAAB, Actual: null
All Answers
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 by 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
i get error
System.ListException: List index out of bounds: 0
Rival__c is a look up field on account to Rival object
Something like this: 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
Update your trigger like below.
Best Regards,
Mithun.
why we did this at line number -- ->> 17,9
string acpickval = ((acc.Rival_Picklist__c.toUpperCase()).trim()).replaceAll('[^\\x00-\\x7F]', '');
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
Because map key values are case sensitive, you can try below updated code.
Best Regards,
Mithun.
trigger works
but still error in test class
error: System.AssertException: Assertion Failed: Expected: a049000000Uj9hcAAB, Actual: null
Try this test class
Best Regards,
Mithun.
i used this test class only same error:
System.AssertException: Assertion Failed: Expected: a049000000UjHMcAAN, Actual: null
i got above error
when i insert and then query the result
i got 100 % coverage
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 ?
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.