I have a list of 'Packages' that will be delivered to the client. It is related to Opportunity. It contains Name of Carrier, tracking number, email sent checkbox, date/time email sent and checkbox for send email.
I am thinking that I will create a VF page for the team to add multiple packages on a single page.
Right now I have a button (called Toggle Email Selection) to set the Send Email checkbox to true. There will be a second button to send the email. I don't want to automate the sending of the email as the user may not have all of the packages when they enter the first.
I want the setting and unsetting of the checkbox using the Toggle Email Selection button to be a toggle but cannot get it to work.
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
var records = {!GETRECORDIDS($ObjectType.Package__c)};
var updateRecords = [];
if (records[0] == null) {
alert("Please select at least one package for which an update should be sent to the client.");
} else {
for (var a=0; a<records.length; a++) {
var update_Package = new sforce.SObject("Package__c");
update_Package.Id = records[a];
alert(update_Package);
if(update_Package.Send_Email__c == false){
update_Package.Send_Email__c = true;
} else {
update_Package.Send_Email__c = false;
}
updateRecords.push(update_Package);
}
result = sforce.connection.update(updateRecords);
location.reload(true);
}
This doesn't work because the value is absent from the data passed by the button. I can't think of an easy way to make it work. I don't really want to have 2 buttons: "Set CB to True" and "Set CB to False"
Can anyone help me?
Edited code:
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
var records = {!GETRECORDIDS($ObjectType.Package__c)};
var updateRecords = [];
var queryRecords= [];
var idList = '(';
if (records[0] == null){
alert("Please select at least one package for which an update should be sent to the client.");
} else {
for (var i = 0; i < records.length; i++) {
idList += "'" + records[i] + "'";
if ((i + 1) !== records.length) {
idList += ',';
}
}
idList += ')';
var packages = sforce.connection.query("SELECT Id, Send_Email__c, shipping_Carrier__c FROM Package__c WHERE ID IN "+idList);
queryRecords = packages.getArray("records");
for (var a=0; a<queryRecords.length; a++){
// var update_Package = new sforce.SObject("Package__c");
update_Package = queryRecords[a];
alert(update_Package.Send_Email__c );
alert(update_Package.shipping_Carrier__c );
if(queryRecords[a].Send_Email__c == false){
update_Package.Send_Email__c = true;
} else {
update_Package.Send_Email__c = false;
}
updateRecords.push(update_Package);
}
alert(updateRecords);
result = sforce.connection.update(updateRecords);
location.reload(true);
}
This does not update the field. (I have 3 rows all set to False and none update.