0

This is in classic. Also I'm pretty new to SF development.

I'm trying to do something very similar to this question. I've created a custom button, which I want to go to a custom page layout for a Task, and I've associated a Task record type with this page layout. I'm trying to get the javascript to 'pre-select' a record type for the new task, so that it will go to the page layout for that record type.

But when I click on the button, I get the generic 'problem with OnClick Javascript...Unexpected token ;'

Here is my javascript for this custom button:

!REQUIRESCRIPT("/soap/ajax/43.0/connection.js")

var taskObj = new sforce.SObject("Task");
var qr = sforce.connection.query("SELECT Id FROM RecordType WHERE SobjectType ='" + "Task" + "'" + " AND DeveloperName = '" + "My Record Type" + "'"); 
taskObj.RecordTypeId = qr.records.Id;
window.location = {!Task.Id};

I also tried using $URLFOR but couldn't get that to work either.

2
  • Is it classics or lightning? Oct 29, 2018 at 20:15
  • classic. I've updated the original question to mention that
    – user26270
    Oct 29, 2018 at 20:49

1 Answer 1

0

Your problem is in this line: window.location = {!Task.Id};

However, that the TaskId, by itself, is not a valid url. You probably want your line to read:

window.location.href = '/' + taskObj.Id;, thus replacing the standard id you used with the id of the object you just created.

The / at the beginning will make this a good URL to navigate to.

Overall, your snippet should look like this

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

var taskObj = new sforce.SObject("Task");
var qr = sforce.connection.query("SELECT Id FROM RecordType WHERE SobjectType ='Task' AND DeveloperName = 'My Record Type'"); 
taskObj.RecordTypeId = qr.records.Id;
window.location.href = '/' + taskObj.Id;
1
  • Thanks. It doesn't throw an error, but resulted in a page that says 'URL no longer exists'. And the current url says ....my.salesforce.com/undefined
    – user26270
    Oct 30, 2018 at 14:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .