You need to sign in to do that
Don't have an account?
Related list button with javascript - check if any records selected
Hi all,
I have a custom object Deal with Deal_Location child objects. I created a custom button 'Replace Deal Locations' as a List Button on Deal_Location and placed this button in the Deal screen, located with the Deal_Location related list.
I created a custom VisualForce page that opens when the button is clicked, displaying the selected Deal_Locations for further processing.
However, I'd like to do a quick check when a user clicks the button, on whether any Deal_Locations were actually checked. This can be done e.g. with the following Javascript:
var records = {!GETRECORDIDS($ObjectType.Deal_Location__c )};
if (records[0] == null) {
alert("Please select at least one Deal Location.") }
else {
window.parent.location.href='/apex/Deal_Location_Update';
}
The problem is that when I open the VisualForce page with the window.parent.location.href command, I loose the reference to the Deal_Location controller, so my VisualForce page has no idea of what Deal_Locations to show.
How can I solve this?
An option would be to only do the check in VisualForce, but this is a huge performance killer as it will load all the VisualForce code for nothing, and will also need a page reload for the original Deal page.
Many thanks in advance,
Guy
You can solve this as follows.
1) Change the javascript in button to:
var records = {!GETRECORDIDS($ObjectType.PRATOS_Deal_Location__c )};
if (records[0] == null) {
alert("Please select at least one Deal Location.") }
else {
window.parent.location.href='/apex/Deal_Location_Update?r='+records;
}
2) In your controller read the selected Id's:
public String paramString {
get; set;
}
public List<String> params {
get; set;
}
Constructor:
....
PageReference testRef = ApexPages.currentPage();
Map<String,String> m = testRef.getParameters();
String s = m.get('r');
// Contains all Id's comma separated
this.paramString=s;
this.params = this.paramString.split(',',0);
....
The variable this.params will now contain a list with the selected Id's (List<String>)
All Answers
You can solve this as follows.
1) Change the javascript in button to:
var records = {!GETRECORDIDS($ObjectType.PRATOS_Deal_Location__c )};
if (records[0] == null) {
alert("Please select at least one Deal Location.") }
else {
window.parent.location.href='/apex/Deal_Location_Update?r='+records;
}
2) In your controller read the selected Id's:
public String paramString {
get; set;
}
public List<String> params {
get; set;
}
Constructor:
....
PageReference testRef = ApexPages.currentPage();
Map<String,String> m = testRef.getParameters();
String s = m.get('r');
// Contains all Id's comma separated
this.paramString=s;
this.params = this.paramString.split(',',0);
....
The variable this.params will now contain a list with the selected Id's (List<String>)
Thanks Harmpie! You're the best :-)