You need to sign in to do that
Don't have an account?
Help with some simple AJAX re-rendering
Hello,
I have used various boards and forums to come up with a fairly simple output panel to rerender some areas of a VF page.
I have a created a tabbed contact view VF page. I have a 'Interventions' tab that lists the interventions (custom object) associated with this contact. Here is the code for the tab...
<apex:tab label="Interventions" name="Interventions" id="tabInterventions"> <apex:form> <apex:pageBlock title="Interventions"> <apex:dataTable value="{!MyInterventions}" var="int" columns="2"> <apex:column> <apex:facet name="header"><b>Name</b></apex:facet> <apex:commandLink action="{!invokeService}" value="{!int.name}" rerender="resultPanel"> <apex:param name="id" value="{!int.id}"/> </apex:commandLink> </apex:column> <apex:column headerValue="Current Stage"> {!int.Current_Stage__c} </apex:column> </apex:dataTable> </apex:pageBlock> </apex:form> <apex:pageBlock> <apex:outputPanel id="resultPanel" layout="block"> <apex:detail subject="{!fetchedData}"/> </apex:outputPanel> </apex:pageBlock> </apex:tab>
and here are the corresponding methods from the controller extension
public List<Intervention__c> getMyInterventions() { myInts = [select Name, id, Current_Stage__c from Intervention__c where Contact__c =:cntact.id]; return myInts; } public Intervention__c getFetchedData(){ return result; } public PageReference invokeService() { Id id = System.currentPageReference().getParameters().get('id'); result = [SELECT Name, Current_Stage__c FROM Intervention__c WHERE Contact__c=:id]; return null; }
This works wonderfully if I have only 1 intervention assiociated witht the contact, but if there is more than 1, I get the following error
System.QueryException: List has more than 1 row for assignment to SObject
I know what is causing my issue, my invokeService is returning a list of all the interventions associated with that Contact. I just don't know how to fix it. How do I make sure that I am querying only the intervention that was clicked on. How do I get that Id of the intervention that was clicked so that I can query for it in my invokeService method?
Or if I'm way off on all of this, how do I accomplish this better?
Also have a part 2 of the question.
Once this is fixed, how do I reset the output panel every time the user navigates away from teh interventions tab. Right now (assuming I have only 1 intervention for that contact, which is the only way it works) once I click on the intervention, the output panel re renders to that intervention. Which is great, but I want to make sure if they navigate to another tab on the tabpanel, that output panel resets itself, because right now it stays on the selected intervention. Basically I want to blank it out if the user navigates away from the interventions tab.
Thanks a million in advance for any help.
Chris
Hi,
I think you are passing the Intervention__c id through the parameter and making the query on Intervention__c object with contact__c= Intervention__c.id which is wrong.
So you have to make this as:
public PageReference invokeService() {
Id id = System.currentPageReference().getParameters().get('id');
result = [SELECT id,Name, Current_Stage__c FROM Intervention__c WHERE id=:id];
return null;
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
S Jain,
Thank you very much for the response. I actually half solved my own problem last night. I bypassed the getFetchedData(), and invokeService() methods altogether by just using {!$CurrentPage.parameters.id} as the subject of my related list. See Below.
I also tried your method which worked great as well, so thank you. I don't exactly know if there is an advantage of using one way over another so any advice on that would be great.
And also I am still stuck on 'blanking' the output panel if the user changes between tabs. I'm assuming I have to write some js function using the onclick event, but am stuck on how to accomplish this. Any help on this would be much appreciated.
Thanks,
Chris