2

New to apex coding and I've been searching on the web all day for what I need to get this task completed. I'm working on a purchasing application. We can probably buy this, but buying it is not an option.

So what I have done so far. Created a Purchase Request object, for the initial request. It has Purchase Request Lines as a related list.

I also have a Purchase Quote related to the Purchase Request and the Purchase Quote has a related line item list.

I want to be able to use a button to create a new Purchase Quote record and pull the Purchase Request Line information and create Purchase Quote line items. It would also need to pull the Purchase Request id and add it to the lookup field in the Purchase Quote.

Here's what I've done so far:

<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="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>

Extension:

public class NewPurchaseQuoteEntry {

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

    public NewPurchaseQuoteEntry (ApexPages.Standardcontroller myController){
        this.purchaseRequest = (Purchase_Request__c)myController.getrecord();
        newPurchaseQuote = new Purchasing_Quote__c();
    }

    public List<Purchase_Quote_Line__c> getQuoteList (){
        List<Purchase_Quote_Line__c> tempList = new List<Purchase_Quote_Line__c>();

        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]){

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

            tempList.add(newQuoteLine);

        }

        return tempList;
    }

    public void newPurchaseQuoteRecord (){

        newPurchaseQuoteLine = getQuoteList();
        newPurchaseQuote.Purchase_Request_Number__c = purchaseRequest.id;
    }

    public PageReference save(){
        newPurchaseQuote.Purchase_Request_Number__c = purchaseRequest.id;
        insert newPurchaseQuote;
        insert newPurchaseQuoteLine;

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

    }

}

I'm unable to pull the Purchase Request id over onto the visualforce page. I would also like to add the Purchase Quote Line related list onto the visualforce page and add in certain information there before saving it.

  • 1
    Need more info... what does your <apex:page ... element look like? All attributes and values for this are needed to make a fair assessment. Also, what kind of button are you using? What does the URL look like when the page loads? What formula/code are you using in the button? The devil is in the details. – sfdcfox Feb 18 '14 at 0:11
  • @sfdcfox is totally right! The quest should not be hard (just pass parameter and handle creating) but real problem is what you want implemented and how? – Artur Kępczyński Feb 18 '14 at 7:54
1

I think there are many issues in your code. First, as the standard controller is the Purchase Request, why don't you assign its id to the Quote when you create it?

Also, the way you're linking the Quote Line items to the Quote is wrong. What you can do is create a method to:

  1. Create the Quote
  2. Create the Quote Line items
  3. Insert the Quote
  4. Loop on the Quote Line items and assign the Quote id to the lookup field
  5. Insert the Quote Line items

Use try/catch blocks and Database savepoint to rollback in case some records were not inserted correctly.

There are ways to insert the Quote and the Line items at the same time using external ids.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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