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

Hello!

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.

{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")} 
try{ 
var task = new sforce.SObject("Task"); 
task.Id = "{!Task.Id}"; 
task.Status = "Closed"; 
task.RecordTypeId = "01260000000Q7kI"; 
task.WhoId ="{!Case.ContactId}"; 
task.WhatId="{!Case.Id}"; 
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]); 

if(resultTask[0].success=="true"){ 

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; 

catch(e){ 
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

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

var tskObj = new sforce.SObject("Task"); 
tskObj.Status = "Not Started"; 
tskObj.RecordTypeId ="012Q0000000DIKh"; 
tskObj.WhoId ="{!Case.ContactId}"; 
tskObj.WhatId="{!Case.Id}"; 
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") { 
alert(result[0].errors.message); 
} else { 
var newURL = "/" + result[0].id + "/e?retURL=%2F{!Case.Id} "; 
window.top.location = newURL; 
}

Thanks for your help!

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