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
Sunny GSunny G 

Need help to write test class for the trigger

Please provide any test class that will cover atleast 75% code area for the following trigger

 

trigger Duplicate on Account (before insert, before update)
{

    Map<String, Account> accountMap = new Map<String, Account>();
   
   
    for (Account account : System.Trigger.new)
    {
        //expected to execute when converting leads to account
        String tempAccountSite = '';
        if (account.Site == null || account.Site == '')
        {
            account.Site = account.Lead_Site__c;
           
            //force the formula of Account_Site_c since mapping of Lead Site to Account Site
            //occurs after this trigger
            tempAccountSite = account.Name + account.Lead_Site__c;
        }
       
        // Make sure we don't treat an Account_Site that is
        // not changing during an update as a duplicate.
        if ((account.Account_Site__c != null) && (System.Trigger.isInsert ||
            (!account.Account_Site__c.EqualsIgnoreCase(System.Trigger.oldMap.get(account.ID).Account_Site__c))))
        {
            // Make sure another in the batch is not a duplicate
            if (accountMap.containsKey(account.Account_Site__c.ToLowerCase()))
            {
                account.addError('A Account is a duplicate - Account Name and Account Site already exists');
            }
            else if (tempAccountSite != '')
            {
                accountMap.put(tempAccountSite.ToLowerCase(), Account);
            }
            else
            {
                accountMap.put(account.Account_Site__c.ToLowerCase(), Account);
            }
        }
    }

    // With a single database query, find all the accounts in
    // the database that have the same name and site as any
    // of the accounts being inserted or updated.
    for (Account account : [SELECT Account_Site__c from Account WHERE Account_Site__c IN :accountMap.KeySet()])
    {
        Account newAccount = accountMap.get(account.Account_Site__c.ToLowerCase());
       
        if (newAccount != null)
        {
            newAccount.addError('B Account is a duplicate - Account Name and Account Site already exists');
        }
    }
}

 

 

SurekaSureka

Hi,

 

You can make use of the below test method:

 

 

@isTest
private class CLS_TST_AccountTrigger
{
    static testMethod void mthd_AccountTrigger()
    {
        Account obj_Account = new Account();
        obj_Account.name = 'Test Account 1';
        obj_Account.Account_Site__c = 'www.test.com';
        insert obj_Account;
        List<Account> lst_Account = new List<Account>{};
        
        for(Integer i = 0; i < 200; i++)
        {
            Account obj_Accounts = new Account(Name = 'Test Account ' + i);
            lst_Account.add(obj_Accounts);
        }
        
        insert lst_Account;
        
        for(Integer i = 0; i < lst_Account.size(); i++)
        {
            if(i<=10)
                lst_Account[i].Account_Site__c = null;
            else
                lst_Account[i].Account_Site__c = 'www.test.com';
        }
        try
        {
            update lst_Account;
        }
        catch(Exception e)
        {
        }
    }
}    

 

 

Hope this helps.

 

Thanks

Sunny GSunny G

Hi Sureka,

 

I am afraid to get an answer of this question on this portal.. pls help

 

It's a new custom object created in sandbox.. and i am trying to deploy it to production. I have the following trigger and for this trigger i have written the following text class... the code coverage is 100% in sandbox. But when i tried deploying it to production it gives me an error saying  "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required" please help me with that..

 

 

 

trigger updaterequestowner on Pre_Sales_Request__c​ (before insert)
{

       Profile auth_profile = [select id from prof​ile where name='HS_StandardUser_Presales'];       ​   
       Id v_User = UserInfo.GetProfileId();
      for(Pre_Sales_Request__c ob : Trigger.new)
        {          
            if(v_User !=  auth_profile.Id)       
                {
                   ob.Request_Owner__c =  Userinfo​.getUserId(); 
                }
        }                           
}

 

and i have the following test class :

 

@isTest

private class TriggerTests {
            
    public static testmethod void testTrigger() {
       Pre_Sales_Request__c psrc=new Pre_Sales_R​equest__c( name = 'a' );
       insert psrc;
       psrc.name = 'b';
       update psrc;  

    }
}