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
Gyanaloka Panda 8Gyanaloka Panda 8 

Test class of below apex class ?

public with sharing class HIV_UserTriggerHandler {
    
    public static void onAfterInsert(List<User> listUserNew){
        HIV_ApexSharing.addMembersToPublicGroup(listUserNew);
        //HIV_ApexSharing.insertContactShareForCP (listUserNew);
    }
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Gyanaloka,

You need to insert records and the handler will automatically be called from trigger.

>> https://salesforce.stackexchange.com/questions/248465/testclass-for-triggerhandler

The above link has a test class for handler you can try checking it I am adding the mentioned test class below for quick reference:
@isTest
public class opportunityFactoryTriggerHandler {
    @isTest
    public static void testDoAfterUpdate() {
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opp';
        opp.CloseDate=System.Today();
                opp.StageName='Prospect';
        
        insert opp;
        
        Meeting__c me = new Meeting__c();
        me.Name = 'Test Meeting';
        me.Opportunity__c = opp.Id;
        insert me;
        
        opp.StageName = 'Closed Won';
        update opp;
    }
}

Additionally, you can try checking this below implementation as well: https://matheus.dev/write-advanced-test-class-advanced-trigger/

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
Ian Lin 585Ian Lin 585
Can anyone answer to the following scenario?. I need to create a account after a contact is created with the contact name as account name. If the account name with the same contact name exists assign the accountId to Id of existing account otherwise create a account with the name of the contact name and assign the new account id to contact.
My code is :
trigger CreateDefaultContact on Contact (after insert) {
       ContactDefault.createContact(trigger.new);
}

public class ContactDefault {
    
    public static void createContact(List<Contact> contactList)
    {
            List<Contact> needAcctsLst = new List<Contact>();
        for(Contact c : contactList){
            needAcctsLst.add(c);
        }
            List<Account> accountList = new List<Account>();
            List<Contact> conLst = new List<Contact>();
            for(Contact conObj : needAcctsLst)
            {
                String cname=conObj.FirstName+' '+conObj.LastName;
                Account acc = [select Id,name from Account where name=:cname LIMIT 1];
                if (acc!=null && acc.Id!=null){
                    System.debug( 'inside if ');
                    conObj.AccountId = acc.Id;
            }else{
                Account accObj = new Account();
                accObj.Name = cname;
                conObj.AccountId = accObj.Id;
                accountList.add(accObj);
                System.debug( 'inside else ');
            }
                
                conLst.add(conObj);
            }
            
            if(accountList.size() > 0)
            {
                insert accountList;
                
            }
           if(conLst.size() > 0)
            {
                System.debug( 'updating contact ');
                update conLst;
            }
    }
 }

@isTest
public class ContactDefaultTest {
    @testSetup
    static void testSetUpData() 
    {
        Account testAccount = new Account();
        testAccount.Name='aaa ttt' ;
        insert testAccount;
    }
    @istest
    public static void testMethod1(){
        Contact cont = new Contact();
        cont.FirstName='aaa';
        cont.LastName='ttt';
        insert cont;
        String cname=cont.FirstName+' '+cont.LastName;
        Account acc = [select Id,name from Account where name=:cname LIMIT 1];
        System.assertEquals(acc.Id,cont.AccountId,'Equals .. passed');
    }
}

It throws the below error. Can anyone help to fix this issue.?
Thanks.
14:59:37:160 EXCEPTION_THROWN [15]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateDefaultContact: execution of AfterInsert
14:49:50:170 FATAL_ERROR System.FinalException: Record is read-only