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
chaitanya salesforcecrmchaitanya salesforcecrm 

How to Insert data in to Related List on Contact Object

Below is the apex class which i have written to insert data in to related list on contact.i am not getting error while executing the class but the data in not getting saved in the related list object.kindly let me know where i am doing wrong?
 
public class IntralinksExtension {

    public IntralinksExtension(ApexPages.StandardSetController controller) {

    }
         Public Contact Cont{get;set;}
         public String ContactId {get;set;}
         public string IntragroupId {get;set;}

  
Public void Save(){


            ContactId = ApexPages.CurrentPage().getparameters().get('Id');
            IntragroupId = ApexPages.CurrentPage().getparameters().get('ILPG1');
            
            List<Contact> Cnt = New List<Contact>([select Id,Name,(select Id,Name from Intralinks_Group_and_Contact_Links__r)from Contact where Id=:ContactId]);
            if(!Cnt.isEmpty()){
                Cnt[0].Intralinks_Group_and_Contact_Links__r[0].Contact__c = ContactId;
                Cnt[0].Intralinks_Group_and_Contact_Links__r[0].Intralinks_Portal_Group__c = IntragroupId;
            insert Cnt; 
            }
           
         
         

}

 
Naresh YadavNaresh Yadav
Hi chaitanya salesforcecrm,

Check the below code. In your case there are two use cases.
  • Contact already has some records in related list(If yes then update them.)
  • No records in related list(Create new records)

public class IntralinksExtension {

    public IntralinksExtension(ApexPages.StandardSetController controller) {

    }
    Public Contact Cont{get;set;}
    public String ContactId {get;set;}
    public string IntragroupId {get;set;}
    private List<Intralinks_Group_and_Contact_Links__c> listToInsert;
    
    Public void Save(){
        List<Contact> Cnt = New List<Contact>([select Id,Name,(select Id,Name from Intralinks_Group_and_Contact_Links__r)from Contact where Id=:ContactId]);
        if(!Cnt.isEmpty()){
            //  Check if contact has already some records if so then update them else create them
            if(Cnt[0].Intralinks_Group_and_Contact_Links__r.size()>0){
                Cnt[0].Intralinks_Group_and_Contact_Links__r[0].Contact__c = ContactId;
                Cnt[0].Intralinks_Group_and_Contact_Links__r[0].Intralinks_Portal_Group__c = IntragroupId;
                update cnt;
            }else{
                //  insert new record
                listToInsert = new Intralinks_Group_and_Contact_Links__c();
                Intralinks_Group_and_Contact_Links__c newRecord = new Intralinks_Group_and_Contact_Links__c();
                newRecord.Contact__c = ContactId;
                newRecord.Intralinks_Portal_Group__c = IntragroupId;
                listToInsert.add(newRecord);
            }
            if(listToInsert.size()>0){
                insert listToInsert; 
            }
        }
    }
}
vedikavedika
Hi @ Naresh Yadav

Can you please help me with the below code 
IRequirement is as below 
Contact already has some records in related list(If yes then update them.)
No records in related list(Create new records)

public with sharing class ChildContactControllerApex {
    public contact mycontact {get; set;}
    public string contactId{get; set;}
    public string EName{get; set;}
    public string EEmergencyContactRelationship{get; set;}
    public string EEmergencyContactPhone{get; set;}
     
  private List<Emergency_Contact_Information__c> eList = New List<Emergency_Contact_Information__c>();
public ChildContactControllerApex(ApexPages.StandardSetController controller) {}
 
public pageReference Save()
      
      Cont = [Select Id, FirstName,LastName,(Select Name,Emergency_Contact_Relationship__c,Emergency_Contact_Phone__c from 
                                                                              Emergency_Contact_Informations__r)from Contact Where Id='0030R000012RtfcQAC'];
      if(!Cont.isEmpty()){
          //Check if contact has already some records if so then update them else create them
          if(cont[0].Emergency_Contact_Informations__r.size()>0)
          {
              cont[0].Emergency_Contact_Informations__r[0].contact__c= '0030R000012RtfcQAC';
              cont[0].Emergency_Contact_Informations__r[0].Name=EName;
              cont[0].Emergency_Contact_Informations__r[0].Emergency_Contact_Relationship__c= EEmergencyContactRelationship;
              cont[0].Emergency_Contact_Informations__r[0].Emergency_Contact_Phone__c =  EEmergencyContactPhone;
              update con;
            }else{
              //Insert new record 
              Emergency_Contact_Information__c createNew = New Emergency_Contact_Information__c();
              createNew.Contact__c = '0030R000012RtfcQAC';
              createNew.Name = EName;
              createNew.Emergency_Contact_Relationship__c = EEmergencyContactRelationship;
              createNew.Emergency_Contact_Phone__c =  EEmergencyContactPhone;
              eList.add(createNew);
          }
     if(eList.size()>0){
      Insert eList;
         }
       }  
   }

     public contact getcontact()
    {
        return mycontact;
      
    }
    
}



Visualforce page 

<apex:page standardController="Contact" extensions="ChildContactControllerApex">
<apex:form >
<apex:pageBlock title="Please Update Emergency Contact Information" mode="edit">
<apex:pageBlockSection >
     <apex:outputField value="{!mycontact.FirstName}"/><br></br>
     <apex:outputField value="{!mycontact.LastName}"/><br></br>
     <apex:inputText label="Emergency Contact Name" value="{!EName}"/><br></br>
     <apex:inputText label="Emergency Contact Relationship" value="{!EEmergencyContactRelationship}"/><br></br>
     <apex:inputText label="Emergency Contact Phone" value="{!EEmergencyContactPhone}"/><br></br>
</apex:pageBlockSection>
<apex:pageblockbuttons >
     <apex:commandButton value="Save" Action="{!Save}"/>
</apex:pageblockbuttons>
</apex:pageBlock>
</apex:form>
</apex:page>