You need to sign in to do that
Don't have an account?
bela
please help me for the following test class
I have wrote a trigger that display error if rating ==hot,for this trigger I wrote testclass but I am getting testclass fail showing following error.
and it is also showing other trigger Trigger.updateAccount,why?can anyone help me
System.AssertException: Assertion Failed: Expected: The Last Name "hot" is not allowed for DML, Actual: updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateAccount: line 2, column 1
Trigger:
----------
trigger adderror on Account (before delete) {
for(Account acc:trigger.old)
{
if(acc.Rating=='hot')
{
acc.addError('Account associtated. Cannot delete this record');
}
}
testclass
-------------
@isTest
public class TestAddError{
static testMethod void errorMethod(){
Account acccc=new Account(Rating='hot');
Database.saveResult sr= Database.insert(acccc,false);
System.assertEquals('The Last Name "'+acccc.rating+'" is not allowed for DML',sr.getErrors()[0].getMessage());
}
}
}
and it is also showing other trigger Trigger.updateAccount,why?can anyone help me
System.AssertException: Assertion Failed: Expected: The Last Name "hot" is not allowed for DML, Actual: updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateAccount: line 2, column 1
Trigger:
----------
trigger adderror on Account (before delete) {
for(Account acc:trigger.old)
{
if(acc.Rating=='hot')
{
acc.addError('Account associtated. Cannot delete this record');
}
}
testclass
-------------
@isTest
public class TestAddError{
static testMethod void errorMethod(){
Account acccc=new Account(Rating='hot');
Database.saveResult sr= Database.insert(acccc,false);
System.assertEquals('The Last Name "'+acccc.rating+'" is not allowed for DML',sr.getErrors()[0].getMessage());
}
}
}
NOTE:- Once you are doing insert in your test class then your "updateAccount" trigger is executing and when you are doing delete then "adderror" trigger is exexuting.
Please check below post for trigger context variable
1) http://amitsalesforce.blogspot.in/2015/10/trigger-context-variables.html
Trigger.old Returns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.
Issue is that you are trying to use Trigger.old in before insert
Please update your trigger like below
OR change like below
Let us know if this will help you
All Answers
Let us know if this will help you
Thanks
Amit Chaudhary
thanks for the response.I have used your code but I am getting following errors.I also want test for addError() amit,can you please help me.
I have updateaccount trigger also and it is active then i am getting following error.why?
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateAccount: line 2, column 1: []
If updateaccount trigger is deactive then I am getting following error.so I am getting following error
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Class.TestAddError.errorMethod: line 9, column 1
Thanks,
B.Mahanandeesh,
your code is working fine,but I want test for adderror(); and can you explain the following error ,why it is coming?and how to overcome this with making updateaccount deactive
I have updateaccount trigger also and it is active then i am getting following error.why?
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null objectTrigger.updateAccount: line 2, column 1: []
It look like some issue is coming in your trigger "updateAccount". Please deactivate "updateAccount" trigger and let us know if that will help
the above code with assert is working fine.
thanks,
B.Mahanandeesh.
Can you please explain the following issue and doubts.
doubts
-------
1.For class if we write test class we will create instance for class in order to call methods and also to mention that this testclass is for the particular class,but for trigger how to mention that this testclass is for particular trigger.
issue:which i mentioned in above a code
-------
I have updateaccount trigger also and it is active then i am getting following error.why?
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null objectTrigger.updateAccount: line 2, column 1: []
thanks,
B.Mahanandeesh,
For Example you create above trigger on Account Delete so for this trigger Test class you need to perform the delete of account record inside your test class.
You are getting error in your updateaccount because there should be some issue in your trigger code. Means Null check is not added in your trigger properly. Please share your trigger code i will try to fix the trigger code as well.
Please check below post to learn test class. i hope that will help you
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Let us know if this will help you
Means we have to write only one trigger for an object,and in that we have to implement whatever DML we want ,is it right .but I wrote different events for each trigger .delete event for adderror trigger and before insert for updateAccount trigger,then why i am getting the following error .can you please explain?
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null objectTrigger.updateAccount: line 2, column 1: []
this is trigger where I am getting above error
-----------------------------------------------------------------------
trigger updateAccount on Account (before insert) {
for(Account a: Trigger.old){
system.debug('TRIGGER.OLD:AccountNAME..,,,,,'+a.name);
system.debug('TRIGGER.OLD:Industry..,,,,,'+a.Industry);
}
for(Account b: Trigger.New){
system.debug('TRIGGER.New:AccountNAME..,,,,,'+b.name);
system.debug('TRIGGER.New:Industry..,,,,,'+b.Industry);
}
Map<id,Account> oldMap=Trigger.oldMap;
Map<id,Account> NewMap=Trigger.NewMap;
system.debug('TRIGGER.oldMAP:NAme..,,,,,'+oldmap);
system.debug('TRIGGER.NewMAP:NAme..,,,,,'+Newmap);
}
thanks,
B.Mahanandeesh.
NOTE:- Once you are doing insert in your test class then your "updateAccount" trigger is executing and when you are doing delete then "adderror" trigger is exexuting.
Please check below post for trigger context variable
1) http://amitsalesforce.blogspot.in/2015/10/trigger-context-variables.html
Trigger.old Returns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.
Issue is that you are trying to use Trigger.old in before insert
Please update your trigger like below
OR change like below
Let us know if this will help you
Thanks for providing your valuable time and making me to understand testclasses.
Thanks,
B.Mahanandeesh.