0

I am developing a VF page where all lookup fields (for both parent & child object) will be replaced by autocomplete/typeahead. It is suggesting perfectly for lookup fields, but can't store the selected value into the database, therefore, the field value for that lookup is empty after creating the record. autocomplete From the image, typeahead/autosuggestion is working good, but when I select a value, click SAVE button of the page, this lookup field is not filled with the selected value. I have used SELECT2 3.4.2 version to build the autocomplete component, & the reason why field value is not posting is i have used inputText in component controller.

<apex:inputText style="{!Style}" styleClass="auto{!randomJsIden}" value="{!cacheField}" /> <apex:outputPanel id="hiddenPanel"> <apex:inputText value="{!targetField}" id="hiddenField" styleClass="hiddenField{!randomJsIden}" style="display:none"/> </apex:outputPanel> <script>v2{!randomJsIden} = new typeahead{!randomJsIden}({});</script>

I find that a good way to store inputText into database is mentioned here . But, it only works for Sobject (I have sobject where it is not working, I have also List where this technique is throwing error). The component code in the VF page is

<c:TypeAheadComponent allowClear="true" importJquery="true" labelField="Name" SObject="General_Ledge_Account__c" valueField="Id" targetField="{!targetField}" style="width:15rem" />

In summary, I am trying to perform autocomplete for lookup fields in VF page (parent record & multiple children record are creating from this VF page). Autocomplete is fine (collecting data from database), but whatever I am selecting from VF page is not Stored in the database. Any idea how can I use inputText to store/bind user selected data into the backend database?

2 Answers 2

1

The easiest way to do this is to give your primary record to the component.


// In page's controller
public SObject record { get; set; }
public ControllerConstructor(ApexPages.StandardController ctrl) {
    record = ctrl.getRecord();
}

// In component controller
public SObject parentRecord { get; set; }
public String fieldName { get; set; }

<!-- in component -->
<apex:attribute name="record" assignTo="{!parentRecord}" type="SObject" description="" />
<apex:attribute name="targetField assignTo="{!fieldName}" type="String" description="" />
<!-- some time later ... -->
<apex:inputHidden value="{!parentRecord[fieldName]}" id="..." />

<!-- back in the page ... -->
<c:TypeAheadComponent record="{!record}" ... />
2
  • But, I have mutiple records to be created from this single VF page.
    – Tanmoy Das
    Commented Nov 25, 2016 at 23:11
  • @tanmoy You can bind whichever record you feel like. For example, if it's in a loop: <apex:iteration value="{!recordList}" var="aRecord"><c:TypeAheadComponent record="{!aRecord}" ... The point is, you simply need to bind directly to the record in question.
    – sfdcfox
    Commented Nov 25, 2016 at 23:12
0

According the logic you have written, it looks like that you are trying to store value of input text to the Sobject's lookup field. For example if you have TestG2, you are trying to assign it to Lookup type field which ultimately will throw exception when insert/update happen.

Reason is lookup fields always have Id of the records instead of name. So, you typehead component showing those value must have Id behind as they are record of some object. Store and set that Id to inputText instead of the name visible on page.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .