You need to sign in to do that
Don't have an account?
SolidLucas
test class
well i'm trying to do the test class for my trigger but it is give me System.DmlException when update.
@isTest private class aiu_IfaceLimiteCreditoDiario { static testMethod void myUnitTest() { List<Account> Auxacc = new List<Account>(); List<Account> ListAcc = [Select a.TipoLimite__c, a.LimiteCredito__c, a.C_digo_do_Cliente__c From Account a]; Account acc = new Account( C_digo_do_Cliente__c = 5, LimiteCredito__c = 100, Tipo_do_Cliente__c = '50' ); update acc; } }
trigger aiu_IfaceLimiteCreditoDiario on IfaceLimiteCreditoDiario__c (after insert, after update) { List<Account> Auxacc = new List<Account>(); List<Account> ListAcc = [Select id,a.TipoLimite__c, a.LimiteCredito__c, a.C_digo_do_Cliente__c From Account a]; for(IfaceLimiteCreditoDiario__c iface : Trigger.new){ for(Account acc : ListAcc){ if(String.valueOf(acc.C_digo_do_Cliente__c) == iface.CustomerNumber__c){ Account accs = acc; accs.LimiteCredito__c = iface.LimiteCredito__c; accs.TipoLimite__c = iface.TipoLimite__c; Auxacc.add (accs); } } } update Auxacc; }
i've finish my class test with 100% thank you for the support guys!
All Answers
As for the DML exception, on line 13 in your test you are "updating" a new account object, which you can't do. It has no ID, no existing record to update, so this will throw an exception.
Your test has no access to existing data in the system, so the query will probably return no results (unless somewhere else in the test context you are inserting accounts)... but that might not really matter, because lines 05 and 06 of your test don't seem to do anything anyway? You just create a "new" account and try to update it... First you are going to have to insert this account, so it has an ID, and a record, and then you can alter the fields and do an update.
Hopefully that makes sense!
(Also, in both your code and your test, you have a non-selective query on Account (ie. you are loading every account in the system, there is no where clause) - this is concerning to me. Just thought I'd say!)
As i understand In your Test class you update Accpount acc.
but first you must have to insert that record before update.
so write Insert operation and then change any value and then do Update
Regards
Virendra
1) You are quering account is a test class. Test classes are isolated unti you do not set @(seeAllData=true). So there should be no record in account list ListAcc. Also there in no meaning to query Accounts.
2) You are updating an Account which doesn't exist in database. So it is obvious that it will throw DML exception.
Try some thing like that:
3) There is no "WHERE" clasue in any query. This is not a good practice.
Hope it helps!
( 1) Need to define the Size check before writing DML operation for ex: if (Auxacc.size() > 0 ) { update Auxacc; } . This will allow records if size is greater than 0.
(2) Second thing is that you didn't mention any Id for refernece in test code :
Account acc = new Account(C_digo_do_Cliente__c = 5,LimiteCredito__c = 100,Tipo_do_Cliente__c = '50');
Add Id field and populate it before doing Update.
Please mark this as correct answer if it solves your problem.
Thanks,
Kirtesh
i've improved my trigger now i'm trying to fix the test class,i'm stil confused on testing.
i've finish my class test with 100% thank you for the support guys!