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
MobileMobile 

i have created a a trigger but need a testmethod for it. Helppppppp.

Hi, i'm fairly new to the apex world, the below trigger works fine on the sandbox but in order to deploy it i need a testmethod for it. it would be appreciated if someone could help create a test method for it.

 

thanks in advance. 

 

 trigger CheckDuplicateAccountOnPhone on Account (before insert) {

Account[] accs = Trigger.new;

 

    for (Account a:accs){

     Integer hits;

     String matchCond = a.phone;

System.debug(matchCond);

hits = [SELECT count() FROM Account WHERE phone = :matchCond];

System.debug(hits); 

     if (hits>0)

     {

        trigger.new[0].addError('Account already exists in Database, Please Contact Your Admin To Give you access to it.  ');

     }       

   }

}

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
shillyershillyer

Here is a lead dedup example that is similar. And here is the corresponding test method. Perhaps this can help you form yours.

 

Hope that helps,

Sati

All Answers

shillyershillyer

Here is a lead dedup example that is similar. And here is the corresponding test method. Perhaps this can help you form yours.

 

Hope that helps,

Sati

This was selected as the best answer
jrotensteinjrotenstein

The example given by shillyer is good for two reasons:

  • It is bulk-safe, meaning that it will not exceed Governor Limits when multiple Accounts are passed to the trigger. It only ever makes on SELECT statement
  • Performing SELECT COUNT(*) is dangerous, since it could return too many rows and, therefore, exceed Governor limits. The bulk-safe "one call" method is best, or if you had to make individual SELECT calls you could use "limit 1" instead of COUNT(*), which would only count one result towards the Governor Limit
MobileMobile

thanks John/Shillyer, i end up using What Shiller referred me to and changed obviously from the lead object to the account object and checked on phone instead of email.

 

Really appreciate your help, thanks again.