• Ian Lin 585
  • NEWBIE
  • 15 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

Ian 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   

Ian 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   
public with sharing class HIV_UserTriggerHandler {
    
    public static void onAfterInsert(List<User> listUserNew){
        HIV_ApexSharing.addMembersToPublicGroup(listUserNew);
        //HIV_ApexSharing.insertContactShareForCP (listUserNew);
    }
}