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
Sami ShakithSami Shakith 

Test class for update trigger

Hi, I am new to salesforce. I dont know how to write a test class for update trigger. I need some one help. Just please give a simple example for test class to update a trigger.
Best Answer chosen by Sami Shakith
Ramanarayanan PadmanabhanRamanarayanan Padmanabhan
Hi Sami,

Assuming you have a update trigger on Account, here is a sample example how you can cover the update part of trigger.

Test Method:
Sample Update Method:
static testMethod void UpdateAccount()
{
   Account acc = new Account();
  acc.Name = 'Test Update';
  acc.Mobile = '12345';
  insert acc;
  acc.Country = 'India';
  update acc;
}

Thanks,
Ram

All Answers

PratikPratik (Salesforce Developers) 
Hi Sami,

If you have trigger which is on update event, then in the test class you can actully create a sample record of that object and update that record so it will fire the trigger.

Here you go:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm

Thanks,
Pratik
Ramanarayanan PadmanabhanRamanarayanan Padmanabhan
Hi Sami,

Assuming you have a update trigger on Account, here is a sample example how you can cover the update part of trigger.

Test Method:
Sample Update Method:
static testMethod void UpdateAccount()
{
   Account acc = new Account();
  acc.Name = 'Test Update';
  acc.Mobile = '12345';
  insert acc;
  acc.Country = 'India';
  update acc;
}

Thanks,
Ram
This was selected as the best answer
Sami ShakithSami Shakith
Is this also same test class like insert?
Ramanarayanan PadmanabhanRamanarayanan Padmanabhan
Yes, the same you need to update the record you inserted.

Please mark this anwers as correct if its helpful.

Thanks,
Ram
Sami ShakithSami Shakith
Thanks a lot.