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
I am ABI am AB 

Record not inserted. Please help

Hi Expert, 
I am very new the salesforce.
I want to insert a records in my custom object Invoice__c, But nothing is inserted with this code.
 
VF page code:

<apex:page standardController="Invoice__c" extensions="particular" docType="Html-5.0" sidebar="false" tabStyle="invoice__tab">
  <apex:form >
      
<apex:pageBlock title="Invoice Details" >
     
 <apex:pageBlockSection title="Account Details" columns="1">
          <apex:inputfield Value="{!Invoice__c.Account_Name__c}"/>
      </apex:pageBlockSection> 
     
 <apex:pageBlockSection title="Invoice Details" columns="2">   
          <apex:inputField Value="{!Invoice__c.Invoice_Date__c}"/>
          <apex:inputField value="{!Invoice__c.Description__c}"/>
          <apex:inputField value="{!Invoice__c.Net_Total__c}"/>
      </apex:pageBlockSection>

<apex:pageBlockButtons >
      <apex:commandButton Value="Save" action="{!save}"/>
      <apex:commandButton Value="Cancel" action="{!cancel}"/>
      </apex:pageBlockButtons>
       </apex:pageBlock>
  </apex:form>
</apex:page>

Controller Code:

Controller Page

public class particular {
    
    public invoice__c invoices;
    public account accountname {get;set;}
    public datetime indate {get;set;}
    public string des {get;set;}
    public decimal nettotal {get;set;}
 
 
    public particular(ApexPages.StandardController controller) { 
    }
    
       public PageReference save(){

         invoice__c objin = new invoice__c();

         objin.Account_Name__r= accountname; 
         objin.Description__c= des; 
         objin.invoice_Date__c=indate;
         insert objin;

         PageReference vin = new PageReference(Page.ViewInvoice.getUrl()+'?Id='+objin.Id);
         vin.setRedirect(true);
         return vin;
 
    }       

     
     public Pagereference cancel(){
            
        PageReference Pg= Page.NewInvoice;
        pg.setRedirect(true);
        return Pg;  
     }
 
}

Thanks in advance
Sherry ! 
Sumeet_ForceSumeet_Force
You can use either just standard controller OR else use inputText instead of Inputfield and use apex variable in that line.
Manohar kumarManohar kumar

Hi Sherry,

pls try this code 

<apex:page standardController="Invoice__c" extensions="particular" docType="Html-5.0" sidebar="false" tabStyle="invoice__tab">
  <apex:form >
      
<apex:pageBlock title="Invoice Details" >
     
 <apex:pageBlockSection title="Account Details" columns="1">
          <apex:inputfield Value="{!Invoice__c.Account_Name__c}"/>
      </apex:pageBlockSection> 
     
 <apex:pageBlockSection title="Invoice Details" columns="2">   
          <apex:inputField Value="{!Invoices.Invoice_Date__c}"/>
          <apex:inputField value="{!Invoices.Description__c}"/>
          <apex:inputField value="{!Invoices.Net_Total__c}"/>
      </apex:pageBlockSection>

<apex:pageBlockButtons >
      <apex:commandButton Value="Save" action="{!save}"/>   <!--  standard controller will do save dont have to do in class -->
      <apex:commandButton Value="Cancel" action="{!cancel}"/>
      </apex:pageBlockButtons>
       </apex:pageBlock>
  </apex:form>
</apex:page>
 
public class particular {
    
    public invoice__c invoices{get;set;}; 
    public account accountname {get;set;}
    public datetime indate {get;set;}
    public string des {get;set;}
    public decimal nettotal {get;set;}
 
 
    public particular(ApexPages.StandardController controller) { 
    }
       // can remove this save code else just write insert invoices; if you want to insert  

       public PageReference save(){

         invoice__c objin = new invoice__c();

         objin.Account_Name__r= accountname; 
         objin.Description__c= des; 
         objin.invoice_Date__c=indate;
         insert objin;

         PageReference vin = new PageReference(Page.ViewInvoice.getUrl()+'?Id='+objin.Id);
         vin.setRedirect(true);
         return vin;
 
    }       

     
     public Pagereference cancel(){
            
        PageReference Pg= Page.NewInvoice;
        pg.setRedirect(true);
        return Pg;  
     }
 
}


havn't tried the code, but this should work. pls let me know if this doesn't work.

Thanks,

Manohar

I am ABI am AB
Yes but if I  am trying to using input text then lookup filed to account not worked and also datetime field not allowed to me to select date and time.
Thanks 
 
I am ABI am AB
Hi Manohar, 
I want to insert record only from Apex method instead of standard method. Please help.
Sumeet_ForceSumeet_Force
in that case , use this:
 public Account acct;
    public particular(ApexPages.StandardController controller) { 
this.acct = (Account)Controller.getRecord();
    }

GIving sample for Account. You can add above code and then in the field assignment , use the API field names instead of variables (example given below). That should suffice.

  Account objin = new Account();
         objin.Description= acct.description; 
         objin.Name=acct.Name;
Manohar kumarManohar kumar

Hi , 

pls try this code 

<apex:page standardController="Invoice__c" extensions="particular" docType="Html-5.0" sidebar="false" tabStyle="invoice__tab">
  <apex:form >
      
<apex:pageBlock title="Invoice Details" >
     
 <apex:pageBlockSection title="Account Details" columns="1">
          <apex:inputfield Value="{!Invoice__c.Account_Name__c}"/>
      </apex:pageBlockSection> 
     
 <apex:pageBlockSection title="Invoice Details" columns="2">   
          <apex:inputField Value="{!Invoices.Invoice_Date__c}"/>
          <apex:inputField value="{!Invoices.Description__c}"/>
          <apex:inputField value="{!Invoices.Net_Total__c}"/>
      </apex:pageBlockSection>

<apex:pageBlockButtons >
      <apex:commandButton Value="Save" action="{!saveNew}"/>   
      <apex:commandButton Value="Cancel" action="{!cancel}"/>
      </apex:pageBlockButtons>
       </apex:pageBlock>
  </apex:form>
</apex:page>
public class particular {
    
    public invoice__c invoices{get;set;}; 
    public account accountname {get;set;}
    public datetime indate {get;set;}
    public string des {get;set;}
    public decimal nettotal {get;set;}
 
 
    public particular(ApexPages.StandardController controller) { 
    }
     
       public PageReference saveNew(){

         insert invoices;  // it will insert if you have all the required fields else it will tell which fields are missing

         PageReference vin = new PageReference('/'+invoices.Id);
         vin.setRedirect(true);
         return vin;
 
    }       

     
     public Pagereference cancel(){
            
        PageReference Pg= Page.NewInvoice;
        pg.setRedirect(true);
        return Pg;  
     }
 
}


pls try this let me know if it workd.

Thanks,

Manohar