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
GuyClairboisGuyClairbois 

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

 

Message Edited by GuyClairbois on 10-07-2009 02:32 AM
Best Answer chosen by GuyClairbois
HarmpieHarmpie

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>)

 

 

Message Edited by Harmpie on 10-07-2009 06:59 AM

All Answers

HarmpieHarmpie

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>)

 

 

Message Edited by Harmpie on 10-07-2009 06:59 AM
This was selected as the best answer
GuyClairboisGuyClairbois

Thanks Harmpie! You're the best :-)

 

HarmpieHarmpie
Love you too! :smileyvery-happy: