You need to sign in to do that
Don't have an account?

FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, restrictCase: execution of BeforeInsert
Dear All,
I was trying to write test class for my trigger, but it it giving me error like below
'FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, restrictCase: execution of BeforeInsert' , 'FATAL_ERROR caused by: System.NullPointerException: Attempt to de-reference a null object'
heres my trigger
and heres the test class I've written
Please help me finding the issue.
Thanks a lot in advance.
I was trying to write test class for my trigger, but it it giving me error like below
'FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, restrictCase: execution of BeforeInsert' , 'FATAL_ERROR caused by: System.NullPointerException: Attempt to de-reference a null object'
heres my trigger
trigger restrictCase on Case(before insert) { MaxCase__c mxx = MaxCase__c.getInstance('MaxValue'); Integer mvv = Integer.valueOf(mxx.Max__c); system.debug('int value' + mvv); Set<Id> userIds = new Set<Id>(); for (case cc : Trigger.new) { userIds.add(cc.ownerId); } List<User> userss = [Select Id, Name from User where Id in :userIds]; Map<Id, Integer> userCases = new Map<Id,Integer>(); for (user u : userss) { integer countCase = [Select count() from Case where ownerId=:u.Id AND createdDate = This_Month]; userCases.put(u.id, countCase); system.Debug('user and case' + u.Name + ' . ' + countcase); } for (case c : trigger.new) { Integer countc = userCases.get(c.OwnerId); system.debug('current user case count' + countc); if (countc > mvv) { c.addError('Too many cases created this month for user' + c.owner.Name + '(' + c.OwnerId + ')' + ':' + mvv); } } }
and heres the test class I've written
@isTest public class testRestrictCase { static testMethod void caseCreation1 () { MaxCase__c maxi = new MaxCase__c(); maxi.Name = 'newMax'; maxi.Max__c = 80; insert maxi; system.debug('maxi custom setting' + maxi.Name); Integer maxiInt = Integer.valueOf(maxi.Max__c); system.debug('max value' + maxiInt); Account acct = new Account(Name='anamika'); insert acct; system.debug('account inserted' + acct.name); Contact con = new Contact(FirstName='aa', LastName='bb', AccountId=acct.Id, email='abc@g.com'); insert con; system.debug('contact inserted' + con.name); List<Case> casess = new List<Case>(); for(integer i = 0; i<maxiInt ; i++) { Case TestC = new Case(Subject='Test Controller Acct Case'+ i, accountId=acct.id, status='New', origin ='Email', contactId=con.id, priority='Medium', type='Mechanical', reason='Installation'); casess.add(Testc); system.debug('testC .i.' + TestC.Subject); } insert casess; system.debug('cases size' + casess.size()); Case TestC1 = new Case(Subject='Test Controller Acct Case new', accountId=acct.id, status='New', origin ='Email', contactId=con.id, priority='Medium', type='Mechanical', reason='Installation'); try{ insert TestC1; } catch(DMLException ex) { system.debug('max cases inserted'); } } }
Please help me finding the issue.
Thanks a lot in advance.
Can you insert "MaxCase__c" custom setting in your test code and then try again.
Best Regards,
Vijay