1

I would like to create a VF page ("AllUnitIdssaved") that will accept users inputs. This VF page is on the "Shipment_Details__c", which has a lookup to "QuoteLineItem". When the user clicks a custom button ("Create Unit Id") on the Shipment_Details related list, it should bring up the VF page.

A couple things I need help on:

  1. I'm getting an error when I click the "Create Unit Id" button. Below is my setup for my custom button.

    Behavior: Display in existing window with sidebar
    Button or Link URL: window.location = '/apex/AllUnitIdssaved';

  2. How do I link the Shipment_Details records that are being created on the Visualforce page to the Quote Line Item records? Here's my markup:

    <apex:page Controller="AddmultipleUnitIdsController">
        <apex:form >
            <apex:pageBlock >
                <apex:pageBlockTable value="{!listUnitId}" var="unit">
                    <apex:column headerValue="Create Unit Id">
                        <apex:inputField value="{!unit.Name}"/>
                    </apex:column>
                </apex:pageBlockTable>
                <apex:pageBlockButtons >
                    <apex:commandButton value="Create Unit Ids" action="{!addUnitId}"/>
                    <apex:commandButton value="Save Unit Ids" action="{!saveUnitId}"/>
                </apex:pageBlockButtons>
            </apex:pageBlock>
        </apex:form>
    </apex:page>
    

    Controller:

    public class AddmultipleUnitIdsController {
    
        Shipment_Detail__c unitid = new Shipment_Detail__c();
    
        public list<Shipment_Detail__c> listUnitId{ get; set; }
    
        public AddmultipleUnitIdsController()
        {
            listUnitId=new list<Shipment_Detail__c>();
            listUnitId.add(unitid);
        }
    
        Public void addUnitId()
        {
            Shipment_Detail__c unit = new Shipment_Detail__c();
            listUnitId.add(unit);
        }
    
        public PageReference saveUnitId() {
    
        for(Integer i=0; i<listUnitId.size(); i++)
        {
            insert listUnitId;
        }
            return Page.AllUnitIdssaved;
        }
    }
    
  • What's the error you're seeing when you click the Create Unit Id button? – Shane Steinfeld Oct 4 '18 at 23:00
1

As for linking the new Shipment_Detail records to their parent QuoteLineItem record, you need to pass the QuoteLineItem's Id to the visualforce page in the querystring, like this:

https://test.salesforce.com/apex/myPageName?Id=myQuoteLineItemId

Then, in your page controller, you can get the Id value like this:

Id quoteLineItemId = apexPages.currentPage().getParameters().get('Id');

Use the Id to populate the lookup field for each new Shipment_Detail record.

Also, you should take your DML write out of the for loop. (This is a no-no!)

for(Integer i=0; i<listUnitId.size(); i++)
{
    insert listUnitId;
}

Since you can only have 150 DML operations (inserts, updates, deletes, ...) in a single transaction, you could easily use them all up if you inserted one record at a time!

Instead, you can insert your list of Shipment_Detail records all at once.

insert(listUnitId);

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.