1

I am trying to auto-insert an array of Tasks based on the Accounts selected in the Account 'List View' tab.

The tasks are inserting as expected, but instead of writing the Account Name in the subject line, actual result is

Followup with undefined - Assigned [Today's Date]

I tried just using {!Account.Name} and no value was passed through there at all.

Relevant code block:

for (var i = 0; i < accountIds.length; i++) { 
        var taskToCreate = new sforce.SObject("Task");
        taskToCreate.WhatId = accountIds[i];     
        taskToCreate.OwnerId = "{!$User.Id}";
        taskToCreate.Subject = "Followup with " + accountIds[i].Name + " - Assigned {!TODAY()}";
        taskToCreate.ActivityDate = dueDate;            

        taskToCreate.Priority = "Normal";
        taskToCreate.Status = "Not Started";

        assignedTasks.push(taskToCreate);
}

How can I pass in the looped through Account's name?

2

The GetRecordIds formula literally does what it says: it gets the list of Id values the user selected. If you want the account names, you have to use sforce.connection.retrieve or sforce.connection.query to get those values. You'll want to read the Ajax Toolkit developer's guide for more details.

Here's an example using your code as a base:

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

var accountIds = {!GETRECORDIDS($ObjectType.Account)};

var accounts = sforce.connection.retrieve("Id,Name","Account",accountIds);
var tasks = [];
var dueDate = /* calculate due date here */ new Date();
accounts.forEach(function(account) {
  var task = new sforce.SObject("Task");
  task.WhatId = account.Id;
  task.OwnerId = "{!$User.Id}";
  task.Subject = "Follow-Up With "+account.Name+" - Assigned {!TODAY()}";
  task.ActivityDate = dueDate;
  task.Priority = "Normal";
  task.Status = "Not Started";
  tasks.push(task);
});
var results = sforce.connection.create(tasks);

Make sure you do basic error checking, and you should be okay.

  • Thank you! I will start digging, looking forward to your edits – GPP Dec 11 '16 at 5:34
  • 1
    @GPP As promised, a code sample for you to look at. This more or less works verbatim in my developer org (tested it before I added it here), but of course you'll want to add error checking. – sfdcfox Dec 11 '16 at 5:49
  • 1
    @GPP accountIds is used in sforce.connecton.retrieve, so you still use GETRECORDIDS to determine which records to create tasks for. – sfdcfox Dec 11 '16 at 5:58
  • 1
    @GPP By not defining a variable, you're polluting the global namespace. This is effectively a memory leak. While nothing bad would likely happen, you take a risk every time you dump something into the global namespace, so you should get in the habit of not doing it. – sfdcfox Dec 11 '16 at 6:44
  • 1
    @GPP The point of using var is to make sure your data doesn't go out to the global scope. Just avoid creating global variables arbitrarily, and you'll be okay. RequireScript only imports some source code and generally produces very little pollution (just sforce.connection is created). It's really just easier to say: "when in doubt, declare a local variable.". There's some exceptions to the rule, but you'll find them necessary when you Usually, you ant privacy party of friends – sfdcfox Dec 11 '16 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.