• vedika
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
I have a requirement to create visual force page which takes contact Id from URL and create related record . Below code does'nt give any error but also not save given input values into record though the related record is created with empty fields. Any help is highly appreciated

public with sharing class ChildContactControllerApex {
    public Contact mycontact {get; set;}
    public Emergency_Contact_Information__c Econ{get; set;}
        public ChildContactControllerApex(ApexPages.StandardController controller) {
        Id id = ApexPages.currentPage().getParameters().get('id');
        system.debug(id);
        mycontact =[SELECT Id, FirstName,LastName From Contact WHERE Id = :id];
                                                     
    }
    
    public contact getcontact()
    {
        return mycontact;
        
    }
    
    public Emergency_Contact_Information__c getEcon()
    {
        return Econ;
     }
    
    
    public PageReference save()
    {
        try{
            Emergency_Contact_Information__c EmergencyContact = new Emergency_Contact_Information__c();
            EmergencyContact.Contact__c= mycontact.Id;
            Insert EmergencyContact;
            }
        catch (DMLException e) {
       System.debug('Exception occured : ' + e.getMessage());
     }
       return null;
    }}



<apex:page controller="EmergencyControllerApex" tabStyle="Contact">
    <apex:form >
            <apex:pageBlock mode="edit">
            <apex:pageMessages />
            <apex:pageBlockSection >
                <apex:outputField value="{!Contact.FirstName}"/><br></br>
                <apex:outputField value="{!Contact.LastName}"/><br></br>
                <apex:inputField value="{!Contact.Emergency_Contact_Name__c}"/><br></br>
                <apex:inputField value="{!contact.Emergency_Contact_Relationship__c}"/><br></br>
                <apex:inputField value="{!Contact.Emergency_Contact_Phone__c}"/><br></br>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
  • November 29, 2020
  • Like
  • 0
Hi , 

I need help in the below code and I am thankful to any suggestion you give in here to correct the code
I have a requirement to create a visualforce page which takes the contactId from the URL and create new Emergency Contact Information__c(custom object ) related record  to the contact record.  The contact and  Emergency Contact Information__c are in master detail relationship.

public with sharing class ChildContactControllerApex {
    public Contact mycontact {get; set;}
    private List<Emergency_Contact_Information__c> listToInsert= new List<Emergency_Contact_Information__c>();
         public ChildContactControllerApex(ApexPages.StandardController controller) {
    }
     
    public pagereference save()
    {
          String ContactId = ApexPages.CurrentPage().getparameters().get('Id');
           List<Contact> cnt = New List<Contact>([SELECT Id, Name ,(Select Name , Emergency_Contact_Relationship__c, Emergency_Contact_Phone__c from Emergency_Contact_Informations__r)
                                  from Contact WHERE Id = :ContactId ]);
        if(!cnt.isEmpty())
        {
          if(cnt[0].Emergency_Contact_Informations__r.size()>0){
             cnt[0].Emergency_Contact_Informations__r[0].Contact__c= contactId;
              cnt[0].Emergency_Contact_Informations__r[0].Emergency_Contact_Relationship__c=mycontact.Emergency_Contact_Relationship__c;
              cnt[0].Emergency_Contact_Informations__r[0].Emergency_Contact_Phone__c=mycontact.Emergency_Contact_Phone__c;
               update cnt;
            }
           else
           {
            Emergency_Contact_Information__c EmergencyContact = new Emergency_Contact_Information__c();
            EmergencyContact.Contact__c= mycontact.Id;
            EmergencyContact.Emergency_Contact_Relationship__c= mycontact.Emergency_Contact_Relationship__c;
            EmergencyContact.Emergency_Contact_Phone__c =  mycontact.Emergency_Contact_Phone__c;
              listToInsert.add(EmergencyContact);
            }
            if(listToInsert.size()>0){
            Insert listToInsert;
            }
          }
            return null;
    }}
    
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:inputField value="{!mycontact.Emergency_Contact_Informations__r.Name}"/><br></br>
     <apex:inputField value="{!mycontact.Emergency_Contact_Informations__r.Emergency_Contact_Relationship__c}"/><br></br>
     <apex:inputField value="{!mycontact.Emergency_Contact_Informations__r.Emergency_Contact_Phone__c}"/><br></br> 

</apex:pageBlockSection >

<apex:pageblockbuttons >
     <apex:commandButton value="Save" Action="{!Save}"/>
</apex:pageblockbuttons>

</apex:pageBlock>
</apex:form>
</apex:page>
  • November 29, 2020
  • Like
  • 0
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; 
            }
           
         
         

}