0

I have create some apex code that is meant to allow me to enter information for two objects into one VF page. I have a custom object leads__c and my standard object Contact. I am able to reference both objects in my VF page, however, my save method does not save any of the custom object fields, and only the contact required field (Last Name), which I have hard coded to 'Sample_LastName' is saved. I need to fix my save method to correctly save the objects.

My Apex Class

public with sharing class MyLeads {
public Leads__c lead {get;set;}

public MyLeads (Apexpages.StandardController stdController) {     
   lead = new leads__c (); 
}   

public Contact con {
    get {     
  Contact cd = new Contact();
  cd.LastName = 'sample_LastName'; //if this is not added i get an error message
  insert cd;           
  return cd;   
    }
    set;
}
public pagereference custsave() {
    try{
        insert this.lead;
        if(con != null)
    {
      insert con;
     }
   }
    catch (exception e) {
        system.debug(logginglevel.error, 'MyLeads.custsave () Exception:' + e);
    }
    pagereference pr = new pagereference ('/' + lead.id);
    pr.setRedirect(true);
    return pr;

    }
}

My VF Page

<apex:page id="leadpage" standardController="Leads__c" showHeader="true" extensions="MyLeads">

 <apex:form id="form">
     <apex:sectionHeader Title="Lead Edit" subtitle="New Lead"/>

         <apex:pageblock Title="Lead Edit">
          <apex:pageMessages />
            <apex:pageblockbuttons >
               <apex:commandButton value="Save" action="{!custSave}"/>
               <apex:commandButton value="Cancel" action="{!Cancel}"/>
             </apex:pageblockbuttons> 

         <apex:pageblockSection title="Lead Information" columns="2" collapsible="false">
             <apex:inputfield taborderhint="1" value="{!Leads__c.name}" required="true"/>
             <apex:inputField taborderhint="4" value="{!Leads__c.Sales_Person_u__c}"/>
             <apex:inputfield taborderhint="2" value="{!Leads__c.Lead_Status__c}"/>
             <apex:inputField taborderhint="5" value="{!Leads__c.Marketing_Person__c}"/>
             <apex:inputfield taborderhint="3" value="{!Leads__c.Lead_Type__c}"/>

             <apex:inputfield taborderhint="6" value="{!Leads__c.Lead_Source__c}" required="true">
                 <apex:actionSupport event="onchange" status="status" reRender="form"/>
             </apex:inputfield>


             <apex:pageblocksectionItem />
             <apex:inputField rendered="{!Leads__c.Lead_Source__c= 'Other'}" taborderhint="7" value="{!Leads__c.Please_Specify__c}" required="true"/>
             <apex:inputField rendered="{!Leads__c.Lead_Source__c= 'Tradeshow'}" taborderhint="7" value="{!Leads__c.Tradeshow__c}" />   
             <apex:inputField rendered="{!Leads__c.Lead_Source__c= 'Broker'}" taborderhint="7" value="{!Leads__c.Broker__c}" />           
         </apex:pageblockSection>  


         <apex:pageblocksection title="Description Information" columns="2" collapsible="false">
             <apex:inputField taborderhint="8" value="{!Leads__c.Personal_Notes__c}"/>
             <apex:inputfield taborderhint="9" value="{!Leads__c.Notes__c}"/>

         </apex:pageblocksection>  

        </apex:pageblock> 

     <apex:pageBlock title="Contact" >
         <apex:pageBlockSection >
             <apex:inputField value="{!accs.Lastname}"  />
             <apex:inputField value="{!accs.Firstname}"  />
             <apex:inputfield value="{!accs.title}"/>
             <apex:inputfield value="{!accs.email}"/>
             <apex:inputfield value="{!accs.phone}"/>
             <apex:inputfield value="{!accs.mobilePhone}"/>
             <apex:inputfield value="{!accs.Office_Phone__c}"/>
             <apex:inputfield value="{!accs.Extension_No__c}"/>
             <apex:inputfield value="{!accs.Fax}"/>

         </apex:pageBlockSection>

         <apex:pageBlockSection columns="1">
             <apex:inputField value="{!accs.Address_Line_1__c}" />
             <apex:inputField value="{!accs.Address_Line_2__c}" />
             <apex:inputField value="{!accs.City__c}" />
             <apex:inputField value="{!accs.Province_State__c}" />
             <apex:inputField value="{!accs.Country__c}" />
             <apex:inputField value="{!accs.Zip_Code__c}" />

         </apex:pageBlockSection>
     </apex:pageBlock>
 </apex:form> 
 </apex:page>
1

You're creating a new contact each time the "get" method is called, which is then later inserted a second time when you click save (this is obviously an exception). Instead, initialize your records in a constructor:

public with sharing class MyLeads {
    public Leads__c lead { get; set; }
    public contact con { get; set; }

    public MyLeads (Apexpages.StandardController stdController) {     
       lead = new leads__c(); 
       con = new contact();
    }   

More generally speaking, you should not generally use DML in a getter or setter, at least not without some extra effort, because getters and setters can be called multiple times per transaction, with the obvious potential side effects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.