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
Harpreet On CloudHarpreet On Cloud 

Checkbox checked on page load in a list

Guys, I am little confused. I want to set checkbox on page load in a list of records.

 

I am rendering a list (of Engagement records) on a custom page. The first column of list is a checkbox. This checkbox should be checked on page load if Engagement record has atleast one related Potential record.

 

I tried using inputCheckbox and tried to use selected attribute but it did not work.

I tried using then HTML input checkbox field and tried to use 'checked'

<input id="{!engagement.Id}" value="{!engagement.Id}" type="checkbox" class="checkbox" name="ids" {!IF( ISBLANK(engagement.Potential__r), '', 'checked')}  />

 

 

but VisualForce starts giving error. I am not able to set checkbox on page load. Can anyone provide any clue of how this can be done.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You need somewhere to bind the value to, or you can't use the checkbox anyways, so a checkbox is appropriate. You'll need a wrapper class which can initialize the checkbox on load. Something like this:

 

<apex:pageBlockTable value="{!engagementList}" var="engagement">
<apex:column>
<apex:facet name="header">
<input type="checkbox" onchange="selectAll(this)" title="Select All" />
</apex:facet>
<apex:inputcheckbox value="{!engagement.selected}" />
</apex:column>
<!-- other columns can go here -->

</apex:pageBlockTable>
public class engagementwrapper {
  public engagementwrapper(engagement__c record) {
    this.record = record;
    selected = !record.potential__r.isempty();
  }
  public engagement__c record { get; set; }
  public boolean selected { get; set; }
}

The engagementList would be a List<EngagementWrapper>. Initialize this in your constructor or some function that loads a page worth of records at once (your choice), and create new instances of the EngagementWrapper for each row, adding it to the list. Remember to include Potential__r in your subquery so you don't get an error.

All Answers

Rakesh Aggarwal.ax1406Rakesh Aggarwal.ax1406

Try creating a roll up summary field (COuntPotentail__c) on engagement object with have count of child records. then you can use the same to check/uncheck your checkbox on the VF Page 

sfdcfoxsfdcfox

You need somewhere to bind the value to, or you can't use the checkbox anyways, so a checkbox is appropriate. You'll need a wrapper class which can initialize the checkbox on load. Something like this:

 

<apex:pageBlockTable value="{!engagementList}" var="engagement">
<apex:column>
<apex:facet name="header">
<input type="checkbox" onchange="selectAll(this)" title="Select All" />
</apex:facet>
<apex:inputcheckbox value="{!engagement.selected}" />
</apex:column>
<!-- other columns can go here -->

</apex:pageBlockTable>
public class engagementwrapper {
  public engagementwrapper(engagement__c record) {
    this.record = record;
    selected = !record.potential__r.isempty();
  }
  public engagement__c record { get; set; }
  public boolean selected { get; set; }
}

The engagementList would be a List<EngagementWrapper>. Initialize this in your constructor or some function that loads a page worth of records at once (your choice), and create new instances of the EngagementWrapper for each row, adding it to the list. Remember to include Potential__r in your subquery so you don't get an error.

This was selected as the best answer
Harpreet On CloudHarpreet On Cloud

Both replies are correct and can be used. However I used the implementation provided by sfdcfox. Thanks guys.