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
Shannon Andreas 21Shannon Andreas 21 

Create Task Button - Saving before it is saved


would appreicate some guidance here. I have two custom buttons which pretty much do the same thing. One is on the Case object and one is on the Task object. Both buttons pre-populate information. The problem is the task button creates the tasks before save as well as on cancel. The case button throws an error (see below. I tried new code to see if I could correct the save issue).

Case Javascript button. Results insert task regardless of save or cancel.

var task = new sforce.SObject("Task"); 
task.Id = "{!Task.Id}"; 
task.Status = "Closed"; 
task.RecordTypeId = "01260000000Q7kI"; 
task.WhoId ="{!Case.ContactId}"; 
task.OwnerId = "{!Case.OwnerId}"; 
task.Priority ="Normal"; 
task.ActivityDate = new Date("{!TODAY()}"); 
task.Subject ="VOC VEP Case Assigned to You for Account {!Case.Account__c}"; 
task.Description="Please click" 

var resultTask = sforce.connection.update([task]); 


var qr = sforce.connection.query("SELECT Id, WhatId, WhoId FROM Task where Id='" + "{!Task.Id}" + "'"); 
var records = qr.getArray("records"); 
window.location = "/00T/e?who_id="+records[0].WhoId + "&what_id="+records[0].WhatId + "&retURL=%2F" + records[0].WhatId; 

alert("An Error has Occured. Error: " + e); 

Task Javascript button. Results in error: An Error has Occured. Error: TypeError: Cannot read property 'WhoId' of undefined


var tskObj = new sforce.SObject("Task"); 
tskObj.Status = "Not Started"; 
tskObj.RecordTypeId ="012Q0000000DIKh"; 
tskObj.WhoId ="{!Case.ContactId}"; 
tskObj.Priority ="Normal"; 
tskObj.Subject ="VOC VEP Follow Up Request"; 
tskObj.Description="Enter Issue Identification and Delivery Solution details in this section." 

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

if (result[0].success=="false") { 
} else { 
var newURL = "/" + result[0].id + "/e?retURL=%2F{!Case.Id} "; = newURL; 

Thanks for your help!

Shannon Andreas 21Shannon Andreas 21
CAn anyone help here? Please?