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
SolidLucasSolidLucas 

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;
	}

Best Answer chosen by SolidLucas
SolidLucasSolidLucas
@isTest
private class tst_aiu_IfaceLimiteCreditoDiario {

    static testMethod void myUnitTest() {
			
			Account acc = new Account(
                Name = 'Teste',
                C_digo_do_Cliente__c = 10,
                LimiteCredito__c = 500,
                TipoLimite__c = '1'    
           );	
          
           insert acc;
           
           IfaceLimiteCreditoDiario__c iface = new IfaceLimiteCreditoDiario__c(
				CustomerNumber__c = '10',
				LimiteCredito__c = 500,
				TipoLimite__c = '1' 
           );
           insert iface;
    } 
}

i've finish my class test with 100% thank you  for the support guys!

All Answers

Jim JamJim Jam
try changing line 13 in your test class to insert acc;
srlawr uksrlawr uk
There are a couple of interesting querks with you code I can see, which I will highlight here. 

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!)
Virendra ChouhanVirendra Chouhan
Hi 

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
AshwaniAshwani
There are two major things which trigger is lacking:

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:

Account acc = new Account(
  C_digo_do_Cliente__c = 5,
  LimiteCredito__c = 100,
  Tipo_do_Cliente__c = '50'
  );
  insert acc;
  acc.Name='Changed Name';
  update acc;

3) There is no "WHERE" clasue in any query. This is not a good practice.

Hope it helps!
srlawr uksrlawr uk
That code will still fail with a DML exception, "_____________", because the "Name" field is going to be required for insert.... but it's a step in the right direction.
Kirtesh_JainKirtesh_Jain
You should be careful about some below few points: 

( 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
SolidLucasSolidLucas

trigger aiu_IfaceLimiteCreditoDiario on IfaceLimiteCreditoDiario__c (after insert, after update) {
		
	Map<Double, IfaceLimiteCreditoDiario__c> mapIface = new Map<Double, IfaceLimiteCreditoDiario__c>();
	List<Account> listAcc = new List<Account>();
	for(IfaceLimiteCreditoDiario__c iface : Trigger.new){
		if(!mapIface.containsKey(Double.valueOf(iface.CustomerNumber__c)))
			mapIface.put(Double.valueOf(iface.CustomerNumber__c), iface);
	}
	for (Account acc : [
					  select a.id,a.TipoLimite__c, a.LimiteCredito__c, a.C_digo_do_Cliente__c
					   from Account a 
					   where a.C_digo_do_Cliente__c in :mapIface.keySet()]) {
		if(mapIface.containsKey(acc.C_digo_do_Cliente__c)){
			IfaceLimiteCreditoDiario__c item = mapIface.get(acc.C_digo_do_Cliente__c); 
			acc.LimiteCredito__c = item.LimiteCredito__c;
			acc.TipoLimite__c = item.TipoLimite__c;
			listAcc.add (acc);	
		}		
	}
	update listAcc;
}
i've improved my trigger now i'm trying to fix the test class,i'm stil confused on testing.
SolidLucasSolidLucas
@isTest
private class tst_aiu_IfaceLimiteCreditoDiario {

    static testMethod void myUnitTest() {
			
			Account acc = new Account(
                Name = 'Teste',
                C_digo_do_Cliente__c = 10,
                LimiteCredito__c = 500,
                TipoLimite__c = '1'    
           );	
          
           insert acc;
           
           IfaceLimiteCreditoDiario__c iface = new IfaceLimiteCreditoDiario__c(
				CustomerNumber__c = '10',
				LimiteCredito__c = 500,
				TipoLimite__c = '1' 
           );
           insert iface;
    } 
}

i've finish my class test with 100% thank you  for the support guys!
This was selected as the best answer