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
geo789geo789 

adding a standard save to a custom object

Hi folks..i am new to salesforce/visualforce/apex. I have created a custom object Deal__c and have created a button to search the deals based on the users input. I need to add the save button. Can someone plz show how to add?i tried creating a stnadard controller and using an extension. But it was showing constructor errror.Any help is greatly appreciated.

 

 

public with sharing class Demo2 {
    public List<Deal__c> SearchResult{get;set;}
    public String SelectedValue1{get;set;}
    public String SelectedValue2{get;set;}
    public Deal__c SearchCriteria{get;set;}
    
    public Demo2 ()
    {
        SearchCriteria = new Deal__c();
    }
    
    public void filterRecord()    
    {
        SearchCriteria = new Deal__c();
        SearchResult = new List<Deal__c>();
        
        SearchResult = [SELECT         Deal_Name__c,
                                    Deal_Type__c
                              FROM Deal__c
                              WHERE Deal_Name__c =: SearchCriteria.Deal_Name__c
                              AND Deal_Type__c    =: SearchCriteria.Deal_Type__c];
         if(SearchResult.size() == 0)
         {
            Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display'));
         }
    }
    
       
}

 

<apex:page controller="Demo2">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection >
          <apex:inputField value="{!SearchCriteria.Deal_Name__c}"/>
      
          <apex:inputField value="{!SearchCriteria.Deal_Type__c}"/>
          
          <apex:commandButton value="Search" action="{!filterRecord}" reRender="applicantTbl,error"/>
          
          
      
  </apex:pageBlockSection>
 
      <apex:pageBlockTable styleClass="appTbl" id="applicantTbl" value="{!SearchResult}" var="rec">
          <apex:column value="{!rec.Deal_Name__c}"/>
          <apex:column value="{!rec.Deal_Type__c}"/>      
          <apex:outputLink value="{!URLFOR($Page.customDetailPage, '', [id=Deal__c.id])" />
 
      </apex:pageBlockTable>
           <apex:pageMessages id="error"></apex:pageMessages>
  </apex:pageBlock>
 
    </apex:form>
</apex:page>

 

 

KunalSharmaKunalSharma

Hi,

 

Just add the below code to your class to leverage standard controller, after adding this you can use standard save in your class.

 

public Demo2(ApexPages.StandardController controller) {

    }

 

Thanks,

Kunal

geo789geo789

I tried adding the code you provided. It still doesnt work. My controller extension looks like this. Anyways thanks for your reply.

 

 

public with sharing class Demo2 {
    public List<Deal__c> SearchResult{get;set;}
    public String SelectedValue1{get;set;}
    public String SelectedValue2{get;set;}
    public Deal__c SearchCriteria{get;set;}
    
    public Demo2 ()
    {
        SearchCriteria = new Deal__c();
    }
    public Demo2(ApexPages.StandardController controller) {

    }
    
    public void filterRecord()    
    {
        SearchCriteria = new Deal__c();
        SearchResult = new List<Deal__c>();
        
        SearchResult = [SELECT         Deal_Name__c,
                                    Deal_Type__c
                              FROM Deal__c
                              WHERE Deal_Name__c =: SearchCriteria.Deal_Name__c
                              AND Deal_Type__c    =: SearchCriteria.Deal_Type__c];
         if(SearchResult.size() == 0)
         {
            Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display'));
         }
    }
    
       
}

KunalSharmaKunalSharma

Have you added the standard controller to your page?? 

 

<apex:page standardController="Deal__c" extensions="Demo2">

 

??

geo789geo789
yes..i have added
<apex:page standardcontroller="Deal__c" extensions="Demo2">

asish1989asish1989

<apex:page standardcontroller="Deal__c" extensions="Demo2">

In the controller ,add constructor like this


public Demo2(ApexPages.StandardController controller) {
SearchCriteria = (Deal__c)new Deal__c();
}

controller.save()---> will call standard sava function.

KunalSharmaKunalSharma
recCtrl = (Deal__c) controller.getRecord(); // Add this in constructor and remove your custom save


<apex:commandButton value="Save" action="{! Save}"/> // Use this in page


 

geo789geo789

I tried it with no sucess. Can you please edit on my pgm and post the program. I feel that i am missing something.

KunalSharmaKunalSharma

Can you please explain a bit what is the functionality you are trying to implement? After searching a record what will you edit?

geo789geo789
I wanted to implement a search and a save button for a custom object. I was able to do it. Anyways Thanks for spending your time. I greatly appreciate your help