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
lokeguptalokegupta 

Saving multiple objects from the a single visual force page : Urgent

I am having relation where customer and offer are in look up relation. I have a visual force page which looks like the as provided in the image.  I want to save the page details in their respective objects. I created a extension of the customer standard controller in which I I am trying to save the objects.

My quetions -

Is it possible to do this kind of thing?
If possible what will be the approach?


Thanks in advance

 

 

S91084S91084

Hi,

In your controller define the following:

 

public Customer__c customer {

         get{

                    if(customer==null)

                         customer = new Customer__c();

                    return customer;

          }

          set;

}

 

public Offer__c offer {

         get{

                    if(offer==null)

                         offer = new Offer__c();

                    return offer;

          }

          set;

}

 

When you display the fields on the page, use <apex:inputField/> tag and mention the value as {!customer.Customer_type__c}

 

Follow the same for other fileds and also for the offer object, then in you save method you can just simply declare,

 

public PageReference save(){

      //Define your custom validations and exception handling and then

       insert offer;

       customer.Offers__c = offer.id; // The field Offers__cis the relationship name between customer and offer

       insert customer;

}

 

Let me know if you need any additional info.

lokeguptalokegupta

Thanks

 

 

I was trying to set the customer and offer object in the constructor.

 

public Customer__c customer { get; set;}
    public Customer__c cust{ get; set;}
    public Offer_del__c ofr { get; set;}
   
    public CrmaController() {
       cust = new Customer__c();

       ofr = new Offer_del__c ofr ;


    }

 

 

What's wrong in this code?

 

 

There is one more query..

 

 

 

I have a pick list for example customer type which is a picklist. Now how should I write in vf page

 

 

<apex:inputField styleClass="combobox" id="operatingunit" value="{!customer.Operating_Unit__c}" />

 

 

or

 

 <apex:selectList styleClass="combobox" id="operatingunit" value="{!customer.Operating_Unit__c}"
                             size="1" required="true">
                    <apex:selectOptions value="{!operatingunits}"/>
                    </apex:selectList>  

 

For the second I will have to write getter something like

 

 public List<SelectOption> getCustomerTypes(){
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Customer__c.Customer_Type__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry f : ple){
            options.add(new SelectOption(f.getLabel(),f.getValue()));
        }
        return options;
        
    }

 

Which is the best way?

S91084S91084

For your picklist value, you can directly use the <apex:inputField value="{!customer.Operating_Unit__c}"/>. if this is a picklist filed, it displays the options for you by default.

 

In your controller, you are defining the object in your constructor, this will be called only for the first time your page gets loaded. But if you would like to retain the values stored in your objects from the page, you will have use the way i have mentioned in my earlier post. What it does is if the object is created, then it returns that object otherwise it will return the object if one exists. If you are using one controller for multiple pages, then also this approach will be very useful.