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
Jha dilipJha dilip 

How to use inserted account returned list record for contact insertion

Hi All,

in the below code i am creating account record which is returning inserted record in list form accountList. now i want to use the account id created in contact record insertion. so please can you let me know how can i use as i want to pass inserted account id to contact  as parameter .
public class AccountTest
    {
    
        public static List<Account> CreateAccount()
            {
            
                List<Account> accountList = new List<Account>();
                
                Account Acc=new Account(Name='Test2');
                accountList.add(acc);  
                insert accountList;
                return accountList;             
            
            }
            
        public static List<Contact> CreateContact(account accid)
            {
                List<Contact> contactList=new List<Contact>();
                Contact con=new Contact(Lastname='Test1',
                AccountId=accid.id);
                contactList.add(con);
                insert contactList;
                return contactList;
                
                
                
            
            }
        
            
    }
Deepak Kumar 138Deepak Kumar 138
Not sure how/where are you planing to call CreateContact method. however i have just modifed your code to use account id from the record inserted in CreateAccount() method. I hope this helps you.

public class AccountTest
    {
    
        public static List<Account> CreateAccount()
            {
            
                List<Account> accountList = new List<Account>();
                
                Account Acc=new Account(Name='Test2');
                accountList.add(acc);  
                insert accountList;
                return accountList;             
            
            }
            
        public static List<Contact> CreateContact(account accid)
            {    
                List<Account> accList = CreateAccount();
                List<Contact> contactList=new List<Contact>();
                Contact con=new Contact(Lastname='Test1',
                con.AccountId = accList[0].id;
                contactList.add(con);
                insert contactList;
                return contactList;               
                
                
            
            }
        
            
    }
Jha dilipJha dilip
Hi Thanks for your response but my scenario is account will be seprate class and contact will be seprate class.
like below

public class AccountTest
    {
    
        public static List<Account> CreateAccount(List<Account>fieldmapvalue)
            {
            
                List<Account> accountList = new List<Account>();
                
                Account Acc=new Account(Name='Test2');
                accountList.add(acc);  
                insert accountList;
                return accountList;             
            
            }
}

public class ContactTest

{
public static List<Contact> CreateContact(List<Contact>fieldvaluemap,account accid)
            {    
                List<Account> accList = CreateAccount();
                List<Contact> contactList=new List<Contact>();
                Contact con=new Contact(Lastname='Test1',
                con.AccountId = accList[0].id;
                contactList.add(con);
                insert contactList;
                return contactList;               
                
                
            
            }

}

now in this case i want to use accountid in contact insertion so please can you suggest
BALAJI CHBALAJI CH
Hi Dilip,

I had a similar scenario and modified your code accordingly. But no idea why you are using Static.
Please find below code.

Class AccountTest:
public class AccountTest
{
    public List<Account> CreateAccount()
    {
        List<Account> accountList = new List<Account>();
        
        Account Acc=new Account(Name='Test2');
        accountList.add(acc);  
        insert accountList;
        return accountList;             
        
    }
}

Class ContactTest:
public class ContactTest
{
    public List<Contact> CreateContact()
    {    
        AccountTest test = new AccountTest(); //Creating an instance for class Account Test
        List<Account> accList = test.CreateAccount(); //Calling CreateAccount method using instance and storing List of Accounts in accList
        
        List<Contact> contactList=new List<Contact>();
        Contact con=new Contact(Lastname='Test1');
        con.AccountId = accList[0].id;
        contactList.add(con);
        insert contactList;
        return contactList;                     
    }   
}
I have tested the code by calling CreateAcontact method and it works fine.
User-added image

Let me know if that works for you or any modifications required.

Best Regards,
BALAJI