function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
reema_agarwalreema_agarwal 

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

Navatar_DbSupNavatar_DbSup

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.