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
himanshu huske 7himanshu huske 7 

contact obj

How to insert value in Salutation, FirstName, Lastname, Account(LookUp) in contact object using Apex, help me with code
Best Answer chosen by himanshu huske 7
Madhukar_HeptarcMadhukar_Heptarc
Hi himansu,

I have understanrd your requirement.
In below i have provided Apex Code for inserting the Contact object records from Apex class.
public class ContactRecordInsertion {

    public void Data()
    {
        // Creating New Account To provide Account Lookup in Contact 
        Account newAccount = new Account ();
        
        newAccount.name = 'AcName';
        newAccount.BillingCity ='TestCity';
        newAccount.BillingCountry ='TestCountry';
        newAccount.BillingStreet ='TestStreet';
        
        insert newAccount;
        
        // Creating New Contact record.
        Contact Con = new Contact();
        
        Con.Salutation = 'Mr.';
        Con.FirstName = 'Community User';
        Con.Lastname = 'Developer';
        Con.AccountId = newAccount.Id ; //Account Lookup
        
        Database.insert(Con,true);
        System.debug(Con);   
    }
}
Please let me know If it helps you.

Thanks & Regards
Madhukar_heptarc
 

All Answers

Madhukar_HeptarcMadhukar_Heptarc
Hi himansu,

I have understanrd your requirement.
In below i have provided Apex Code for inserting the Contact object records from Apex class.
public class ContactRecordInsertion {

    public void Data()
    {
        // Creating New Account To provide Account Lookup in Contact 
        Account newAccount = new Account ();
        
        newAccount.name = 'AcName';
        newAccount.BillingCity ='TestCity';
        newAccount.BillingCountry ='TestCountry';
        newAccount.BillingStreet ='TestStreet';
        
        insert newAccount;
        
        // Creating New Contact record.
        Contact Con = new Contact();
        
        Con.Salutation = 'Mr.';
        Con.FirstName = 'Community User';
        Con.Lastname = 'Developer';
        Con.AccountId = newAccount.Id ; //Account Lookup
        
        Database.insert(Con,true);
        System.debug(Con);   
    }
}
Please let me know If it helps you.

Thanks & Regards
Madhukar_heptarc
 
This was selected as the best answer
Madhukar_HeptarcMadhukar_Heptarc
Hi Himanshu,

 
Excecution Steps for Above Class :
==========================

Open Your developer console ==> Click on Debug ==> On the dropdown list you will see Open Excecute annoymous Window option ==> Click on that.


Paste below 2 lines on the window:

ContactRecordInsertion N= new ContactRecordInsertion();
N.Data();


Then Click on Excecute.


Now your contact record is created on the Contact object. Check in your contact object.

Thanks & Regards 
Madhukar_heptarc