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
Samantha ReddySamantha Reddy 

Adding the Trigger in the Apex class in Salesforce

UseCase In Account Object:

Created a Trigger when the ParentAccount is selected in account object ,need to copy  the phone number and name and insert the record.
how can i add the Trigger in the Apex Class and Write Test Class...
 
trigger CopyAccountTriggerTest on Account (before insert) {
  
   
    if(checkRecursiveTest.runOnce()) {
        Set<Id> accountIds = new Set<Id>();
        for (Account acc : Trigger.new) {
            if (acc.ParentId != null){
                accountIds.add(acc.ParentId);
            }
            if(accountIds.size() > 0){  
                  List<Account> accounts = [select id,Name,Phone from account where id in :accountIds ];
                
                for (Account acc2 : Trigger.new) {
                    if (acc2.ParentId != null){
                        acc2.Name = 'Duplicate'+accounts[0].Name;
                        acc2.Phone= 'Duplicate'+accounts[0].Phone;
                    }
                }
            }
        }
 }

  
 
    
}



Apex Class:
=========
 
public class checkRecursiveTest {
    private static boolean run = true;
    public static boolean runOnce(){
     if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

 
Best Answer chosen by Samantha Reddy
Abdul KhatriAbdul Khatri
Sai Praveen you are right this is duplicate but putting same here


Here is the code for you reference with the Test class 100% Coverage. You do not need checkRecursiveTest.runOnce since you are running on before Insert so not including that in my code.
 
trigger CopyAccountTriggerTest on Account (before insert) {
    
    Map<Id, Account> qualifiedAccounts = new Map<Id, Account>();
    List<Account> qualifiedAccount = new List<Account>();
    for(Account acc : Trigger.new)
    {
        if(acc.ParentId != null)
        {
            qualifiedAccounts.put(acc.ParentId, acc);
        }
    }
    
    if(!qualifiedAccounts.isEmpty())
    {
        
        Map<Id, Account> parentAccountMap = new Map<Id, Account>([SELECT Name, Phone FROM Account WHERE Id = :qualifiedAccounts.keySet()]);
        
        for(Account acc2 : qualifiedAccounts.values())
        {
            if(parentAccountMap.get(acc2.ParentId) != null){
                acc2.Name = 'Duplicate'+parentAccountMap.get(acc2.ParentId).Name;
				acc2.Phone = 'Duplicate'+parentAccountMap.get(acc2.ParentId).Phone;
            }
        }       
    }

}

Test Class
@isTest
public class CopyAccountTriggerTest {

    static testMethod void testCopyAccountTrigger() {
        
        Account parentAccount = new Account (Name = 'Test Parent Account', Phone = '11122233333');
        Insert parentAccount;

		Test.startTest();        
        Account account = new Account (Name = 'Test Account', ParentId = parentAccount.Id);
        insert account;
        Test.stopTest();
        
        Account accountAfterInsert = [SELECT Name, Phone FROM Account WHERE Id = :account.Id];
        System.assert(accountAfterInsert.Name.contains('Duplicate'));
        System.assert(accountAfterInsert.Phone.contains('Duplicate'));

    }
    
}

I hope this will help

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Samantha,

This seems to be duplicate for the other question . Can you close this question by marking this as best answer so it won't be duplicate efforts for others.

Thanks,
 
Abdul KhatriAbdul Khatri
Sai Praveen you are right this is duplicate but putting same here


Here is the code for you reference with the Test class 100% Coverage. You do not need checkRecursiveTest.runOnce since you are running on before Insert so not including that in my code.
 
trigger CopyAccountTriggerTest on Account (before insert) {
    
    Map<Id, Account> qualifiedAccounts = new Map<Id, Account>();
    List<Account> qualifiedAccount = new List<Account>();
    for(Account acc : Trigger.new)
    {
        if(acc.ParentId != null)
        {
            qualifiedAccounts.put(acc.ParentId, acc);
        }
    }
    
    if(!qualifiedAccounts.isEmpty())
    {
        
        Map<Id, Account> parentAccountMap = new Map<Id, Account>([SELECT Name, Phone FROM Account WHERE Id = :qualifiedAccounts.keySet()]);
        
        for(Account acc2 : qualifiedAccounts.values())
        {
            if(parentAccountMap.get(acc2.ParentId) != null){
                acc2.Name = 'Duplicate'+parentAccountMap.get(acc2.ParentId).Name;
				acc2.Phone = 'Duplicate'+parentAccountMap.get(acc2.ParentId).Phone;
            }
        }       
    }

}

Test Class
@isTest
public class CopyAccountTriggerTest {

    static testMethod void testCopyAccountTrigger() {
        
        Account parentAccount = new Account (Name = 'Test Parent Account', Phone = '11122233333');
        Insert parentAccount;

		Test.startTest();        
        Account account = new Account (Name = 'Test Account', ParentId = parentAccount.Id);
        insert account;
        Test.stopTest();
        
        Account accountAfterInsert = [SELECT Name, Phone FROM Account WHERE Id = :account.Id];
        System.assert(accountAfterInsert.Name.contains('Duplicate'));
        System.assert(accountAfterInsert.Phone.contains('Duplicate'));

    }
    
}

I hope this will help
This was selected as the best answer