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 = purchaseRequest.id;
        //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 = :purchaseRequest.id]){

            //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 = newPurchaseQuote.id;



        return tempList;

    public PageReference save(){

        insert newPurchaseQuoteLine;

        PageReference pageRef = new PageReference('/' + newPurchaseQuote.id);
        return pageRef;



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

2 Answers 2


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


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.

  • Is there a way I can insert and pull the record that I've just inserted? Feb 18, 2014 at 17:19
  • 4
    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, 2014 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? Feb 18, 2014 at 17:21
  • Can you insert inside a constructor or make a call to a method that does insert in the constructor? Feb 18, 2014 at 17:28
  • 1
    That is mostly true, @SalesForceGirl; you cannot execute DML in a VF controller's constructor.
    – Mike Chale
    Feb 18, 2014 at 17:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .