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

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY in test class
Hi all,
i wrote test class for this trigger
trigger ActivityReportTrigger on Task (before insert)
{
account a1;
integer count=0;
integer count1=0;
list<Account> acclist= new list<Account>();
Map<String,Integer> m= new Map<String,Integer>();
Map<String,Integer> m1= new Map<String,Integer>();
set<id> s= new set<id>();
for(task t:trigger.new)
s.add(t.whatid);
for(account a:[select id,Sum_of_emails__c,Sum_of_calls__c,Sum_of_call_durations__c from account where id in:s])
a1=a;
for(task t:trigger.new)
{
if(m.get(t.Subject)!=null && t.Subject!=null && t.Subject=='Email')
count=m.get(t.Subject);
count++;
system.debug(count+'$$$$$');
m.put(t.Subject,count);
for(String tm: m.keyset())
{
if(m.get(t.Subject)!=null && t.Subject!=null && t.Subject=='Email')
{
a1.Sum_of_emails__c=a1.Sum_of_emails__c+m.get(tm);
update a1;
}
}
}
for(task t:trigger.new)
{
if(m1.get(t.Five9__Five9CallType__c)!=null && t.Five9__Five9CallType__c!=null )
count1=m1.get(t.Five9__Five9CallType__c);
count1++;
system.debug(count+'$$$$$');
m1.put(t.Five9__Five9CallType__c,count1);
for(String tm1: m1.keyset())
{
if(m1.get(t.Five9__Five9CallType__c)!=null && t.Five9__Five9CallType__c!=null )
a1.Sum_of_calls__c=a1.Sum_of_calls__c+m1.get(tm1);
update a1;
}
}
for(task t:trigger.new)
{
if(t.CallDurationInSeconds!=null && t.Five9__Five9CallType__c!=null )
a1.Sum_of_call_durations__c=a1.Sum_of_call_durations__c+t.CallDurationInSeconds;
update a1;
}
}
But i wrote test class for this trigger as follows.
@isTest
public class activitytestcls
{
public static testMethod void verifyaccdetails()
{
task t=new task()
//t.Owner='William Raney';
t.Status='Not Started';
t.Priority='Normal';
t.Subject='Email';
t.Five9__Five9CallType__c='Test';
insert t;
account account a=[select id,Sum_of_emails__c,Sum_of_calls__c,Sum_of_call_durations__c from account where id =:t.id];
update a;
}
}
But it showing an one failure like as follows.
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ActivityReportTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.ActivityReportTrigger: line 25, column 1: [>
How to fix this.Any one can u please help me this.
Thansk in advance.
Try following changes
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Hi ,
Please use this code:
trigger ActivityReportTrigger on Task (before insert)
{
account a1;
integer count=0;
integer count1=0;
list<Account> acclist= new list<Account>();
Map<String,Integer> m= new Map<String,Integer>();
Map<String,Integer> m1= new Map<String,Integer>();
set<id> s= new set<id>();
for(task t:trigger.new)
s.add(t.whatid);
for(account a:[select id,Sum_of_emails__c,Sum_of_calls__c,Sum_of_call_durations__c from account where id in:s])
a1=a;
for(task t:trigger.new)
{
if(m.get(t.Subject)!=null && t.Subject!=null && t.Subject=='Email')
count=m.get(t.Subject);
count++;
system.debug(count+'$$$$$');
m.put(t.Subject,count);
for(String tm: m.keyset())
{
if(m.get(t.Subject)!=null && t.Subject!=null && t.Subject=='Email' && a1 !=null )
{
a1.Sum_of_emails__c=a1.Sum_of_emails__c+m.get(tm);
update a1;
}
}
}
for(task t:trigger.new)
{
if(m1.get(t.Five9__Five9CallType__c)!=null && t.Five9__Five9CallType__c!=null )
count1=m1.get(t.Five9__Five9CallType__c);
count1++;
system.debug(count+'$$$$$');
m1.put(t.Five9__Five9CallType__c,count1);
for(String tm1: m1.keyset())
{
if(m1.get(t.Five9__Five9CallType__c)!=null && t.Five9__Five9CallType__c!=null && a1 !=null)
a1.Sum_of_calls__c=a1.Sum_of_calls__c+m1.get(tm1);
update a1;
}
}
for(task t:trigger.new)
{
if(t.CallDurationInSeconds!=null && t.Five9__Five9CallType__c!=null )
a1.Sum_of_call_durations__c=a1.Sum_of_call_durations__c+t.CallDurationInSeconds;
update a1;
}
}
But i wrote test class for this trigger as follows.
@isTest
public class activitytestcls
{
public static testMethod void verifyaccdetails()
{
Profile p = [select id from profile where name='System Administrator'];
User u1 = new User(alias = 'standt2', email='standarduser@test12.com',
emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,firstname='Heather',
timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');
insert u1;
account a=new account(name='test',Sum_of_emails__c=10,Sum_of_call_durations__c=10);
insert a; // assign the desire value for Sum_of_emails__c, Sum_of_call_durations__c if field type //is not of number type.
task t=new task();
t.Ownerid=u1.id;
t.Status='Not Started';
t.Priority='Normal';
t.Subject='Email';
t.Five9__Five9CallType__c='Test';
t.whatid=a.id;
insert t;
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.