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
Mike RethageMike Rethage 

Onclick Javascript Custom Button "Follow-up Task" button for Opportunity page

I created a custom button for the "leads" page so that our sales team can have a one-click follow-up created for various lengths of time.  The below button is for a 1 week follow-up and works just fine on the leads page:

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

var myTask = new sforce.SObject("Task"); 
myTask.WhoId = "{!Lead.Id}"; 
myTask.Subject = "{!Lead.LastName} 1 Month Follow-up"; 
myTask.status = "Not Started"
myTask.ActivityDate = "{!SUBSTITUTE(TEXT(TODAY()+30), "/", "-")}"
sforce.connection.create([myTask]); 
window.location.reload();

I would like to do something similar on our Opportunity and Case pages. I would expect that I could just swap out Lead.ID for Opportunity.ID and Lead.Lastname for the Opportunity.Subject and it should work.  Unfortunatly, I'm just getting a page refresh and no task.  Please help!
Best Answer chosen by Mike Rethage
Vivek DeshmaneVivek Deshmane
Hi
For Opportunitty you ca try below code.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var myTask = new sforce.SObject("Task"); 
myTask.WhatId= "{!Opportunity.Id}"; 
myTask.Subject = "{!Opportunity.name} 1 Month Follow-up"; 
myTask.status = "Not Started"
myTask.ActivityDate = "{!SUBSTITUTE(TEXT(TODAY()+30), "/", "-")}"
sforce.connection.create([myTask]); 
window.location.reload();

For Case you ca try below code.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var myTask = new sforce.SObject("Task"); 
myTask.WhatId= "{!Case.Id}"; 
myTask.Subject = "{!Case.caseNumber} 1 Month Follow-up"; 
myTask.status = "Not Started"
myTask.ActivityDate = "{!SUBSTITUTE(TEXT(TODAY()+30), "/", "-")}"
sforce.connection.create([myTask]); 
window.location.reload();


Please let me know if this helps you.
Best Regards,
-Vivek

All Answers

kaustav goswamikaustav goswami
When you are trying to link a task to an opportunity you need to chage the line - myTask.WhoId = "{!Lead.Id}"; 

it should be - myTask.WhatId = "{Opportunity.Id}";

Thanks,
Kaustav
Vivek DeshmaneVivek Deshmane
Hi
For Opportunitty you ca try below code.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var myTask = new sforce.SObject("Task"); 
myTask.WhatId= "{!Opportunity.Id}"; 
myTask.Subject = "{!Opportunity.name} 1 Month Follow-up"; 
myTask.status = "Not Started"
myTask.ActivityDate = "{!SUBSTITUTE(TEXT(TODAY()+30), "/", "-")}"
sforce.connection.create([myTask]); 
window.location.reload();

For Case you ca try below code.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var myTask = new sforce.SObject("Task"); 
myTask.WhatId= "{!Case.Id}"; 
myTask.Subject = "{!Case.caseNumber} 1 Month Follow-up"; 
myTask.status = "Not Started"
myTask.ActivityDate = "{!SUBSTITUTE(TEXT(TODAY()+30), "/", "-")}"
sforce.connection.create([myTask]); 
window.location.reload();


Please let me know if this helps you.
Best Regards,
-Vivek
This was selected as the best answer