1

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?

1

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;
    }
}
0

It work in my scenario, perhaps you making any mistake before navigating to it. Other thing is the URL must match with the URL on which you are redirecting using Javascript:

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



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

parent.window.location.href = "https://huddle-idp-dev-ed.my.salesforce.com/home/home.jsp";

Create a custom detail button and change the "https://huddle-idp-dev-ed.my.salesforce.com" with instance URL your system has.

  • Thanks for the replies, Adrian Larson and @Ashwani. Unfortunately, I have no idea what to do with the console once I opened it, and I can't write Visualforce pages either. I'm not sure what is more ignorant than a newbie, but apparently, that's me. For what it's worth, if I added the last line in Ashwari's post to my code, it worked perfectly and sent me back to the home page. But as soon as I change the end of the url to the case Id, I get "unexpected token ILLEGAL". So it has something to do with how I'm referring to that case Id and how I'm "pasting" it on to the end of the URL. Ideas? – ATSDataJunkie Feb 16 '15 at 14:20
0

I'm still not sure why this worked, but I already had a variable defined that referred to the Case Id as part of the previous code. Since it was a related object (master-detail), in order to attach the newly created record to its parent, I had to set that object's parentId, which I did by creating the variable ct, and then setting the value for parentID. Then I created another variable for caseurl and set it using the ct.parentId, and it worked perfectly:

ct.parentId="{!My_Object__c.Case_ID__c}"; var caseurl = "https://cs12.salesforce.com/"+ct.parentId; window.location.href=caseurl

So it really was something to do with how I was referring to the object ID. I don't know why, and if anyone can shed light on that, I would very much appreciate it!

@Ashwani and @Adrian Larson

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.