You need to sign in to do that
Don't have an account?
Gyanaloka 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);
}
}
public static void onAfterInsert(List<User> listUserNew){
HIV_ApexSharing.addMembersToPublicGroup(listUserNew);
//HIV_ApexSharing.insertContactShareForCP (listUserNew);
}
}
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:
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.
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