+ Start a Discussion

Simple Trigger and Code Coverage - Update an OwnerID from a lookup field

Hi all,


I am very new to Apex and have written my first Trigger to Update an OwnerID from a custom lookup field. Both fields on the same Merchandise__c custom object. Although a very simple trigger it works in my developer org. I have a couple of questions.. I would be more than grateful.. if anyone can kindly assist.


1. Is it safe to have such simple code for a Trigger (re: governor limits and endless loops etc)? Should I add anything else?


2. Can anyone help with the test code for this trigger please?


Thanks in advance :smileyhappy:


trigger UpdateOwner on Merchandise__c (before insert, before update) {
    for (Merchandise__c a: Trigger.New){
    a.Ownerid= a.TriggerUser__c;
Chamil MadusankaChamil Madusanka

1. Your code  is safe. There is no issues with governor limit

     Refer following link to understand best practice on Apex trigger


2. You can write simple test class for this trigger.

     In your test class insert a Merchandise__c record

     Then update your test record in test class




That's it :)


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.