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

Test method for a trigger
Hello
I have written a trigger on asset name that it should be unique with no duplicate values I want to wrrite a test class 4 it can someone please help me.
The trigger is as below:
trigger NameTrigger on Asset (before insert)
{
list <Asset> al = new List <Asset>();
al = [select Name from Asset];
if(Trigger.isInsert)
{
For(integer i=0;i<al.size();i++)
{
for(Asset a2 : Trigger.New)
{
if(al[i].Name == a2.Name)
{
a2.Name.addError('This value already Exist');
}
}
}
}
}
Thanks
Hi,
Try the below code snippet as reference:
@istest
public class SCampaignTestCls
{
static testmethod void testSCampaign()
{
Asset obj = new Asset();
obj.name = 'testname';
insert obj;
Asset obj2 = new Asset();
obj2.name = 'testname'
insert obj2;
// note plese write the code for required field in Asset object
// example: obj2.requiredfield = 'proper value';
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.