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
renuamirenuami 

pageblockTable inputField

Can someone please assist me with this?

 

We are displaying inpufield in the pageblocktable.

when the form is saved, how can we pass the inputfield value for each row in the pagblocktable to the controller?

 

 

 

 

<apex:page controller="controller_test"> <apex:form>

<apex:pageblock>

<apex:pageBlockTable value="{!Contact_Results}" var="eve"> <apex:column> <apex:inputField value="{!eve.Email}" required="true"></apex:inputField> </apex:column> </apex:pageblocktable>

<apex:pageblockButtons>

<apex:commandButton action="{!save}" value="Save">

</apex:pageBlockButtons>
</apex:pageBlock>

</apex:form> </apex:page> public class controller_test { List<Contact> Contacts_info = new List<Contact>(); for(Contact c:[Select Id,Email from Contact where Id IN:WhoIds]) { Contacts_info.add(c); } } Public List<Contact> getContact_Results() { return Contacts_info; } }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Hi, I totally understand that you need to build a List of Contact records to feed the page.  I'm only saying that the code:

 

 

for(Contact c:[Select Id,Email from Contact where Id IN:WhoIds])
{
Contacts_info.add(c);
}
}

 can be replaced with:

 

Contacts_info = [Select Id, Email from Contact where Id in :WhoIds];

 

 

Regarding the Save.  In short, Yes, just updating the Contact would update the e-mail value.  However, you'll need to know which Contacts to update.

 

This would require keeping a copy of the Contacts_info List, and in the Save method you'd have to go through and compare the E-mails, building a new List of Contacts_To_Update as you go, and then issue the final Update command for that list.

 

So, instead of doing that, you might look into writing your controller as an Extension of the StandardController for a Contact.  Then, you just use the StandardControllers save() method and you're done.  Much easier.

 

 

 <apex:page standardController="Contact" extensions="Controller_Test">

 

 

public with sharing class Controller_Test { ApexPages.StandardController CONTROL; public Controller_Test(ApexPages.StandardController stdController) { this.CONTROL = stdController; } public PageReference save() {

  return CONTROL.save();

}

}

 

 

 Best, Steve.

All Answers

SteveBowerSteveBower

You don't have to, Salesforce is doing this for you behind the scenes.  However, I think your controller would be better served with:

 

 

public class controller_test { List<Contact> Contacts_info = [Select Id, Email from Contact where Id IN: WhoIds

 

public List<Contact> getContact_Results() { return Contacts_info; } }

 

// where is "WhoIds" defined?

 

 

renuamirenuami

Since we cannot directly retrieve, contact email from the event, thats the reason

we are again inserting them into the list.

 

So are you saying that just Update Contact in the save method should update the email value in the contact record?

 

 

 

 

public class controller_test { List<String> WhoIds = new List<String>(); List<Contact> Contacts_info = new List<Contact>(); stdate=Date.newInstance(datetime.now().adddays(-7).year(),datetime.now().adddays(-7).month(),datetime.now().adddays(-7).day()); enddate=Date.newInstance(datetime.now().adddays(-1).year(),datetime.now().adddays(-1).month(),datetime.now().adddays(-1).day()); events_info=[Select AccountId,What.Name,Who.Name,Who.Email,Who.Id from Event Where OwnerId = :UserInfo.getUserId() and ActivityDate >= :stdate and ActivityDate <= :enddate]; for(Integer i=0;i<events_info.size();i++) { WhoIds.add(events_info[i].Who.Id); } for(Contact c:[Select Id,Email from Contact where Id IN:WhoIds]) { Contacts_info.add(c); } } Public List<Contact> getContact_Results() { return Contacts_info; } public PageReference save() { //Here i have to update the contact email for each whoid (what ever is keyed in the VF page) } }

 

 

 

SteveBowerSteveBower

Hi, I totally understand that you need to build a List of Contact records to feed the page.  I'm only saying that the code:

 

 

for(Contact c:[Select Id,Email from Contact where Id IN:WhoIds])
{
Contacts_info.add(c);
}
}

 can be replaced with:

 

Contacts_info = [Select Id, Email from Contact where Id in :WhoIds];

 

 

Regarding the Save.  In short, Yes, just updating the Contact would update the e-mail value.  However, you'll need to know which Contacts to update.

 

This would require keeping a copy of the Contacts_info List, and in the Save method you'd have to go through and compare the E-mails, building a new List of Contacts_To_Update as you go, and then issue the final Update command for that list.

 

So, instead of doing that, you might look into writing your controller as an Extension of the StandardController for a Contact.  Then, you just use the StandardControllers save() method and you're done.  Much easier.

 

 

 <apex:page standardController="Contact" extensions="Controller_Test">

 

 

public with sharing class Controller_Test { ApexPages.StandardController CONTROL; public Controller_Test(ApexPages.StandardController stdController) { this.CONTROL = stdController; } public PageReference save() {

  return CONTROL.save();

}

}

 

 

 Best, Steve.

This was selected as the best answer