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 

Create a custom button using Javascript to create a Task

However, I keep getting errors.
I am just trying to create a custom button that opens up a task window. Then the person fills things in and then clicks "save" and the task is created.
Here's what I have as Javascript...

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

  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";

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

   result[0].success == ("true"){ location.reload()};

Im just learning Javascript so I apologize for missing the obvious thing casuing me to get erros.
Thanks
Shawn
Best Answer chosen by Shawn Low 24
Raj VakatiRaj Vakati
Found the issue. .here is the working code
 
{!REQUIRESCRIPT("/soap/ajax/41.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/41.0/apex.js")}

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]);

if(result[0].getBoolean("success"))
{
   window.location.reload();

}

 

All Answers

Abdul KhatriAbdul Khatri
I don't see a code where you said 

create a custom button that opens up a task window

What Error you are getting?

I don't think Task has a status called "Not Started"


 
Gururaj BGururaj B
You mean the entire code that you given is written as Javascript? I dont think they are the right syntax on Javascript. you are using the Apex syntax into JS file?
Raj VakatiRaj Vakati
Your code is correct .. as we discussed on other thread its might be an issue with other code .. 

Can you give me a debug logs pls to check the issue .. ??
 
Shawn Low 24Shawn Low 24
Hey Raj,
I have no idea how to "debug" the code for the button. (disadvantage of just starting to learn how to write code)
The errors are definatly related to the code behind the button.
The current error message looks like this...
User-added image

I keep goign back through what I have and seeing if I can find the missing symbol like "{" but my inexperience is frustrating.
It seems so simple, all I want is a task screen to open, like it does when you click the "New Task" button on the "Activities" related list. Some of the fields will be populated for the sales people, and some will not. They fill in the blanks, and save the Task.
Should be a piece of cake but its turning into a mess.
What am I missing???
Thanks
Shawn
 
Raj VakatiRaj Vakati
Found the issue. .here is the working code
 
{!REQUIRESCRIPT("/soap/ajax/41.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/41.0/apex.js")}

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]);

if(result[0].getBoolean("success"))
{
   window.location.reload();

}

 
This was selected as the best answer
Shawn Low 24Shawn Low 24
Thanks Raj,
It now creates a Task, and thats awesome
I need the Task Window to stay open though, so the salesperson can enter in any notes and choose a due date.
Right now, the task is created and saved so there is no opportunity to add info, aside from re-opening the task and then entering in the info.
Is there a way to have the task window stay open instead of closing?

Thank you
Raj VakatiRaj Vakati
You need to use URL redirect after save was success 
Shawn Low 24Shawn Low 24
What do you mean I need to use a url to redirect after the save was successful?
Raj VakatiRaj Vakati
instead of  window.location.reload() use window.location.href ='........salesforcec.om/'+<RECORD ID> 
Shawn Low 24Shawn Low 24
Hey Raj,
On two of the lines, there is a "//" right before the "taskToCreate". Why is that?
Raj VakatiRaj Vakati
Here is the code
 
{!REQUIRESCRIPT("/soap/ajax/41.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/41.0/apex.js")}

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]);

if(result[0].getBoolean("success"))
{
alert(result);
   window.location.href='/'+result[0].get('id');

}

 
Shawn Low 24Shawn Low 24
Thank you for the code, it works fine now. I was just wondering why the two lines had those "//" (line # 06 & 07)
Is it becasue they are custom fields?
Raj VakatiRaj Vakati
Thos filed are not in my instance so  I commented .. your can uncomment those two lines