You need to sign in to do that
Don't have an account?
Seksaki
write test class for trigger
Hello,
can you help me write a test for this trigger plz
Thanks for help
can you help me write a test for this trigger plz
trigger ContractTrigger on Contract (after update) { Public String[] statut_activated {set;get;} Public String[] statut_waiting {set;get;} Public String[] statut_terminated {set;get;} statut_activated = new String[] {'Accepted','Accepted2'}; statut_waiting = new String[] {'waiting','waiting2'}; statut_terminated = new String[] {'terminated','terminated2'}; List<Decimal> listAmount= new List<Decimal>(); set<Id> AccountIds = new set<Id>(); for(Contract c : Trigger.new){ if(c.AccountId != null) AccountIds.add(c.AccountId); } Map<String, Contract> cntrMap = new Map<String, Contract>(); for(Contract contr : [Select Id,contract_stat__c,amount__c From Contract Where AccountId IN: AccountIds]){ cntrMap.put(contr.Id , contr); Account acc = [SELECT Id,st__c FROM Account WHERE Id IN: AccountIds]; if(contr.amount__c > 0){ listAmount.add(contr.amount__c); }else{ acc.checked__c=FALSE; update acc; } if(statut_activated.contains(contr.contract_stat__c)){ cntrMap.put(contr.contract_stat__c , contr); acc.st__c='customer'; update acc; break; /* Contrat En attente*/ }else if(statut_waiting.contains(contr.contract_stat__c)){ acc.st__c='customer_type2'; update acc; break; /* Contrat En Terminé */ }else if(statut_terminated.contains(contr.contract_stat__c)){ acc.st__c='customer_type3'; update acc; break; }else{ acc.st__c='customer_other'; update acc; } } for(Decimal l : listAmount){ if(l > 0){ Account acc = [SELECT Id,st__c FROM Account WHERE Id IN: AccountIds]; acc.checked__c=TRUE; update acc; } } }
Thanks for help
Hello there Seksaki!
Your trigger on Contract is on "after update" but in your testclass you are not triggering an update on the contract-record.
Either change the "after update" to "after insert" or make sure that you update the contract in your test-class so that the trigger is being triggered!
All Answers
Hello there Seksaki!
Your trigger on Contract is on "after update" but in your testclass you are not triggering an update on the contract-record.
Either change the "after update" to "after insert" or make sure that you update the contract in your test-class so that the trigger is being triggered!
I've added to your test-class so that it covers 100% of your trigger, if you add (after update, after insert) to the trigger.
indeed, when you said after insert it worked, but do you have to add after insert even if I don't use it?
thanks a lot for your help Daniel.
No, you do not need to have the "after insert" if the trigger is only supposed to be used on before the records are inserted to the database.
You are welcome! :)