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
Manju_BNManju_BN 

Updating the data in two objects

Hi All,

 

I have created a VF page. The main requirement is when ever the data is entered and saved, then some fields should be saved in one object and the remaining in other object....

 

Can anyone pls help in writing the controller as per the requirement....

 

Thanks,

 

Manjunath BN

 

Sridhar BonagiriSridhar Bonagiri

Hi,

 

Create a controller class , add the code to the extent of your knowledge, let me know once it is done, I will help you in finishing the remaining thing.

 

Regards,

Sridhar Bonagiri

SFDC_LearnerSFDC_Learner

We can do many ways for this. One of the way is below.

 

Page:

 

<apex:page controller="MultipleInsert">
  <apex:form >
      <apex:pageBlock mode="edit">
          <apex:pageblocksection columns="1" title="Account Insert" >
              <apex:inputfield value="{!objAcc.Name}"/>
          </apex:pageblocksection>
          
          <apex:pageblocksection columns="1" title="Contact Insert" >
              <apex:inputfield value="{!objCon.lastName}"/>
          </apex:pageblocksection>
          
      <apex:commandButton value="Save" action="{!doSave}" style="margin-left:200px;"/>
      </apex:pageBlock>
 
  </apex:form>
</apex:page>
 
Class:
 
public with sharing class MultipleInsert {
 
    public PageReference doSave() {
        insert objAcc;
        insert objCon;
        return (new pagereference('/apex/MultipleInsertPage').setredirect(true));
    }
 
    public Account objAcc{get;set;}
    public Contact objCon{get;set;}
    
    public MultipleInsert(){
        objAcc = new Account();
        objCon = new Contact();
    }
}
Manju_BNManju_BN

Hi Sridhar,

 

Here is my controller....

 

public class ContactUpdateController {

    public PageReference cancel() {
        return null;
    }


    public ContactUpdateController() {

    }


    public Shipping_Vendor__c vendor {get; private set;}   
    public Shipping_Vendor_Contact__c contact {get; private set;}

    public ContactUpdateController(ApexPages.StandardController controller) {
        vendor=new Shipping_Vendor__c();     
        contact=new Shipping_Vendor_Contact__c();
    }
    
    public boolean getRecordNameRetrieval(){
        String recordTypeName=ApexPages.currentPage().getParameters().get('RecordType.Name');    
        if(recordTypeName.equals('B2C'))       
        return true;          
        
        return false;     
        }
                
    public PageReference save() {
    try{                       
     insert(vendor);         
     }       
     
     catch(System.DMLException e){           
         ApexPages.addMessages(e);
         return null;
        }
        

          return (new ApexPages.StandardController(vendor)).view();      
}
         
}

Sridhar BonagiriSridhar Bonagiri

Hi,

 

Are you using this controller as extension controller or custom controller in VFP? what is the standard controller?

 

Regards,

Sridhar Bonagiri

Manju_BNManju_BN

It is the extension controller.................

Sridhar BonagiriSridhar Bonagiri

Hi,

 

Ok, Then in the constructor of the controller class get the record from the main object and using main object's id get the record from second object. In the save method update fields in to the respective objects and save them.


Let me know if you need more help.

 

Regards,

Sridhar Bonagiri