You need to sign in to do that
Don't have an account?
adrissel
Custom button validation and passing selected records
I have placed a custom button on a Case List view. The button, when clicked, needs to validate whether any case records were actually selected. That part is done simply with a Javascript OnClick button with the following code:
The problem is that when validation passes (i.e. it goes to the MyPage visualforce page) the selected records aren't passed along. Here is my VF page thus far:
I have searched the internet for hours on everything I could think of and couldn't find anything to help me.
Can anyone out there offer a solution?
THANKS!!
{!REQUIRESCRIPT ("/soap/ajax/13.0/connection.js")} var selected = {!GETRECORDIDS($ObjectType.Case)}; if (selected[0] == null) { alert("Please select at least one Case.") } else { window.location = 'apex/MyPage'; }
The problem is that when validation passes (i.e. it goes to the MyPage visualforce page) the selected records aren't passed along. Here is my VF page thus far:
<apex:page standardController="Case" recordSetVar="cases"> <apex:form > <apex:pageBlock title="MyPage" mode="edit"> <apex:pageBlockButtons > <apex:commandButton action="{!save}" value="MyButton"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Information" columns="2"> <apex:inputCheckBox value="{!case.test__c}" selected="true" required="true"/> <apex:pageBlockSectionItem /> <apex:inputField value="{!case.test2__c}" showDatePicker="true" required="true"/> <apex:inputField value="{!case.test3__c}" showDatePicker="true" required="true"/> </apex:pageBlockSection> <apex:pageBlockSection title="Selected Cases"> <apex:pageBlockTable value="{!selected}" var="case"> <apex:column value="{!case.casenumber}"/> <apex:column value="{!case.subject}"/> <apex:column value="{!case.status}"/> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>The goal is to have the custom button on the List view validate whether records are checked, then when validation passes have those records displayed in the block table on the VF page. I can get one or the other to work, but not both at the same time. If I change the button to go directly to the VF page, the selected records appear perfectly, but no validation occurs (i.e. it allows redirect when no records are selected). If I have the button set to the Javascript code it validates the records but doesn't display the selected records on the VF page.
I have searched the internet for hours on everything I could think of and couldn't find anything to help me.
Can anyone out there offer a solution?
THANKS!!
VF:
<apex:page standardController="Case" recordSetVar="cases" extensions="apxCaseStanSetController">
<apex:form >
<apex:pageBlock title="MyPage" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!doSave}" value="MyButton"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Case Information" columns="2">
<apex:inputField value="{!NewCase.Value2__c}" showDatePicker="true" required="true"/>
<apex:inputField value="{!NewCase.Value3__c}" showDatePicker="true" required="true"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Selected Cases">
<apex:pageBlockTable value="{!cases}" var="case">
<apex:column value="{!case.casenumber}"/>
<apex:column value="{!case.subject}"/>
<apex:column value="{!case.status}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public with sharing class apxCaseStanSetController {
ApexPages.StandardSetController setCon;
List<string> strIds = new List<string>();
List<Case> lstcase = new List<Case>();
public case c;
public Case getNewCase()
{
if(c==null)c= new case();
return c;
}
public apxCaseStanSetController (Apexpages.Standardsetcontroller cont)
{
setCon = cont;
strIds = ApexPages.currentPage().getParameters().get('recs').split(',',-2);
}
public List<Case> getCases()
{
lstcase = [select Subject,CaseNumber, Status,Id from Case where id IN: strIds ];
return lstcase;
}
public Pagereference doSave()
{
List<Case> updatecase = new List<Case>();
for(Case cse : lstCase)
{
cse.Value2__c = c.Value2__c;
cse.Value3__c = c.Value3__c;
updatecase.add(cse);
}
update updatecase;
return new Pagereference('/'+Case.getSObjectType().getDescribe().getKeyPrefix()+'/o');
}
}
All Answers
Below links can help you:
https://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_massupdate.htm
http://www.interactiveties.com/b_execute_javascript_button.php#.U6v7TPmSynZ
The problem is that I cannot do both at the same time. Ideally I would like the button to be a Javascript OnClick button (as in your second link above) to perform the validation, but then after validation passes based on the button code the selected records are passed to a VF page for displaying. Can this not be done?
Am I missing something obvious here? :(
I dont think you can do both at the same time. The current API doesnt support this since the controller reference is lost. But, here is a work around. You have to write an extension to the standard controller.
Please mark as best anwser if it works for you. It helps others.
Below is the code:
Button Code:
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Case)};
var updateRecords = [];
if (records[0] == null) {
alert("Please select at least one record to update.");
} else {
window.location = 'apex/vfcasepage?recs='+records.join(',');
}
VF Code:
<apex:page standardController="Case" recordSetVar="cases" extensions="apxCaseStanSetController">
<apex:form >
<apex:pageBlock title="MyPage" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="MyButton"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Selected Cases">
<apex:pageBlockTable value="{!cases}" var="case">
<apex:column value="{!case.casenumber}"/>
<apex:column value="{!case.subject}"/>
<apex:column value="{!case.status}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public with sharing class apxCaseStanSetController {
ApexPages.StandardSetController setCon;
List<string> strIds = new List<string>();
public apxCaseStanSetController (Apexpages.Standardsetcontroller cont)
{
setCon = cont;
strIds = ApexPages.currentPage().getParameters().get('recs').split(',',-2);
}
public List<Case> getCases()
{
return [select Subject,CaseNumber, Status from Case where id IN: strIds ];
}
}
Please mark as best anwser if it works for you. It helps others.
Amazing! Works perfectly! Thanks!!
One follow-up question if you don't mind. When I click to save the records on the VF Page the redirect is to the Home tab. Any way to go back to the Case List View page?
Also, is the "?recs='+records.join(',')" URL extension in some Apex documentation somewhere?
Thanks again!
Thoughts?
Cool, thanks! Here is the custom button code:
This works excellent, by the way, for pulling the records!Here is my controller extension:
Both of those working together work perfectly to display the records on the VF page. Love it!
Here is the VF page:
Here's where the issue and different behavior happens. This is what I notice:
(1) If I don't use the Javascript button, but instead just make it a VF page button that directs to that VF page, the field updates work perfectly!
(2) If I use the Javascript button, the records are displayed properly, but the two following things don't function properly:
--> a) The Fields don't update in the selected case records
--> b) The redirect page after submission isn't the case view list, it is the home tab (unlike the behavior without the Javascript button)
Am I missing something that needs to be there to update the field data?
Thanks again!!
VF:
<apex:page standardController="Case" recordSetVar="cases" extensions="apxCaseStanSetController">
<apex:form >
<apex:pageBlock title="MyPage" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!doSave}" value="MyButton"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Case Information" columns="2">
<apex:inputField value="{!NewCase.Value2__c}" showDatePicker="true" required="true"/>
<apex:inputField value="{!NewCase.Value3__c}" showDatePicker="true" required="true"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Selected Cases">
<apex:pageBlockTable value="{!cases}" var="case">
<apex:column value="{!case.casenumber}"/>
<apex:column value="{!case.subject}"/>
<apex:column value="{!case.status}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public with sharing class apxCaseStanSetController {
ApexPages.StandardSetController setCon;
List<string> strIds = new List<string>();
List<Case> lstcase = new List<Case>();
public case c;
public Case getNewCase()
{
if(c==null)c= new case();
return c;
}
public apxCaseStanSetController (Apexpages.Standardsetcontroller cont)
{
setCon = cont;
strIds = ApexPages.currentPage().getParameters().get('recs').split(',',-2);
}
public List<Case> getCases()
{
lstcase = [select Subject,CaseNumber, Status,Id from Case where id IN: strIds ];
return lstcase;
}
public Pagereference doSave()
{
List<Case> updatecase = new List<Case>();
for(Case cse : lstCase)
{
cse.Value2__c = c.Value2__c;
cse.Value3__c = c.Value3__c;
updatecase.add(cse);
}
update updatecase;
return new Pagereference('/'+Case.getSObjectType().getDescribe().getKeyPrefix()+'/o');
}
}
Hi Adrissel,
Did you get any solution for this test class. I have similar requiremnet and unbale to write test class. Please share the code if you have.
Thanks in advance.
Thanks,
Susheel Reddy
CONTROLLER:
VF Page:
CUSTOM BUTTONS:
Enable:
Disable:
Does that provide you with all the information you need? Let me know if you have questions!
Thanks for posting you code. Even I have written similar code. I need help with test class for this code.
Thanks,
Susheel Reddy
That help?
: That comment must be appended with today date and should be added to comment section
Next time another comment is added the previous comment should be added to a description filedSo on so forth
[4:42 AM, 8/9/2019] S: Where comment and description will be read only fields on the page
[4:42 AM, 8/9/2019] S: Comment is Rich text field