function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shawn Low 24Shawn Low 24 

Receiving an URL error on a custom button.

I built a custom button in my Dev sandbox that is supposed to open a Task window and pre-populate several fields. The sales person then selects a due date and adds notyes and saves. Pretty simple. I am using Javascript and here is my code.
It is a "Detail Page Button" and the Behavior is "Execute Javascript". The Content Source is "OnClickJavascript"


var taskToCreate = new sforce.SObject("Task");
taskToCreate.OwnerId = "{!$User.Id}";
taskToCreate.Task_Type__c = "Community Appointment from NHS";
taskToCreate.Subject = "Community Site Visit";
taskToCreate.Division__c = "Pardee Homes - Inland Empire";
taskToCreate.WhoId = "{!Lead.Id}";
taskToCreate.Priority = "Normal";
taskToCreate.Status = "Not Started";

var result = sforce.connection.create( [ taskToCreate ] );

window.parent.location.href = "/" + result[0].id + "/e";

In my Dev Sandbox, is works great. I pushed out to the Full Sandbox for testing and received this error...
User-added image
Im not using a URL
I have been going a little crazy trying to figure this out.
Why is this not working in the Full Sandbox, but not in my Dev Sandbox?
Raj VakatiRaj Vakati
If i understand correctly, You code is not inserting successfully in full sandbox may because if any triggers or workflow or process builder or may some other code or config ..

please add alter statement before code and see
var result = sforce.connection.create( [ taskToCreate ] ); 

Shawn Low 24Shawn Low 24
Thanks Raj
Still getting an error, Looks like this...

User-added image
Raj VakatiRaj Vakati
Are you using it on the lightning screen ??
Shawn Low 24Shawn Low 24
Nope, standard SFDC.
Raj VakatiRaj Vakati
Can you debug where you are getting error .. with alerts and debug logs 
Shawn Low 24Shawn Low 24
Just figured out that there is a Validation Rule that was created a few days ago (6/6/18). This was after I created this buttonand had tested it in my Sandbox, (which is why it worked there) but it was before the QA team started testing this button in the Full Sandbox.
The Validation Rule is requiring a value be selected for the field called "Community Site" (look up). But this is why I need the Task screen to open, so the sales person can select the Community of Interest.
If I turn this Validation rule off, and simply make the field a "Required" field, do you believe the button will work?
Thansk Raj,
Shawn Low 24Shawn Low 24
So Tried that and still getting the "Array Element at 0 is null" error.
What does that error message mean?
Shawn Low 24Shawn Low 24
Hey Raj,
Ok, so I have been doing some back tracking and I think I have most of the issues figured out, except one.
I am hoping you can explain things for me.
   This is a "Detail Page Button" and the "Behavior" is Javascript.
   The button is not working and showing a error that says "URL No Longer Exists"
   I am learning to write code, so I apologize for my ignorante questions, where is this code trying to reference a URL? Is it in the last two (2) lines of code? Most of the code behind the button are just opening up the task and pre-setting the values for several fields. The only lines that are not doing those things are the first 2 lines and the last two lines.One of those four (4) lines is referencing a URL, correct?
1. Which one(s)
2. Can you offer a suggestion to adjust to code so it references the right URL?

Raj VakatiRaj Vakati

var taskToCreate            = new sforce.SObject("Task");
  taskToCreate.OwnerId        = "{!$User.Id}";
   //taskToCreate.Division__c    = "{!User.Division}";
   //taskToCreate.Task_Type__c   = "Community Appointment from NHS";
   taskToCreate.Subject        = "Community Appointment from NHS";
   taskToCreate.WhoId          = "{!Lead.Id}";
   taskToCreate.Priority       = "Normal";
   taskToCreate.Status         = "Not Started";

var result = sforce.connection.create([taskToCreate]);