You need to sign in to do that
Don't have an account?
vedika
create or update custom object related record for contact object using visualforce page
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>
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>