I have the following page that shows documents from database, what I'm trying to accomplish is to make this page refresh or update automatically if a new document is added in the database. Is there a way I can use AJAX or pulling or something in my controller or page to accomplish that ?
Page:
<apex:pageBlockTable value="{!docs}" var="d" rendered="{!NOT(ISNULL(docs))}" Title="Documents">
<apex:column headerValue="Name">
<apex:outputText value="{!d.Name}"/>
</apex:column>
</apex:pageBlockTable>
Contoller
public List<FTPAttachment__c> getDocs()
{
docs= [Select Name from FTPAttachment__c where Case__c = :cse.id];
return docs;
}
Sounds like you're looking for the <apex:actionPoller> tag:
<apex:actionPoller action="{!refreshDocs}" rerender="docsTable" interval="5" />
<apex:pageBlockTable id="docsTable" value="{!docs}" var="d" rendered="{!NOT(ISNULL(docs))}" Title="Documents">
<apex:column headerValue="Name">
<apex:outputText value="{!d.Name}"/>
</apex:column>
</apex:pageBlockTable>
You could have the refreshDocs() method explicitly repopulate the docs list, but since you're already doing that in your getter (which will be called when the table is re-rendered), this method can just return without doing anything special:
public List<FTPAttachment__c> getDocs() {
return [Select Name from FTPAttachment__c where Case__c = :cse.id];
}
public PageReference refreshDocs() {
return null;
}
Alternatively you could use the streaming api.
http://www.salesforce.com/us/developer/docs/api_streaming/index.htm
Related
I'm trying to display values of a List<Map<String, Object>> in a Visualforce page by using apex:pageBlockTable.
I'm stuck on what to put in apex:outputField value like below.
Is there any way that this can be made to work? The number of Map<String, Object> is variable.
apex:
String theJsonString = '[{"id":1, "name":"Abc_SS", "description":"Abc", "address":"Abc"}, {"id":2, "name":"sales", "description":"sales", "address":"Abc"}]';
Object theJsonObject = JSON.deserializeUntyped(theJsonString);
List<Object> theJsonList = (List<Object>) theJsonObject;
List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();
for (Object obj : theJsonList) {
theJsonMapList.add((Map<String, Object>) obj);
}
visualforce page:
<apex:pageBlockTable value = "{!theJsonMapList}" var="al">
<apex:column headerValue="id">
<apex:outputField value=??>
</apex:column>
<apex:column headerValue="name">
<apex:outputField value=??>
</apex:column>
<apex:column headerValue="discription">
<apex:outputField value=??>
</apex:column>
<apex:column headerValue="address">
<apex:outputField value=??>
</apex:column>
</apex:pageBlockTable>
You can't use outputField at all because it's "magic". It works only with real database sobjects (Account, Contact, custom objects...) because it then can read what's the field type and how to properly display it (lookup - make a link. Date - format. etc)
Your data is unrelated, bunch of generic Objects so you're limited to outputText etc.
This should work nicely:
public class Stack73773910 {
public List<Map<String, Object>> getData(){
String theJsonString = '[{"id":1, "name":"Abc_SS", "description":"Abc", "address":"Abc"}, {"id":2, "name":"sales", "description":"sales", "address":"Abc"}]';
Object theJsonObject = JSON.deserializeUntyped(theJsonString);
List<Object> theJsonList = (List<Object>) theJsonObject;
List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();
for (Object obj : theJsonList) {
theJsonMapList.add((Map<String, Object>) obj);
}
return theJsonMapList;
}
}
<apex:page controller="Stack73773910">
<apex:pageBlock>
<apex:pageBlockTable value = "{!data}" var="al">
<apex:column headerValue="id">
<apex:outputText value="{!al['id']}"/>
</apex:column>
<apex:column headerValue="name">
<apex:outputText value="{!al['name']}"/>
</apex:column>
<apex:column headerValue="description" value="{!al['description']}"/> <!-- you can cheat if you don't need special output like number / date / checkbox formatting -->
<apex:column headerValue="address" value="{!al['address']}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
In the long run... as your JSON grows more complex and maybe you start using it in Apex for more things, not just displaying it... You'll grow angry with all the casting to Date , Boolean, String, Decimal you'll have to do to access the data. There are tools to help make you a helper class to hold this data as a proper object, not a stupid Map<String, Object>.
Have a look at https://json2apex.herokuapp.com/. It will let you have a List<MyWrapper> - parsing it should be similar complexity to what you have now but then in Visualforce you'll be back to old, simple {!al.name}
I want to add a custom VF component to display the batch job details in a pageblock table. However my component aint saving, it says: Error Error: Read only property 'c:batchDetailsComponent.BatchJobDetails'
Please help.
This is the visualforce component:
<apex:component controller="BatchOpportunityDetailsExtension">
<apex:attribute name="batchJob" type="List" assignTo="{!BatchJobDetails}" description="" />
<apex:form >
<apex:pageBlock>
<apex:pageblockTable value="{!batchJob}" var="batch">
<apex:column value="{!batch.CompletedDate}"/>
<apex:column value="{!batch.JobItemsProcessed}"/>
<apex:column value="{!batch.NumberOfErrors}"/>
</apex:pageblockTable>
</apex:pageBlock>
</apex:form>
</apex:component>
VF Page:
<apex:page standardController="Opportunity_Scheduled_Information__c"
extensions="BatchOpportunityDetailsExtension">
<c:oppScheduleComponent componentValue="{!batchJob}"/>
</apex:page>
Controller:
public class BatchOpportunityDetailsExtension {
public List<AsyncApexJob> batchJobDetails = new List<AsyncApexJob>();
public Opportunity_Scheduled_Information__c pageController {get;set;}
public BatchOpportunityDetailsExtension() {}
public BatchOpportunityDetailsExtension(ApexPages.StandardController controller) {
controller.addFields(new List<String>{'Total_Amount__c', 'Number_of_Opportunities__c'});
pageController = (Opportunity_Scheduled_Information__c)controller.getRecord();
BatchJobDetails = [ SELECT id,ApexClassID,CompletedDate,JobType,JobItemsProcessed,NumberOfErrors,MethodName,Status,ExtendedStatus,TotalJobItems FROM AsyncApexJob WHERE ApexClassId='01p7F000000bKIlQAM' LIMIT 50] ;
}
public List<AsyncApexJob> getBatchJobDetails()
{
return BatchJobDetails ;
}
}
You need to set the access for your component to global.
Like this: <apex:component access="global" controller="BatchOpportunityDetailsExtension">
I have a visualforce page that has a picklist called Topic and sometimes I will need to select one of the picklist options upon page load (meaning the Topic will be passed on from another page and will need to be selected upon loading the page for the first time). I'm not sure how to do this? I'm posting part of the Visualforce page that handles topic selection and the Controller code that below. Any help would be appreciated.Thanks.
Visualforce page:
<!---------------------------------- Select Topic ----------------------------------------------------->
<apex:pageblockSection title="Select the Topic" >
<apex:selectList value="{!topic}" size="1">
<apex:outputlabel for="Topic" value="Pick a Topic :" ></apex:outputlabel>
<apex:selectOptions id="topic" value="{!Topics}"/>
<apex:actionSupport action="{!populateParameters}" reRender="parametersSection,querySection" event="onchange"/>
</apex:selectList>
</apex:pageblockSection>
<!---------------------------------- End of Select Topic ---------------------------->
<!---------------------------------- Parameters for Topic ----------------------------------------------------->
<apex:pageblockSection id="parametersSection" title="Input Parameters">
<apex:repeat value="{!topicpParamWrapperList}" var="params">
<apex:outputPanel >
<apex:outputlabel value="{!params.parameter.Name}" ></apex:outputlabel>
<apex:inputfield value="{!params.parameter.inputValue__c}" rendered="{!params.renderAsText}">
<apex:actionsupport action="{!placeValuesInQuery}" reRender="querySection,splunUrlLink" event="onchange"/>
</apex:inputfield>
<apex:inputfield value="{!params.parameter.DateTimeValueHolder__c}" rendered="{!params.renderAsDate}">
<apex:actionsupport action="{!placeValuesInQuery}" reRender="querySection,splunUrlLink" event="onchange"/>
</apex:inputfield>
</apex:outputPanel>
</apex:repeat>
</apex:pageblockSection>
<!---------------------------------- End of Parameters for Topic ----------------------------------------------------->
Apex Controller
public List < topicpParamWrapper > topicpParamWrapperList {
get;
set;
} {
topicpParamWrapperList = new List < topicpParamWrapper >();
}
public void populateParameters()
{
if(!topicpParamWrapperList.isEmpty())
{
topicpParamWrapperList.clear();
}
if(topic!='' && topic!=Null)
{
for(Query_Parameter__c qParam :[select id, Parameters__r.Variable_Name__c, Parameters__r.Type__c,Parameters__r.Name from Query_Parameter__c where Topics__c=:topic])
{
Parameters__c param = new Parameters__c();
param.Name =qParam.Parameters__r.Name ;
param.type__c = qParam.Parameters__r.type__c;
param.Variable_Name__c=qParam.Parameters__r.Variable_Name__c;
topicpParamWrapperList.add(new topicpParamWrapper(param));
}
getQueryToRun();
}
}
public void getqueryToRun(){
if(mapTopics.containsKey(topic))
{
this.queryToRun =mapTopics.get(topic).query__c;
this.queryMain=mapTopics.get(topic).query__c;
}
}
public List < topicpParamWrapper > paramList {
get;
set;
} {
paramList = new List <topicpParamWrapper>();
}
All you really have to do is to set the topic to some initial value in the constructor (the special function that has name identical to class' name). You set it to some value and then it'll be rendered properly in visualforce (assuming same value is one of the selectable options!).
You have omitted the constructor or <apex:page> tag so we don't know how you're navigating to that page. But probably easiest for you would be to pass the topic in the URL. So if you access the page like that:
/apex/MyPage?topic=Something, something
then in the constructor you could do this:
topic = ApexPages.currentPage().getParameters().get('topic');
(the name of the URL parameter doesn't have to be same as the variable name but it makes sense to have them at least similar)
You can read more about getParameters()
If there is risk that your topic will contain &, spaces etc you probably should URLENCODE it when building the link.
Lets assume I have a Page code:
<apex:pageBlockTable value="{!allContacts}" var="c" >
<apex:column value="{!c.id}" headervalue="ID"/>
<apex:column value="{!c.FirstName}" headervalue="First Name"/>
<apex:column value="{!c.LastName}" headervalue="Last Name"/>
<apex:column value="{!c.Title}" headervalue="Title"/>
<apex:column value="{!c.Company}" headervalue="Company"/>
<apex:column>
<apex:commandButton action="{!addToRecruits}" value="Recruit">
<apex:param assignTo="{!leadID}" name="leadID" value="{!c.id}"/>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
And relevant controller :
public String leadID { get; set; }
public PageReference addToRecruits() {
System.debug('LeadID is: ' + leadID);
List<Lead> potentialCandidate = [SELECT id, FirstName, lastName, Title, Company FROM Lead WHERE id = :leadID];
delete potentialCandidate;
return null;
}
It seems that I can NOT pass leadID to addToRecruits() method. Do you have any idea why is so?
UPDATE:
I could manage to solve it. Instead of querying using SOQL, I approached with this style:
public String leadID { get; set; }
public PageReference addToRecruits() {
Lead candidate=new Lead(id=leadID);
....
}
Looks like the infamous platform bug? where apex:param values are not always send to the controller with apex:commandButton (though they are send with apex:commandLink).
A simple overview of the issue and possible workarounds are summarised by Jeff Douglas here: http://blog.jeffdouglas.com/2010/03/04/passing-parameters-with-a-commandbutton/
Add "rerender" attribute to apex:commandButton and its will start working - Something like
<apex:commandButton rerender="myForm" action="{!addToRecruits}" value="Recruit">
There is one way to pass parameter to controller but it will use commandLink also. I mean we need to use
command link and command button
Eg:
<apex:commandLink action="{!applyNow}" id="applybuttonLink" style="text-decoration:none">
<apex:commandButton value="Apply now"/>
<apex:param name="passId" assignTo="{!passId}" value="{!Vac.id}"/>
</apex:commandLink>
Controller:
public String vacancyId{get;set;}
Hi I am getting a headache with the visualforce repeater control:
<apex:repeat value="{!productDetails}" var="o">
<apex:pageBlockSectionItem >
<apex:outputText style="label" value="Name:"></apex:outputText>
<apex:outputLabel value="{!o.name}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="{!o.ProductCode}"/>
<apex:outputText value="{!o.Family}" />
</apex:pageblockSection>
<apex:pageBlockSection>
<apex:outputText style="label" value="Quantity:"> </apex:outputText>
<apex:inputText value="{!theList}"/>
</apex:pageblockSection>
</apex:repeat>
What I am trying to do, is for each record in the product details list, generate a text box. This text box is bound to another field (theList.quantity). But I am finding that when I change the value in the last text box it sets the quantity in all the text boxes(obviously as they are bound to the same field).
So my question is whats the best way to have each textbox that is generated in the repeater have its own parameter value?
Or am I using the repeater in the wrong way?
EDIT(clarification):
For each product detail record(What I am iterating through) I want to have a textbox where a user can enter the quantity of products. The value of this textbox is unrelated to the product detail records.
My question is how can I generate unique parameters for each iteration of the textbox? Hopefully that makes sense.
Cheers
You need to use the var in the <apex:repeat> for each element in the productDetails list. For example:
<apex:repeat value="{!productDetails}" var="productDetail">
<apex:pageBlockSection>
<apex:outputText style="label" value="Quantity:"> </apex:outputText>
<apex:inputText value="{!productDetail.quantity}"/>
</apex:pageblockSection>
</apex:repeat>
That will set the the quantity property of each productDetail.
If you're really iterating over a parent of the productDetail in this example, then you'll need to change your controller to create a parent for each and then iterate over that. I'll write the example code as if you're iterating over a list of potential orders.
In your controller, you'll need to create an order for each of the products. I'm not sure if the parent is an SObject or a custom class, but I'll write the example as if it was a custom class.
public class Order {
public Order(ProductDetail productDetail) {
this.productDetail = productDetail;
}
public ProductDetail productDetail;
public Integer quantity;
}
// I assume you've already implemented this getter or are setting it some other way.
public ProductDetail[] productDetails {
get {
if (productDetails == null) {
productDetails = ...;
}
return productDetails;
}
set;
}
public Order[] orders {
get {
if (orders == null) {
orders = new Order[]{};
for (ProductDetail productDetail: productDetails) {
orders.add(new Order(productDetail);
}
}
return orders;
}
set;
}
Now, in your VisualForce page you can iterate over the Order list and have the user set the quantity.
<apex:repeat value="{!orders}" var="order">
...
<apex:outputtext style="label" value="Name:"></apex:outputText>
<apex:outputLabel value="{!order.productDetail.name}" />
...
<apex:inputText value="{!order.quantity}"/>
....
</apex:repeat>
Back in your controller, only save the orders that have a quantity of more than zero (or whatever other criteria you have in mind).
The easiest way to do this is to add a custom field to the object you're repeating through. However, I understand there are cases where that's not desirable.
You probably could use a custom Apex object (not custom Salesforce object) for this. You can do this by adding a nested class within your Controller or Extension.
For example:
public class MyClass
{
public class MyProductDetail
{
public string Name {get;set;}
public string Family {get;set;}
public string ProductCode {get;set;}
public integer Quantity {get;set;} // theList
}
public List<MyProductDetail> MyProductDetails {get;set;}
}
You would need to loop through all of your Product detail records (returned from SOQL), and add them to the MyProductDetails list. Once you have that, though, you can use a Visualforce Repeater to display each of them them and save the inputted quantity data for each record.
<apex:repeat value="{!MyProductDetails}" var="o">
<apex:pageBlockSectionItem >
<apex:outputText style="label" value="Name:"></apex:outputText>
<apex:outputLabel value="{!o.Name}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="{!o.ProductCode}"/>
<apex:outputText value="{!o.Family}" />
</apex:pageblockSection>
<apex:pageBlockSection>
<apex:outputText style="label" value="Quantity:"> </apex:outputText>
<apex:inputText value="{!o.Quantity}"/>
</apex:pageblockSection>
</apex:repeat>
Hope that helps!