Thursday, October 4, 2012

Opportunity Line Items VF:

Opportunity Line Items VF:
This example shows how to update opportunity line items using visualforce. The controller looks like this
public with sharing class scheduleBuilder {
    public String selectedOLI {set; get;}
    public Decimal salesPrice {set; get;}
    OpportunityLineItem oli;
    OpportunityLineItemSchedule oliSchedule;
    public scheduleBuilder(ApexPages.StandardController controller) {

    }
    public PageReference scheduleValidation() {
        // If (schedule=true) then {throw message to delete schedule first.} else {update sales price}
        ApexPages.Message scheduleFoundMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'You must delete the schedule first');
        System.debug('selectedOLI => ' + selectedOLI);
        try{
            //ApexPages.addMessage(scheduleFoundMsg);
            oli = [Select id, Sales_price__c, HasSchedule, OpportunityID from OpportunityLineItem where id=:selectedOLI];
            if(oli != null && oli.HasSchedule == true) {
                ApexPages.addMessage(scheduleFoundMsg);
            } else {
                oli.Sales_price__c = salesPrice;
                update oli;
                PageReference pageRef = new PageReference('/apex/optyschedule?id='+oli.OpportunityID);
                pageRef.setRedirect(true);
                return pageRef;
            }
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);
        }
        return null;
    }

}
and the page looks like this

<apex:page standardController="Opportunity" extensions="scheduleBuilder">
    <apex:messages />
  <!--  <apex:outputLink value="http://www.kermoonyconsulting.com">Kermoony Consulting</apex:outputLink> -->
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Select an Opportunity Product Line Item to edit by clicking on the id link">
                <apex:pageBlockTable value="{!Opportunity.OpportunityLineItems}" var="line" columns="5" columnsWidth="600">
                    <apex:column headerValue="Sales Price (Monthly)"><apex:outputField value="{!line.Sales_Price__c}"/></apex:column>
                    <apex:column headervalue="Quantity"><apex:outputField value="{!line.quantity}"/></apex:column>
                    <apex:column headervalue="OLI ID">
                        <apex:commandLink reRender="target" >{!line.id}
                            <apex:param name="OLI_ID" value="{!line.id}" assignTo="{!selectedOLI}"/>
                        </apex:commandLink>
                    </apex:column>
                    <apex:column headervalue="If checked, Delete schedule"><apex:outputField value="{!line.HasSchedule}"/></apex:column>
                </apex:pageBlockTable> 
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:panelGrid columns="1" id="theGrid">
                    <apex:panelGroup >
                        <apex:outputText >Selected OLI => </apex:outputText>
                        <apex:outputText value="{!selectedOLI}" label="Selected OLI" id="target"></apex:outputText>                    
                    </apex:panelGroup>
                    <apex:panelGroup >
                        <apex:outputText >Sales Price (Monthly) </apex:outputText>
                        <apex:inputtext value="{!salesPrice}" />                    
                    </apex:panelGroup>
                    <apex:commandButton action="{!scheduleValidation}" value="Update"/>
                </apex:panelGrid>
                </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

No comments:

Post a Comment