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
Swetha A 5Swetha A 5 

how to pass parent objects's id to custom button dynamically

Hi All,

I have a custom button on task related list of Account Object.
The button contains the URL as:/apex/GetDynamoDBTasks?id={!Account.Id} Where GetDynamoDBTasks is a Visualforce page.
I want to pass id dynamically in place of {!Account.Id}. I mean as it is on Account object so I used, Account.Id. If I use this button On opportunity's task related list, it should take opportunity.Id and If I use it for contact, then the Id value should be contact.Id. That means the same custom button for all objects. How can I achieve this? Any solution or idea on this.
Best Answer chosen by Swetha A 5
Rajiv Penagonda 12Rajiv Penagonda 12
Swetha, you can change your button's content source to "OnClick JavaScript" and use the following code in it:
var recId = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
window.location = '/apex/GetDynamoDBTasks?id=' + recId;

User-added image

 Hope this helps.

All Answers

Tavva Sai KrishnaTavva Sai Krishna
Hi Swetha,

yes, you have to create different list buttons on each object. the type of the button is select as url,and by using the merge fieldUser-added image  selector you can select the account record id.

Regards,
Sai Krishna Tavva.
Rajiv Penagonda 12Rajiv Penagonda 12
Swetha, you can change your button's content source to "OnClick JavaScript" and use the following code in it:
var recId = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
window.location = '/apex/GetDynamoDBTasks?id=' + recId;

User-added image

 Hope this helps.
This was selected as the best answer
Swetha A 5Swetha A 5
Hi Rajiv,

I pasted your code but it is showing error. It is not showing my visualforce code. can you provide me some other solution please.

Thank you.
Rajiv Penagonda 12Rajiv Penagonda 12
Javascript based solution is the only one feasible for your case. Can you share what error you are getting?
Tavva Sai KrishnaTavva Sai Krishna
Hi Swetha,

In the Url of the button you can add the parameters for the page like 

/apex/GetDynamoDBTasks?accid={!Account.Id}&oppid={!Opportunity.id}&conid={!contact.id}

in the page controller of the "GetDynamoDBTask" page, you check with the parameter such as 

if(apexpages.currentpage().getparameters().get('accid')!=null)
{
  //you got the account record id. and assign it to the id of the variable you are using
}
if(apexpages.currentpage().getparameters().get('conid')!=null)
{
  //you got the contact record id. and assign it to the id of the variable you are using
}
if(apexpages.currentpage().getparameters().get('oppid')!=null)
{
  //you got the account opportunity id. and assign it to the id of the variable you are using.
}

Let me know , if you face any issues.

Regards,
Sai Krishna Tavva.
Rajiv Penagonda 12Rajiv Penagonda 12
Tavva Sai Krishna, what you have mentioned should work, however their is also the effort involved to cover the corresponding controller logic in a test class.
Swetha A 5Swetha A 5
Hi Rajiv,

I am getting this error:

Invalid id: ClientDetail?id=00119000006doek
Error is in expression '{!GetItemRequest}' in component <apex:page> in page getdynamodbtasks: Class.getDynamoDBTaskList.GetItemRequest: line 16, column 1
 
Tavva Sai KrishnaTavva Sai Krishna
Hi Rajiv ,

I hope that is not a big deal. we can create 3 test methods which sets each parameter of the url in each method by using apexpages.currentpage().getparameters.put(string,string), and will call the method as usual.

let me know if i didnt answer to your question.

Regards,
Sai Krishna Tavva.
Amit Chaudhary 8Amit Chaudhary 8
Hi Swetha,

Please try below java script code on button. I tested below code in my developer org which is working fine
var contId ='{!Contact.Id}';
var accId ='{!Account.Id}';
var OppID = '{!Opportunity.Id}';
var url = '';

if(contId != null && contId != '')
{
url = '/apex/GetDynamoDBTasks?id=' + contId + '&Type=Contact';
}
else if(accId != null && accId != '')
{
url = '/apex/GetDynamoDBTasks?id=' + accId + '&Type=Account';
}
else if(OppID != null && OppID != '')
{
url = '/apex/GetDynamoDBTasks?id=' + OppID + '&Type=Opportunity';
}

if(url != '')
{
window.parent.location.href = url;
}
User-added image

Let us knoow if this will help you
Richard Jimenez 9Richard Jimenez 9
Swetha,

Reading the Id from the URL solution (suggested by Rajiv) will not work if there is any text after the Id in the URL, for example if the user has clicked on a related list link the URL will be something like /apex/GetDynamoDBTasks?id=00Qb000000KASjj#00Qb000000KASjj_RelatedHistoryList_target. You will need to limit the substring to 15 characters. Its still a nasty hack and if the URL format changes this will be broken (not likely i know, but url hacks risk being broken when you least expect it).

For the second approach of using JS (suggeted by Amit), you'll need to be careful on the order of your 'IF' blocks because on a Contact record the Account.Id will also exist - so you'll need to ensure your object if blocks order is tested thoroughly. You will have the same issue with custom objects that have Master-Detail relationships and you could have more that one Id populated that you are looking for. Be carefull of any new or change of object relationships in the future that may break the order of id checks.

Separate buttons is cleanest option, but depending on how many objects you want this on could make it unmanageable.

Just some thoughts!
Richard.
Rajiv Penagonda 12Rajiv Penagonda 12
@Swetha, it looks like the control is reaching your visual force page from the button with the ID which you expected. Now there seems to be something wrong within your VF page. This is a different issue altogether.

On the surface it appears that the component "ClientDetail" within '{!GetItemRequest}' is expecting someother ID.

@Tavva Sai Krishna, yeah 3 test methods are no big deal, but it is extra code. Right?
Swetha A 5Swetha A 5
Thanks All for your responses. I fixed it. Though I changed the JavaScript code, But the answer given by Rajiv helped me in finding solution. Thanks @Rajiv
Tavva Sai KrishnaTavva Sai Krishna
Hi Rajiv,

obviously , it is extra code but as we can see common code in all 3 methods we can wrap up the code in a single method and can call the method from the three methods.
Also, as @amit , using the same logic written in javascript will work , then we didnt have get chance to work on test class.

Regards,
Sai Krishna Tavva.
Rajiv Penagonda 12Rajiv Penagonda 12
Swetha, do consider the point Richard mentioned above:

"...will not work if there is any text after the Id in the URL, for example if the user has clicked on a related list link the URL will be something like /apex/GetDynamoDBTasks?id=00Qb000000KASjj#00Qb000000KASjj_RelatedHistoryList_target.."

and update your code to:
var recId = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
recId = recId.substring(0, (recId.length < 15 ? recID.length : 15));
window.location = '/apex/GetDynamoDBTasks?id=' + recId;