+ Start a Discussion
kelsey.joyce1.394641015380688E12kelsey.joyce1.394641015380688E12 

Javascript Button Issue

I am trying to get a Javascript button to work - it is working when it comes up updating the lead fields however the task is not being created and the page is not being reloaded.  Any ideas where I am going wrong?

{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')}

var lead = new sforce.SObject('Lead');
lead.id ='{!Lead.Id}';
lead.strike__c = '1';
lead.status = 'Contact Not Made'
lead.Lead_Status_Details__c = 'Called LM & Emailed'

try{
var result = sforce.connection.update([lead]);

var task = new sforce.SObject('Task');
task.RecordTypeId = '012D0000000B9be';
task.OwnerId = '{!User.Id}';
task.Subject = 'No Connect';
task.Description = 'Left Message and Sent Email';
task.WhatId = '{!Lead.Id}';
task.Type = 'No Connect';
task.ActivityDate = new Date();
task.Status = 'Completed';

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

if(resultTask[0].success=='true' && resultLead[0].success=='true'){
alert('The Lead has been Updated Successfully and the Task was Created.');
location.reload();
}
}
catch(e){
alert('An Error has Occured. Error: ' + e);
}
Coco_SdyneyCoco_Sdyney
WhoID refers to people things.  So that would be typically a Lead ID or a Contact ID
WhatID refers to object type things.  That would typically be an Account ID or an Opportunity ID

On your case , you will need to write LeadId into WhoId instead of whatId, please try.
kelsey.joyce1.394641015380688E12kelsey.joyce1.394641015380688E12
Hi Coco - now I get the following error.  I used WhoId on another custom button and it worked fine?User-added image
Coco_SdyneyCoco_Sdyney
Looks like you referenced wrong field name, search your code, you may find 'LeadId', be sure to write code as: task.WhoId=''{!Lead.Id}'
kelsey.joyce1.394641015380688E12kelsey.joyce1.394641015380688E12
Yes that was the original code I had, see below.  Issue with the below is that the task is not being logged and page is not refreshing but lead field updates are being made.

{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')}

var lead = new sforce.SObject('Lead');
lead.id ='{!Lead.Id}';
lead.strike__c = '1';
lead.status = 'Contact Not Made'
lead.Lead_Status_Details__c = 'Called LM & Emailed'

try{
var result = sforce.connection.update([lead]);

var task = new sforce.SObject('Task');
task.RecordTypeId = '012D0000000B9be';
task.OwnerId = '{!User.Id}';
task.Subject = 'No Connect';
task.Description = 'Left Message and Sent Email';
task.WhoId = '{!Lead.Id}';
task.Type = 'No Connect';
task.ActivityDate = new Date();
task.Status = 'Completed';

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

if(resultTask[0].success=='true' && resultLead[0].success=='true'){
alert('The Lead has been Updated Successfully and the Task was Created.');
location.reload();
}
}
catch(e){
alert('An Error has Occured. Error: ' + e);
}
Coco_SdyneyCoco_Sdyney
lead.status = 'Contact Not Made'
lead.Lead_Status_Details__c = 'Called LM & Emailed'

please add ';' at each end of line
lead.status = 'Contact Not Made';
lead.Lead_Status_Details__c = 'Called LM & Emailed';

Coco_SdyneyCoco_Sdyney
and also
if(resultTask[0].success=='true' && resultLead[0].success=='true'){
resultLead is not defined, use result instead
if(resultTask[0].success=='true' && result[0].success=='true'){
kelsey.joyce1.394641015380688E12kelsey.joyce1.394641015380688E12
Hi - the ' are alredy in there and I changed the resultTask part - still error.