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
Neeraj Sharma 103Neeraj Sharma 103 

Hi Everyone How to insert a record in custom object

Hi

Everyone How to insert a record in custom object which is under in Contact Standard Object Lookup RelationShip Between them

Attendance__c lookup (Contact)

Thanks and Regards 
neeraj Sharma
Best Answer chosen by Neeraj Sharma 103
Ajay K DubediAjay K Dubedi
Hi Neeraj,

Try the following code:
 
Contact conObj=[SELECT Id,Name FROM Contact LIMIT 10000];
List<CustomObjectName__c> newObjList = new List<Attendance__c>();
integer i=0;
for(Contact c: conObj){
    CustomObjectName__c customObj = new CustomObjectName__c();
    customObj.Name='Test Record'+i;
    customObj.Attendance__c = c.Id;
    newObjList.add(customObj);
    i++;
}
insert newObjList;

This will create custom object records with Contact as parent.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Neeraj,
Greetings to you!

- Replace CustomObjectName__c with your Custom Object Name.
- Lookup always takes the id of record from Object. So I gave contact id in Attendance__c because it is a lookup of contact.
Contact conObj=[SELECT Id,Name FROM Contact LIMIT 1];
CustomObjectName__c customObj = new CustomObjectName__c();
customObj.Name='Test Record';
customObj.Attendance__c = conObj.Id;
insert customObj;

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Neeraj Sharma 103Neeraj Sharma 103
Hi Deepali Kulshrestha

Little Bit changes the Contact Records is a Community User when i login in community as a contact after that insert the record so 
SELECT Id,Name FROM Contact LIMIT 1 this query insert only particular contact so please suggest for all contacts

Thanks
Neeraj sharma
Shyam SwamiShyam Swami
Hi Neeraj,
Greetings to you!

- Replace Custom ObjectName__c with your Custom Object Name.
- Lookup always takes the id of record from Object. So I gave contact id in Attendance__c because it is a lookup of contact.
List<Contact> conObj = [SELECT Id,Name FROM Contact];
List<ObjectName__c> allRecords = new LList<ObjectName__c>();
foreach(Contact cnn:conObj ){
ustomObjectName__c customObj = new CustomObjectName__c();
customObj.Name='Test Record';
customObj.Attendance__c = cnn.Id;
allRecords.add(customObj );
}
insert allRecords;
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Deepali KulshresthaDeepali Kulshrestha
Hi Neeraj,
Greetings to you!

- Okay, Please use the below code for more than one contact.
List<CustomObjectName__c> customObjectList = new List<CustomObjectName__c>();
List<Contact> conList=[SELECT Id,Name FROM Contact LIMIT 10000];
if(!conList.isEmpty()){
    for(Contact con:conList){
        CustomObjectName__c customObj = new CustomObjectName__c();
        customObj.Name='Test Record';
        customObj.Attendance__c = con.Id;
        customObjectList.add(customObj);
    }
}
if(!customObjectList.isEmpty()){
    insert customObjectList;
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Ajay K DubediAjay K Dubedi
Hi Neeraj,

Try the following code:
 
Contact conObj=[SELECT Id,Name FROM Contact LIMIT 10000];
List<CustomObjectName__c> newObjList = new List<Attendance__c>();
integer i=0;
for(Contact c: conObj){
    CustomObjectName__c customObj = new CustomObjectName__c();
    customObj.Name='Test Record'+i;
    customObj.Attendance__c = c.Id;
    newObjList.add(customObj);
    i++;
}
insert newObjList;

This will create custom object records with Contact as parent.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer