salesforce - Apex Trigger Works, can not get test pass -


i came 3 hours later , no changes code, test execution worked. disregard question

i have written trigger works great setting accountid on contact record based on external id.

i had test worked great, added logic trigger updates contacts 'default account' account. test fails saying did not update account though still works designed when saving records.

what missing?

i added line 4

id defaultaccid = [select id account name = 'default account'].id; 

and if around line 9

if (contactnew.accountid == defaultaccid) { } 

to trigger:

trigger trg_contact_sethouseholdid on contact (before update, before insert) { set<string> accidlist = new set<string>(); id defaultaccid = [select id account name = 'default account'].id;  // loop through records finding thoes need linked for(contact contactnew:trigger.new){     if (contactnew.accountid == defaultaccid) {         accidlist.add(contactnew.rphouseholdid__c);     } }  map<string, id> accountmap = new map<string, id>();  //loop needing linked , account id if exist      for(account oneaccount:[select rphouseholdid__c, id account rphouseholdid__c in :accidlist]){     accountmap.put(oneaccount.rphouseholdid__c, oneaccount.id); }  // loop through records updating ones need link for(contact contactnew:trigger.new){     if (accountmap.get(contactnew.rphouseholdid__c) <> null){         contactnew.accountid = accountmap.get(contactnew.rphouseholdid__c);     } } } 

test class:

@istest private class trg_contact_sethouseholdid_test { static testmethod void test0_testinsertwithvalue(){     inserttestaccounts();     account defaultaccount = [select id account name = 'default account' limit 1];     account newaccount = [select id account name = 'camper family' limit 1];     test.starttest();         inserttestcontact(defaultaccount.id);     test.stoptest();     contact newcontact = [select id,accountid contact firstname = 'happy' , lastname = 'camper' limit 1];     system.assertequals(newcontact.accountid, newaccount.id, 'accountid did not changed default (' + defaultaccount.id + ')'); }  private static void inserttestaccounts() {     account obj = new account();     obj.name = 'default account';     insert obj;     obj = new account() ;     obj.name = 'camper family';     obj.rphouseholdid__c = '111';     insert obj;  } private static void inserttestcontact(id defaultid) {     contact obj = new contact();     obj.accountid = defaultid;     obj.firstname = 'happy';     obj.lastname = 'camper';     obj.rphouseholdid__c = '111';     insert obj; } } 

expanding bit on @zachelrath 's answer, beginning api version 24.0, test methods isolated data in organization (with few exceptions user, recordtype, etc. see here full list). rather changing api version of file, easiest thing add see data directive istest annotation, so:

@istest(seealldata=true) private class trg_contact_sethouseholdid_test { // etc 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -