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
Ravi PrabhuRavi Prabhu 

I want to create records for 2 unrelated custom object using 2 save buttons in a single VF page

I have 2 unrelated custom objects. I need to save (insert/upsert) records seperatly with 2 save buttons in 2 custom objects. I have written below controller and VF page but when I click on 'save' button nothing happens even the debug log doesnt show any errors. The save button is not performing any function. 
Please help and thanks in advance!!

Apex controller:
public class ValidateFields {
    
    List<Ticket_Effort_Category__c> ticketExist{set;get;} 
    List<problem_ticket__c> ticketExistp{set;get;} 
            
    public Ticket_Effort_Category__c tktRecord{set;get;}
       public Problem_Ticket__c          prbRecord{set;get;}

    public ValidateFields() {
        tktRecord = new Ticket_Effort_Category__c();
        prbRecord = new Problem_Ticket__c();
    }
    
    public Pagereference saveTicketData(){
        try{
            ticketExist = [select id from Ticket_Effort_Category__c 
                           where Ticket_Number__C = :tktRecord.Ticket_Number__C ] ;
            if(!ticketExist.isEmpty()){
               tktRecord.id =  ticketExist[0].id;
               upsert tktRecord;

            }else{ 
               insert tktRecord;

            }
              PageReference pr = new PageReference('/apex/ticketdataCapature?core.apexpages.request.devconsole=1');
              pr.setRedirect(true);
              return pr;
            
       }
        catch(Exception e){
            String error = e.getMessage();
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,error));
            return null;
        }
    }
    public Pagereference cancelTicketData() {
        tktRecord = new Ticket_Effort_Category__c();
        return null;
    }
    
    public Pagereference saveproblemTicket(){
        try{
            ticketExistp = [select id from problem_ticket__c 
                           where problem_Ticket__C = :prbRecord.problem_ticket__c ] ;
            if(!ticketExistp.isEmpty()){
               prbRecord.id =  ticketExistp[0].id;
               upsert prbRecord;
            }else{ 
               insert prbRecord;
            }
PageReference pr = new PageReference('/apex/ticketdataCapature?core.apexpages.request.devconsole=1');
              pr.setRedirect(true);
              return pr;
            
       }
        catch(Exception e){
            String error = e.getMessage();
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,error));
            return null;
        }
    }
    public Pagereference cancelprbTicketData() {
        prbRecord = new Problem_Ticket__c();
        return null;
    }
}

VF page:

<apex:page Controller="ValidateFields"   showHeader="false" tabStyle="Ticket_Effort_Category__c" extensions="ValidateFields">
    <script>
          document.title = "XYZ Reporting";
    </script>
       
    <style type="text/css">
     .activeTab {background-color: #236FBf;  color:white; background-image:none}
     .inactiveTab { background-color: white; color:black; background-image:none}
        body{background-color: #F8F8FF;}
       .bPageBlock {background-color: #778899 ;}
        .bAlign {text-align:center;}
    </style>
    
    <apex:panelGrid columns="2" width="100%">
        <apex:panelGrid columns="1" width="100%">
            <img src="{!$Resource.logo1}" width="100px" height="100px" align="left"/>
        </apex:panelGrid>

        <apex:outputlabel style="font-style:regular; font-weight:bold; font-size:30px; font-family:Helvetica; color:DodgerBlue" 
                          value="AMS Reporting" />

    </apex:panelGrid>
    <apex:form styleClass="myFormStyle" >
    

    <apex:pageBlock title="Incident Reporting" tabStyle="Ticket_Effort_Category__c" >
        <apex:pageBlockTable value="{!tktRecord}"  title="Incident Reporting" var="Incident" id="msgsI" style="float:centre">
            <apex:column headerValue="Divsion">
                    <apex:inputField value="{!Incident.Division__c}"/>
            </apex:column>
            <apex:column headerValue="Application">
                    <apex:inputField value="{!Incident.Application__c}"/>
            </apex:column>
               <apex:column headerValue="Ticket Number">
                    <apex:inputField value="{!Incident.Ticket_Number__c}"/>
            </apex:column>
            <apex:column  headerValue="Technology">   
                    <apex:inputField value="{!Incident.Technology__c}"/>
            </apex:column>
            <apex:column headerValue="Responsible">
                    <apex:inputField value="{!Incident.Responsible__c}"/>
            </apex:column>
             <apex:column headerValue="Category">
                    <apex:inputField     value="{!Incident.Category__c}"/>
             </apex:column>
             <apex:column headerValue="Effort">
                    <apex:inputField     value="{!Incident.Effort__c}"/>
              </apex:column>
           </apex:pageBlockTable>
        <apex:pageBlockButtons location="bottom" style="float:centre">
            <apex:commandButton value="Save"     action="{!saveTicketData}" reRender="msgsI" onclick = "Record Saved"/>
            <apex:commandButton value="Cancel"     action="{!cancelTicketData}" reRender="msgsI" onclick="Cancelled" />
        </apex:pageBlockButtons>
    </apex:pageBlock>
    
    

    <apex:pageBlock title="Problem Ticket Reporting" tabStyle="Problem_Ticket__c" >
        <apex:pageBlockTable value="{!prbRecord}"  title="Problem Ticket Reporting" var="problem" id="msgsP" style="float:centre">
            <apex:column headerValue="Divsion">
                    <apex:inputField value="{!problem.Division__c}"/>
            </apex:column>
            <apex:column headerValue="Application">
                    <apex:inputField value="{!problem.Application__c}"/>
            </apex:column>
               <apex:column headerValue="Ticket Number">
                    <apex:inputField value="{!problem.Problem_Ticket__c}"/>
            </apex:column>
            <apex:column  headerValue="Technology">   
                    <apex:inputField value="{!problem.Technology__c}"/>
            </apex:column>
            <apex:column headerValue="Responsible">
                    <apex:inputField value="{!problem.Responsible__c}"/>
            </apex:column>
            <apex:column headerValue="Closed Date">
                    <apex:inputField     value="{!problem.Completed_Date__c}"/>
              </apex:column>

             <apex:column headerValue="Effort">
                    <apex:inputField     value="{!problem.Effort__c}"/>
              </apex:column>
           </apex:pageBlockTable>
        <apex:pageBlockButtons location="bottom" style="float:centre">
            <apex:commandButton value="Save"     action="{!saveproblemTicket}" reRender="msgsP" onclick = "Record Saved"/>
            <apex:commandButton value="Cancel"     action="{!cancelprbTicketData}" reRender="msgsP" onclick="Cancelled" />
        </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>
</apex:page>


 
Best Answer chosen by Ravi Prabhu
Ravi PrabhuRavi Prabhu
Was able to resolved, the requirement changed.