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
Lisa SeldenLisa Selden 

Create a button to add task to ALL contacts in an account

Hello, I am trying to figure out how to write a JAVA script to create a button. I was hoping to add the same task to all contacts in the account. Any help would be appreciated. Thanks!
 
Best Answer chosen by Lisa Selden
Neetu_BansalNeetu_Bansal
Hi Lisa,

You need to create a new custom button, which Exceute javascript and use the below code:
{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/34.0/apex.js")} 

var url = '/{!Account.Id}';

var myquery = "Select Id, Name from Contact Where AccountId = '{!Account.Id}'"; 
result = sforce.connection.query(myquery); 
records = result.getArray("records");

var taskArr = [];
for(var i=0; i < records.length;i++)
{
var task = new sforce.SObject("Task");;
task.WhoId = records[i].Id;
task.WhatId = '{!Account.Id}';
taskArr.push(task);
}

var results = sforce.connection.create(taskArr); 
if (results[0].getBoolean("success")) { 
window.parent.open(url, "_self");
} else { 
alert("Failed to create Task " + results[0]); 
}

Let me know if any other help is needed.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Lisa,

You need to create a new custom button, which Exceute javascript and use the below code:
{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/34.0/apex.js")} 

var url = '/{!Account.Id}';

var myquery = "Select Id, Name from Contact Where AccountId = '{!Account.Id}'"; 
result = sforce.connection.query(myquery); 
records = result.getArray("records");

var taskArr = [];
for(var i=0; i < records.length;i++)
{
var task = new sforce.SObject("Task");;
task.WhoId = records[i].Id;
task.WhatId = '{!Account.Id}';
taskArr.push(task);
}

var results = sforce.connection.create(taskArr); 
if (results[0].getBoolean("success")) { 
window.parent.open(url, "_self");
} else { 
alert("Failed to create Task " + results[0]); 
}

Let me know if any other help is needed.

Thanks,
Neetu
This was selected as the best answer
Neetu_BansalNeetu_Bansal
Any luck Lisa, or you still having the issue.

Thanks,
​Neetu
Lisa SeldenLisa Selden
Hi Neetu, thank you for the script. I am sorry I didn't reply earlier I just saw this. I went ahead and added the button and I was able to see it under the account. However, I get an error message when I try to use the button. I took a screen shot and attached it. Could this be a setting that I need to contact customer support about or is there some other way to fix it? Thank you!

User-added image
Neetu_BansalNeetu_Bansal
Hi Lisa,

API is an 'add on' feature for Professional Edition, you can add API access to Professional Edition for an additional fee. You will not be able to enable that during your trial.

If you want to experiment, you can sign up for a free Developer Edition at developerforce.com (https://developerzzsf.kosoku2015.jp).  This will be an Enterprise Edition org (with small data allowance).

Thanks,
Neetu
Lisa SeldenLisa Selden
Hi again Neetu, 

Do you know if it is possible to write this script in a way so that it would not require API. I am able to create custom buttons using JAVA script in my edition, however the other add on was out of our price range. Thank you!

Lisa 
Neetu_BansalNeetu_Bansal
Hi Lisa,

Is it possible for you to share your Salesforce Org credentials, so I can have a look and suggest if any other solution can be implemented. My personal details are mentioned in my profile, you can contact me anytime.

Thanks,
Neetu