function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Varshitha KVarshitha K 

​Displaying the records present in a lookup field as a list

User-added imageI have created a lookup field  called Metric Board Name for a object named Metrics.Now i need to add the records present in the metric board lookup field,one at a time, to a separate list .How can I do this?
plzz help me,thanks in advance.


















 
Best Answer chosen by Varshitha K
Pankaj_GanwaniPankaj_Ganwani
Hi Varshitha,

Not sure if you got the solution for your problem. If not, you can try to use below mentioned code to achieve this:

page
<apex:page controller="TestForlookup" id="pageId">
  <apex:form id="formId">
      <apex:inputField value="{!objAccount.ParentId}" id="selectAccount" />
      <apex:commandButton value="Add to list" onClick="return addtolist('{!$Component.selectAccount}')" /><br/>
      <apex:selectlist size="10" id="optionlist" style="width:200px;">
          <apex:selectOption itemvalue="" itemlabel="--None--"></apex:selectOption>
      </apex:selectlist>
  </apex:form>
  <script>
  function addtolist(selectedValue)
  {
       var listElement = document.getElementById('{!$Component.pageId:formId:optionlist}');
       var listItem = document.createElement('option');
       listItem.text = document.getElementById(selectedValue).value;
       listItem.value = document.getElementById(selectedValue+'_lkid').value;
       listElement.add(listItem);
       return false;
  }
  </script>
</apex:page>

Controller:
public class TestForlookup
{
      public Account objAccount {get;set;}
      public TestForlookup()
      {
           objAccount = new Account();
      }
}

I have done this for Account object, you can replace it with your Matrix object.

Thanks,
Pankaj

All Answers

sandeep sankhlasandeep sankhla
Hi varshitha,

you can bind the list with the section which is currently blank...now on load thi slist will be empty ...now on click of add to list button, you can call a controller method and there you can add that selected element in the list and then again you can rerender the section so it will show the value instead of blank section....

Please check adn let me know if you need any help

Thanks
Sandeep
Varshitha KVarshitha K

Can you plzz write the code?
Pankaj_GanwaniPankaj_Ganwani
Hi Varshitha,

Not sure if you got the solution for your problem. If not, you can try to use below mentioned code to achieve this:

page
<apex:page controller="TestForlookup" id="pageId">
  <apex:form id="formId">
      <apex:inputField value="{!objAccount.ParentId}" id="selectAccount" />
      <apex:commandButton value="Add to list" onClick="return addtolist('{!$Component.selectAccount}')" /><br/>
      <apex:selectlist size="10" id="optionlist" style="width:200px;">
          <apex:selectOption itemvalue="" itemlabel="--None--"></apex:selectOption>
      </apex:selectlist>
  </apex:form>
  <script>
  function addtolist(selectedValue)
  {
       var listElement = document.getElementById('{!$Component.pageId:formId:optionlist}');
       var listItem = document.createElement('option');
       listItem.text = document.getElementById(selectedValue).value;
       listItem.value = document.getElementById(selectedValue+'_lkid').value;
       listElement.add(listItem);
       return false;
  }
  </script>
</apex:page>

Controller:
public class TestForlookup
{
      public Account objAccount {get;set;}
      public TestForlookup()
      {
           objAccount = new Account();
      }
}

I have done this for Account object, you can replace it with your Matrix object.

Thanks,
Pankaj
This was selected as the best answer
Varshitha KVarshitha K
Thank you.It's working.