I have a custom Javascript button on Opportunity Layout that executes a method that creates a case. After the alert says "Case Created" and User clicked OK, I want the page to redirect to the Case that was created. Tried using "navigateToUrl" in javascript, could not get it to work. How can I achieve this?

var result = sforce.apex.execute("CreateCase","CreateCaseMethod",{oppId:'{!Opportunity.Id}'}); 
alert("Case Created");
  • 1
    Why Javascript?why not visualforce? – Mohith Shrivastava Apr 23 '13 at 18:16
  • @MohithKumar my requirements were to create a custom button on the Opportunity Layout. So that's what I did. How would i do this in Visualforce ? If you suggest i do so – user988 Apr 23 '13 at 18:20
  • What does your "Create Case" method return? use alert(result) or console.log(result) to see it (comment out the last line though or you'll lose the current context). Modify it to return either full Case record or just the Id and set the href appropriately? – eyescream Apr 23 '13 at 18:27
  • @eyescream it currently returns a Case – user988 Apr 23 '13 at 18:30
  • To answer how you would do this VF... In this case, it doesn't look like you actually want to land on the VF page, but rather you want to perform a bunch of logic in code and then land on the Case record in edit mode. You case object has some standard fields that you may want to complete in your code logic, like the Subject, Status, Due Date, etc. I don't see that in your JS. You can execute a VF controller method (make sure it's not in the constructor) with an action on the page itself. – greenstork Apr 23 '13 at 18:39

Is there anything "magical" in the "Create Case" method? I mean anything more than what you could achieve with "URL hacking" (prepopulation of fields on the New Case page layout, if needed you can even select Case record type and add &save=1 to immediately save the Case record. If there are complex queries behind then it might pay off to keep the logic in Apex but so far it looks pretty straightforward.

If you only need to pull data from Opportunity (and anything above it, such as Account), current user etc then it might be much simpler and faster to prepopulate the form than to send a webservice callout, wait for results, parse the results, redirect user to new Case's detail(?) page...

If you insist on the webservice route then the URL you have to navigate to is /500.... (Case Id) if you want to view the Case, /500.../e if you want to edit it etc.

  • nothing too magical. But there are things like assignment rules etc that come into play in this method. – user988 Apr 23 '13 at 18:53
  • /500/e?&cas21=1&retUrl=%2F{!Opportunity.Id}, next requirement plz ;) (of course assuming the checkbox is visible). It moght pay you off immensely to tell us what's the real end result you're after... detail screen? edit screen? edit after save? – eyescream Apr 23 '13 at 18:58
  • yep. So basically On Click of the button (on opportunity) it Creates the Case already, but i want the user to be taken to the Case Record page (in edit mode) so they can add a couple more details and just save the changes etc.. Thanks for the help! – user988 Apr 23 '13 at 19:03

You can always retrieve the Case Id on the var result variable and assign it to the wondow.location: "window.location = caseId;"

Your pages will navigate automatically to the case you have created.

Anyway, I do not recommend you to use a JS button because you can get unfriendly error messages due to some times outs or incompatibility with the browser.

VF button is much better and use a Controller to redirect the page to the new Case. But you would also need to take into account that SalesForce doesn't allow any action attribute on tag that performs a DML operation, so in your case, you would need a confirmation page and from this Confirm button call the method that creates the Case.

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.