TransWikia.com

How can we use GetRecordIds in a VisualForce page

Salesforce Asked by SFDCLearner on December 31, 2021

How can we convert following javascript button to Visualforce page.Especially “GETRECORDIDS”,how can we write “GETRECORDIDS” logic in visualforce page.

I would like to convert following javascript button to visualforce page.

Please check the image i.e standard visualforce page. if the user checks the checkbox, I need that perticular “recordId” in controller.

Here

  • customobject__c.Id is parent
    selected contains all “child records”
    checkbox has been checked

enter image description here

 {!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
 {!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 
  {!REQUIRESCRIPT ("/soap/ajax/13.0/connection.js")} 
    var selected = {!GETRECORDIDS($ObjectType.Opportunity)};
  if (selected[0] == null) { 
   alert("Please select at one custom object Record."); 
    } else{ 
  window.location.href='/apex/TestUpdateInquiry?
   id='+'{!customobject__c.Id}'+'&selected='+selected;
     }

One Answer

You're probably going to need a wrapper class to store your custom objects data. A wrapper class is a simple apex class, which stores a record, and adds additional information and methods.

Heres a basic example:

public class Controller {
    public List<WrapperClass> wrappers; 

    public Controller() {
        // Fill via query 
        wrappers = new List<WrapperClass>(); 
    }

    public List<Id> GetSelectedWrappers() {
        List<Id> ids = new List<Id>();

        for (WrapperClass wrapper:wrappers) {
            if (wrapper.selected) {
                ids.add(wrapper.record.Id);
            }
        }

        return ids; 
    }

    public class WrapperClass {
        public sObject record { get; set; }
        public Boolean selected { get; set; }

        public WrapperClass(sObject record) {
            this.record = record; 
            selected = false; 
        }
    }
}

Inside your visualforce page, you'll need to expose a column for the user to interact with to select the wrappers.

<apex:repeat var="wrapper" value="{!wrappers}">
    <apex:column>
        <apex:inputCheckbox value="{!wrapper.Selected}" />
    </apex:column>
    <apex:column>
        <apex:outputField value="{!wrapper.Name}" />
    </apex:column>
</apex:repeat>

Now you've got a class which contains your sObjects data, while providing an extra field (Selected) for use on the page. You can call GetSelectedWrappers to get the selected wrappers on the page, and then perform contextual actions based on your business needs.

For another example, you can read this developer article, which has another good example of a wrapper class, and some extra info.

Answered by battery.cord on December 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP