2

I am trying to create a list button to update a status field on a list of records, but I have problems with the syntax. Please help..

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

var url = parent.location.href; 
var records = {!GETRECORDIDS($ObjectType.Tasks__c )}; 

var updateRecords = []; 

if (records[0] == null) { 
alert("Please select at least one record to update."); 
} else { 

for (var a=0; a<records.length; a++) { 

    var update_Tasks = new sforce.SObject("{!Tasks__c}"); 
    update_{!Tasks__c.Id} = records[a]; 

    update_{!Tasks__c.Status__c} = "Completed"; 
    updateRecords.push(update_Tasks__c); 

    }
    result = sforce.connection.update(updateRecords); 

    parent.location.href = url; 
}

Error is in the {!GETRECORDIDS ($ObjectType.... I am using a custom object and not sure how to write it.

7
  • Try using the label of the object instead.
    – PepeFloyd
    Jul 28, 2015 at 14:37
  • @PepeFloyd Thanks for replying. I tried replacing the Tasks__c with just the label which is "Customer Tasks" but it says it doesn't exist.
    – Lizzy Li
    Jul 28, 2015 at 14:42
  • What is the error that you're getting at the line containing GETRECORDIDS()?
    – Mark Pond
    Jul 28, 2015 at 14:48
  • @MarkPond Hi... it says "Field Customer_Tasks" does not exist. I changed it from API Name I had before to Label. Thnks!
    – Lizzy Li
    Jul 28, 2015 at 14:52
  • What error are you getting with the code the way you posted it? Also, this line you do not need them markup tag: new sforce.SObject("{!Tasks__c}"); and I am not sure what you are doing here update_{!Tasks__c.Id} = records[a]; update_{!Tasks__c.Status__c} = "Completed";
    – CyberJus
    Jul 28, 2015 at 14:59

2 Answers 2

2

Your object names look fine but you've got merge syntax {! } in your javascript that shouldn't be there. I've modified your javascript variables in the code below to make it a bit clearer what is happening in the loop. You may still have field API name issues to clear up.

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

var url = parent.location.href; 
var records = {!GETRECORDIDS($ObjectType.Tasks__c)}; 

var recordsToUpdate = []; 

if (records[0] == null) { 
    alert("Please select at least one record to update."); 
} else { 

    for (var a=0; a<records.length; a++) { 

        var taskToUpdate = new sforce.SObject("{!Tasks__c}"); 
        taskToUpdate.Id = records[a]; 

        taskToUpdate.Status__c = "Completed"; 
        recordsToUpdate.push(taskToUpdate); 

        }

    var result = sforce.connection.update(recordsToUpdate); 

    // you probably want to check what is in 'result' and look for success before 
    // redirecting the browser to the new URL

    parent.location.href = url; 
}
1
  • Thanks! I am clearly missing a piece of the puzzle, so I'll try to figure it out based on the comments. Thank you!
    – Lizzy Li
    Jul 28, 2015 at 15:33
1

Building on Mark's answer, the final piece of the puzzle is that your sObject creation syntax is not quite right.

You don't need {!} at this point.

Change var taskToUpdate = new sforce.SObject("{!Tasks__c}");

To var taskToUpdate = new sforce.SObject("Tasks__c");

The curly bracket syntax is a static way to tell salesforce that you want the value in the curly bracket to be replaced with some actual value. In this case, you don't want that - you want to create an object of type "Tasks__c"

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .