2

Good day everyone. I am trying to create a custom delete button which is similar to standard delete button(although there are few added functions).

How do I redirect the page to list view after deletion? At the moment I set the behavior as execute javascript and the content source as onclick javascript. I can get the current url using window.sfdcPage.entityId but since it is deletion, I want to get the list view url. Please help.

Here's the code for the custom button:

{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/29.0/apex.js')} 
sforce.apex.execute("ButtonClick", "delImportFile", {theId:window.sfdcPage.getEntityId()}); 
alert('Notice: This action may take some time to finish. Please refresh this page after a minute if changes on the values are not seen.'); 

/* 
  This is the part where I dont know what is the exact url 
  i've make it static at the moment
*/ 

window.location.href = "/a0c";
  • can you post your JS code – Rao Oct 8 '15 at 2:19
  • {!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')} {!REQUIRESCRIPT('/soap/ajax/29.0/apex.js')} sforce.apex.execute("ButtonClick", "delImportFile", {theId:window.sfdcPage.getEntityId()}); alert('Notice: This action may take some time to finish. Please refresh this page after a minute if changes on the values are not seen.'); /* This is the part where I dont know what is the exact url i've make it static at the moment'*/ window.location.href = "/a0c"; – Hope Oct 8 '15 at 2:27
  • Why are you using Javascript for a server side operation (deletion)? Any reason for not using Visualforce page that redirects to the right page after operation is completed? – Amr Ibrahim Oct 8 '15 at 2:28
  • @AmrIbrahim Because I am calling other classes after the deletion. – Hope Oct 8 '15 at 2:31
  • @Hope there is no reason you can't do it in controller. My opinion is to have these in controller ... no performance gained by using Javascript and controllers are easier to test, debug and guaranteed compatibility. – Amr Ibrahim Oct 8 '15 at 2:34
1

My own preference for creating custom button functionality is with Visualforce. I just have an aversion to eating my API calls when I don't need to. Although a delete use case is unlikely to be onerous.

I imagine you could attempt to redirect the user to the overview tab:

window.location.href = "/a0c/o";

Although I think this is what you get anyway with your current solution.

The ListView is referenced by the ListView metadata record ID when you select it directly like this:

window.location.href = "/a0c?fcf=00BB0000001FJpY";

My bigger question is this: why aren't you using a trigger for your custom on-delete logic?

If there is some kind of back-end processing that should take place on delete, that is what the trigger is for. You can always add the custom redirect anyway, but putting the logic in the trigger ensures that this rule is always followed (including when accessed through the API).

You're a little vague as to what extra processing you're doing, to perhaps I'm reading more into this, but in general you want to avoid custom CRUD logic anywhere but a trigger (unless trigger is not supported on that object).

  • Actually I've tried to create a trigger. But it is not working in my part since the method i am calling is asynchronous. In my case the method is not yet finish, while the deletion is already done. I am not sure if I am lacking something. Actually the method I am trying to call is a batch process. If I will use trigger again can you give me some samples on how to do it ? because in my case, It is not working. – Hope Oct 8 '15 at 10:05
  • Here's what I would suggest: create a new question. In that question, outline the exact problem you are trying to solve (why do you need to invoke batch from a trigger? why batch at all? what are the other things you are attempting to do on delete?). Make certain to include the code that you mention above. The more details you can give that pertain specifically to the question, the better. – pchittum Oct 8 '15 at 10:23
  • yeah you are right. This is the link salesforce.stackexchange.com/questions/95157/… – Hope Oct 8 '15 at 10:51
0

I found I idea how to do this.

sforce.connection.sessionId = '{!$Api.Session_ID}';
var res = sforce.connection.describeSObjects(['yourObjectName__c']);

var newId = res[0].keyPrefix;
window.location.href = "/"+newId;

Thanks for your help...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.