[SalesForce] Need to add a redirect URL to a Custom Button using Javascript

My apologies in advance for being a JavaScript newbie. I just discovered custom buttons could create new related records, and I managed to get that working with no problems. Now, once it's done creating that record, I want to send it to another (related) page. But I can't figure out the syntax of how to do it.

I'm working with a custom Object related to the Case object as Master Detail. I won't bore you with the other code, as it's working just fine. But once it does its thing, I want it to go back to the case record that it is related to.

So, for example…

{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')} 

//Code to do other stuff...

var mycaseid = "{My_Object__c.Case_ID__c}"

//where the Case_ID__c field is capturing the ID of the master Case that my custom object is related to//

parent.window.location = "https://na6.salesforce.com/"+mycaseid

I know this doesn't work, but I don't know what syntax I should use. How do I refer dynamically to the "https//na6.salesforce.com/" part of the url and how do I "paste" the case Id to it?

Best Answer

Most browsers have a console where you can view JavaScript errors. Right click on the page and inspect element, then click on the console tab

Personally, I would do this with a VisualForce Page which, being top level, would not have any framing issues and will clearly display any errors:

<apex:page standardController="Case"
    extensions="CreateCaseChildrenExtension"
    action="{!createChildren}">

    <apex:pageMessages />

</apex:page>

And a controller extension:

public with sharing class CreateCaseChildrenExtension()
{
    public PageReference redirect { get; private set; }
    public CreateCaseChildrenExtension(ApexPages.StandardController controller)
    {
        this.redirect = controller.view(); // If you want to redirect to the Case
    }

    public PageReference createChildren()
    {
        try
        {
            // insert children
        }
        catch (DMLException dmx)
        {
            ApexPages.addMessages(dmx);
            return null;
        }
        return redirect;
    }
}