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 

How to write the test method for the trigger to deploy

Hi,

 

I have the following trigger to deactivate, i do not know how to write the test method for this, can anyone please help me:

 

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);
            }
        }
    }


    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');
        }
    }
}

AshishGargAshishGarg

hi..