Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having problems with my apex code. I don't know if it's just me, but I can get an ID out of a custom object.

So for example if my production org has a Purchase_Quote__c object and in my apex code I do something like Purchase_Quote__c newQuote = new Purchase_Quote__c(); does that automatically give the new object an ID?

Ok so now that I have a better understanding of instantiating objects, this is what I'm trying to do. I'm trying to create a new Purchasing_Quote__c record and multiple child records, line items called Purchase_Quote_Line__c.

<apex:page standardController="Purchase_Request__c" extensions="NewPurchaseQuoteEntry" >
<apex:form >  
  <apex:pageBlock title="New Purchase Quote" >
      <apex:pageBlockButtons >
          <apex:commandButton value="Save" action="{!save}" rerender="error" />

      <apex:pageBlocktable value="{!newPurchaseQuote}" var="a" id="table">
          <apex:column headerValue="Quote Number">
            <apex:outputField value="{!a.Name}"/>
          <apex:column headerValue="Purchase Request Number">
            <apex:inputField value="{!a.Purchase_Request_Number__c}"/>
          <apex:column headerValue="Contact">
            <apex:inputField value="{!a.Contact__c}"/>
          <apex:column headerValue="Additional Information">
            <apex:inputField value="{!a.Additional_Information__c}"/>

Here's the extension class:

public class NewPurchaseQuoteEntry {

    public final Purchase_Request__c purchaseRequest;
    public Purchasing_Quote__c newPurchaseQuote { get; set; }
    public List<Purchase_Quote_Line__c> newPurchaseQuoteLine {get;set;}

    public NewPurchaseQuoteEntry (ApexPages.Standardcontroller myController){
        //get record informaiton from Purchase Request object
        purchaseRequest = (Purchase_Request__c)myController.getrecord();
        //instantiate list of Purchase Quote Line
        newPurchaseQuoteLine = new List<Purchase_Quote_Line__c>();
        //instantiate Purchase Quote
        newPurchaseQuote = new Purchasing_Quote__c();
        newPurchaseQuote.Purchase_Request_Number__c =;
        //get the list of items for the Quote Line items
        newPurchaseQuoteLine = getQuoteList();
        //relate the Purchase Quote back to Purchase Request object


    public List<Purchase_Quote_Line__c> getQuoteList (){
        //error when this inserts
        insert newPurchaseQuote;
        List<Purchase_Quote_Line__c> tempList = new List<Purchase_Quote_Line__c>();

        //loop through all line item records in the Purchase Request Lines
        for(Purchase_Request_Line__c tempRequest: [SELECT Product__c, Request_Qty__c, Hardware_Level__c,
                                                    Software_Level__c FROM Purchase_Request_Line__c 
                                                    WHERE Purchase_Request_Line__c.Purchase_Request__c =]){

            //create new Quote Line item to capture necessary fields
            Purchase_Quote_Line__c newQuoteLine = new Purchase_Quote_Line__c();
            newQuoteLine.Product__c = tempRequest.Product__c;
            newQuoteLine.Qty_Available__c = tempRequest.Request_Qty__c;
            newQuoteLine.Hardware_Level__c = tempRequest.Hardware_Level__c;
            newQuoteLine.Software_Level__c = tempRequest.Software_Level__c;
            //relate all line items to the Purchase quote
            newQuoteLine.Purchase_Quote_Number__c =;



        return tempList;

    public PageReference save(){

        insert newPurchaseQuoteLine;

        PageReference pageRef = new PageReference('/' +;
        return pageRef;



I get a visualforce error "System.LimitException: DML currently not allowed" when trying to insert the newPurchaseQuote.

share|improve this question
up vote 11 down vote accepted

No, Objects do not have an Id until after insert. Insert action automatically will return Id for your record.

share|improve this answer
Thanks for the information. – Pracha Yang Feb 18 '14 at 17:07

To add to what @Birthus said, a few other fields also don't get values until after insert:

SystemModstamp, OwnerId, Name, LastModifiedDate, LastModifiedById, IsDeleted, Id, CreatedDate, CreatedById (I want to say that formula fields will be in this group as well, and any default values for fields like pick-list's etc)

This also means that you wont have access to them in the 'before insert' trigger.

so when you do:

Account a = new Account();
insert a;
System.assert(a.Id != null);

after you insert the object using the var you made for the object will contain the id.

share|improve this answer
Is there a way I can insert and pull the record that I've just inserted? – Pracha Yang Feb 18 '14 at 17:19
Yes; just INSERT the record and query it through SOQL to get all field values. Note that your variables get their Ids populated upon INSERT, so you do not need to query for the id. – Mike Chale Feb 18 '14 at 17:20
yes, once you have inserted, the actual object you have will contain the .id value, so you could just pump that back into a SOQL query. Are you using formula fields by any chance? – Simon Lawrence Feb 18 '14 at 17:21
Thanks for the information, I'm learning a lot. – Pracha Yang Feb 18 '14 at 17:25
That is mostly true, @SalesForceGirl; you cannot execute DML in a VF controller's constructor. – Mike Chale Feb 18 '14 at 17:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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