[SalesForce] VisualForce: How To INSERT Custom Object(s) From VF Form

PREFACE: This is my first VF project but I have spent some R&D time getting familiar with relationship between VFPages and VFControllers, attempting to understand Getters & Setters, and VF Page Order of Execution.

OBJECTIVE: With that said I current have a new Shipment form that I am working on. Essentially this will use the url parameter to fetch an Account in order to pre-populated the form with known shipment details (otherwise all fields are blank). Using details from the Account object, as well as record details from a custom Inventory object that is related to the Account. I need this form to create a new Shipment__c object rather than using the standard Saleforce workflow.

Also, once the Shipment is INSERTED (much like Opportunities and OpportunityLineItems), I expect that all Inventory item returned in the InventoryLineItems getter will be added to this Shipment object as a new ShipmentLineItem__c (which is a child object of Shipment__c). Someday this form could require a checklist that allows me to select which Inventory items to insert as ShipmentLineItem__c(s), but at the moment just inserting them all is fine.

Once the transaction is complete I would like the user to be redirected to the newly created Shipment__c record.

PROBLEM: I am not familiar with how to post/set the data I need in order to create and insert the custom objects I need. I'm very familiar with trigger development so I feel like if I could better understand how to work with the data throughout this process I could figure out the rest

VisualForce Controller Extension:

public with sharing class ShipmentExtension {

    ApexPages.StandardController standardController;
    String accountId;

    /* Setup Carrier Options */
    public String carrier { get; set; }

    public List<SelectOption> carrier_options { get; private set; } {
        carrier_options = new List<SelectOption>();
        carrier_options.add( new SelectOption('Private Freight', 'Private Freight') );
        carrier_options.add( new SelectOption('UPS', 'UPS') );
        carrier_options.add( new SelectOption('Other', 'Other') );
    }

    public ShipmentExtension(ApexPages.StandardController standardController) {
        this.standardController = standardController;
        this.accountId = ApexPages.currentPage().getParameters().get('accountid');
        this.carrier = 'Private Freight'; /* Default Option */


    }

     /* List Inventory Line Items */
    public List<Custom_Inventory__c> InventoryLineItems {
        get { return [SELECT Product__c, Product_Name__c, Balance__c FROM Custom_Inventory__c WHERE Account__c = :this.AccountId AND Difference__c > 0]; }
        set;
    }

    public Account getAccount() {
        if(!string.isEmpty(this.accountId)){
            return [
                SELECT Id, Name, Owner.Name, ShippingStreet, ShippingCity, ShippingCountry, ShippingState, ShippingPostalCode FROM Account
                WHERE Id = :this.AccountId
            ];
        } else {
            return new Account();
        }
    }

    public PageReference createShipment(){
        Shipment__c new_shipment = new Shipment__c();

        System.debug('::CREATE SHIPMENT:: ');
        System.debug(Apexpages.currentPage().getParameters());

        new_shipment.Account__c = ...;
        new_shipment.Carrier__c = ...;
        new_shipment.Shipment_Address__c = ...;

        INSERT new_shipment;

        List<ShipmentLineItem> new_shipmentLineItems = new List<ShipmentLineItem>();
        for(Custom_Inventory record : InventoryLineItems){
            ShipmentLineItem__c newSLI = new ShipmentLineItem__c();
            newSLI.Product__c = record.Product__c;
            newSLI.Shipment__c = new_shipment.Id;
            new_shipmentLineItems.add(newSLI);
        }

        return null;
    }
}

VisualForce Page:

<apex:page standardController="Shipment__c" extensions="ShipmentExtension">

    <apex:outputPanel layout="block">

        <apex:outputPanel layout="block">
            <apex:outputPanel layout="block">
                <apex:outputPanel layout="block">Shipment Overview for: {!Account.Name}</apex:outputPanel>
            </apex:outputPanel>
        </apex:outputPanel>

        <apex:form id="createShipmentForm">
            <apex:pageBlock >

                 <apex:outputPanel layout="block">
                      <apex:outputPanel layout="block">
                          <apex:outputPanel layout="block">Shipping Information</apex:outputPanel>
                    </apex:outputPanel>
                </apex:outputPanel>

                <apex:outputPanel layout="block">
                    <apex:outputPanel layout="block">
                        <apex:outputPanel layout="block">Ship To:</apex:outputPanel>
                    </apex:outputPanel>
                </apex:outputPanel>

                <apex:outputPanel layout="block">
                    <apex:outputPanel layout="block">
                        <apex:inputText value="{!Account.Owner.Name}" /><!-- Name -->
                    </apex:outputPanel> 
                </apex:outputPanel>

                <apex:outputPanel layout="block">
                    <apex:outputPanel layout="block">
                        <apex:inputText value="{!Account.ShippingStreet}" /><!-- Street -->
                    </apex:outputPanel>
                </apex:outputPanel>

                <apex:outputPanel layout="block">
                    <apex:outputPanel layout="block">
                        <apex:inputText value="{!Account.ShippingCity}" /><!-- City -->
                    </apex:outputPanel> 

                    <apex:outputPanel layout="block">
                        <apex:inputText value="{!Account.ShippingState}" style="text-align:center;"/><!-- State -->
                    </apex:outputPanel>

                    <apex:outputPanel layout="block">
                        <apex:inputText value="{!Account.ShippingPostalCode}" /><!-- Postal Code -->
                    </apex:outputPanel>

                </apex:outputPanel>

                <apex:outputPanel layout="block">
                    <apex:outputPanel layout="block">
                        <apex:outputPanel layout="block">Refrigerant to be Shipped</apex:outputPanel>
                    </apex:outputPanel> 
                </apex:outputPanel>

                <apex:pageBlockTable value="{!InventoryLineItems}" var="i">
                    <apex:column headerValue="{!i.Product_Name__c}" value="{!i.Product_Name__c}"/><!-- Product Name -->
                    <apex:column headerValue="{!$ObjectType.Custom_Inventory__c.fields.Balance__c.label}" value="{!i.Balance__c} lbs"/><!-- Balance -->
                </apex:pageBlockTable>

                <apex:outputPanel layout="block">
                    <apex:outputPanel layout="block">
                        <apex:outputPanel layout="block">Shipping and Carrier Details</apex:outputPanel>
                    </apex:outputPanel>
                </apex:outputPanel>

                <apex:outputPanel layout="block">
                    <!-- Shipping Via -->
                    <apex:outputPanel layout="inline">Shipping via:</apex:outputPanel>
                    <apex:outputPanel layout="block">
                        <apex:selectList value="{!carrier}" styleClass="editable" multiselect="false" size="3" >
                            <apex:selectOptions value="{!carrier_options}"/>
                        </apex:selectList>
                    </apex:outputPanel>

                    <!-- Tracking # -->
                    <apex:outputPanel layout="block">Tracking #:</apex:outputPanel>
                    <apex:outputPanel layout="block">
                        <apex:inputText />
                    </apex:outputPanel>
                </apex:outputPanel>

                <apex:outputPanel layout="block">
                    <!-- Shipping Notes -->
                    <apex:outputPanel layout="inline">Shipping Notes:</apex:outputPanel>
                    <apex:outputPanel layout="block">
                        <apex:inputTextarea styleClass="editable" style="width:100%;"/>
                    </apex:outputPanel>

                    <!-- Ship Date -->
                    <apex:outputPanel layout="block">Expected to Ship on:</apex:outputPanel>
                    <apex:outputPanel layout="block">
                        <apex:inputText />
                    </apex:outputPanel>
                </apex:outputPanel>

            <apex:pageBlockButtons >
                <apex:commandButton value="Create Shipment" action="{!createShipment}">
                </apex:commandButton>
            </apex:pageBlockButtons>

        </apex:pageBlock>

    </apex:form>

</apex:outputPanel>

I know this is a more complex ask, and I've been researching what I can on my end to get this far, but any feedback that could help me link the missing pieces would be greatly appreciated. Thanks!

Best Answer

You can start by defining an Account object that you would refer in your createShipment method.

public with sharing class ShipmentExtension {

    ApexPages.StandardController standardController;
    String accountId;
    public Account objAccount {get; set;}

Next, in your constructor you can initialize objAccount.

public ShipmentExtension(ApexPages.StandardController standardController) {
        this.standardController = standardController;
        this.accountId = ApexPages.currentPage().getParameters().get('accountid');
        this.carrier = 'Private Freight'; /* Default Option */

if(!string.isEmpty(this.accountId)){
            objAccount = [SELECT Id, Name, Owner.Name, ShippingStreet, ShippingCity, ShippingCountry, ShippingState, ShippingPostalCode FROM Account
                WHERE Id = :this.AccountId];
        } else {
            objAccount = new Account();
        }

    }

In your VF page, you would need to use {!objAccount.} instead of {!Account.}

In you createShipment() method, you would be able to get the updated values when you refer the objAccount object.

new_shipment.Account__c = objAccount.Id;

Finally, instead of returning null, you would return the PageReference of the new shipment record.

Related Topic