Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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:pageBlockButtons> 

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

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;

            tempList.add(newQuoteLine);

        }

        return tempList;
    }


    public PageReference save(){

        insert newPurchaseQuoteLine;

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

    }

}

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

share|improve this question
add comment

2 Answers

up vote 10 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 at 17:07
add comment

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 at 17:19
3  
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 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 at 17:21
    
Thanks for the information, I'm learning a lot. –  Pracha Yang Feb 18 at 17:25
    
Can you insert inside a constructor or make a call to a method that does insert in the constructor? –  Pracha Yang Feb 18 at 17:28
show 6 more comments

Your Answer

 
discard

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.