You need to sign in to do that
Don't have an account?
Muhammad Sahil
hi, i want to select all the records which have checkbox checked in vf page and update their name to updated + name
vf page
<apex:page controller="AccountsSearchController" >
<div style="margin: 1%;">
<apex:form >
<apex:pageBlock >
<span>Account Name:</span>
<apex:selectList value="{!selectedAccountId}" size="1">
<apex:selectOptions value="{!accountOptions}" />
</apex:selectList>
<apex:commandButton action="{!getDetails}" value="Get Contacts"/>
<apex:pageBlockSection id="display" title="Related Contacts">
<apex:pageBlockTable value="{!relatedContacts}" var="contact" style="border:1px solid black;">
<apex:column >
<apex:facet name="header">
<input type="checkbox" id="checkAllBox" onclick="selectAllCheckboxes(this,'inputId')"/>
<script>
function selectAllCheckboxes(obj,receivedInputID){
var inputCheckBox = document.getElementsByTagName("input");
for(var i=0; i<inputCheckBox.length; i++){
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
inputCheckBox[i].checked = obj.checked;
global var i = obj.checked;
}
}
}
</script>
</apex:facet>
<apex:inputCheckbox id="checkboxShowP" value="{!isChecked}">
<apex:actionSupport event="onchange" action="{!click}"/>
</apex:inputCheckbox>
</apex:column>
<apex:column headerValue="Contact Name" style="width:200px;border:1px solid black;">
<apex:outputField value="{!contact.Name}" />
</apex:column>
<apex:column headerValue="Contact Email" style="width:200px;border:1px solid black;">
<apex:outputField value="{!contact.Email}" />
</apex:column>
<apex:column headerValue="Owner ID" style="width:200px;border:1px solid black;">
<apex:outputField value="{!contact.OwnerId}" />
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value="Update Selected"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</div>
</apex:page>
controller:
public with sharing class AccountsSearchController {
public List<Account> selectedAccount {get; set;}
public Boolean isChecked{ get; set; }
public List<Contact> relatedContacts {get; set;}
public List<Contact> SelectedContacts {get; set;}
public List<Contact> checkedCon {get; set;}
public List<Contact> i {get; set;}
public List<SelectOption> accountOptions {get; set;}
public String selectedAccountId {get; set;}
public String selected {get; set;}
List<Contact> contacts = new List<Contact>();
public AccountsSearchController() {
accountOptions = new List<SelectOption>();
for (Account acc : [SELECT Id, Name FROM Account]) {
accountOptions.add(new SelectOption(acc.Id, acc.Name));
}
}
public void getDetails() {
selectedAccount = [SELECT Id, Name, Phone, (SELECT Id, Name, Email, AccountId, OwnerId FROM Contacts) FROM Account WHERE Id = :selectedAccountId LIMIT 1];
relatedContacts = selectedAccount[0].Contacts;
}
public void click(){
for (Contact con : Contacts) {
if(isChecked==true){
con.LastName = 'Updated ' + contact.LastName;
contacts.Add(con);
} insert Contacts;
update relatedContacts;
}
}
}
<apex:page controller="AccountsSearchController" >
<div style="margin: 1%;">
<apex:form >
<apex:pageBlock >
<span>Account Name:</span>
<apex:selectList value="{!selectedAccountId}" size="1">
<apex:selectOptions value="{!accountOptions}" />
</apex:selectList>
<apex:commandButton action="{!getDetails}" value="Get Contacts"/>
<apex:pageBlockSection id="display" title="Related Contacts">
<apex:pageBlockTable value="{!relatedContacts}" var="contact" style="border:1px solid black;">
<apex:column >
<apex:facet name="header">
<input type="checkbox" id="checkAllBox" onclick="selectAllCheckboxes(this,'inputId')"/>
<script>
function selectAllCheckboxes(obj,receivedInputID){
var inputCheckBox = document.getElementsByTagName("input");
for(var i=0; i<inputCheckBox.length; i++){
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
inputCheckBox[i].checked = obj.checked;
global var i = obj.checked;
}
}
}
</script>
</apex:facet>
<apex:inputCheckbox id="checkboxShowP" value="{!isChecked}">
<apex:actionSupport event="onchange" action="{!click}"/>
</apex:inputCheckbox>
</apex:column>
<apex:column headerValue="Contact Name" style="width:200px;border:1px solid black;">
<apex:outputField value="{!contact.Name}" />
</apex:column>
<apex:column headerValue="Contact Email" style="width:200px;border:1px solid black;">
<apex:outputField value="{!contact.Email}" />
</apex:column>
<apex:column headerValue="Owner ID" style="width:200px;border:1px solid black;">
<apex:outputField value="{!contact.OwnerId}" />
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value="Update Selected"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</div>
</apex:page>
controller:
public with sharing class AccountsSearchController {
public List<Account> selectedAccount {get; set;}
public Boolean isChecked{ get; set; }
public List<Contact> relatedContacts {get; set;}
public List<Contact> SelectedContacts {get; set;}
public List<Contact> checkedCon {get; set;}
public List<Contact> i {get; set;}
public List<SelectOption> accountOptions {get; set;}
public String selectedAccountId {get; set;}
public String selected {get; set;}
List<Contact> contacts = new List<Contact>();
public AccountsSearchController() {
accountOptions = new List<SelectOption>();
for (Account acc : [SELECT Id, Name FROM Account]) {
accountOptions.add(new SelectOption(acc.Id, acc.Name));
}
}
public void getDetails() {
selectedAccount = [SELECT Id, Name, Phone, (SELECT Id, Name, Email, AccountId, OwnerId FROM Contacts) FROM Account WHERE Id = :selectedAccountId LIMIT 1];
relatedContacts = selectedAccount[0].Contacts;
}
public void click(){
for (Contact con : Contacts) {
if(isChecked==true){
con.LastName = 'Updated ' + contact.LastName;
contacts.Add(con);
} insert Contacts;
update relatedContacts;
}
}
}
To update the names of the selected contacts in your Visualforce page, you can modify your click method as follows:
In this code, you loop through the relatedContacts and add the selected ones (based on the isChecked field) to a separate list called checkedCon. Then you can update the checkedCon list.
You also need to add a getter and setter for the isChecked field in your Contact object:
Finally, you need to bind the isChecked value of the input checkbox to the isChecked property of the related contact:
To update the name of the selected records, you can add the logic to the click method in the controller. The updated name should be "Updated + original name". Here's the updated code:
Don't forget to add the "isChecked" property to the Contact class and bind it to the checkbox in the VF page.