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.