2

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.

1

You can always try querying via connection.js to get the most up-to-date value from the database.

var idList = '(';

for (var i = 0; i < records.length; i++) {
    idList += "'" + records[i].Id + "'";

    if ((i + 1) !== records.length) {
        idList += ',';
    }
}

idList += ')';

result = sforce.connection.query('SELECT Send_Email__c from Package__c WHERE Id IN ' + idList);
queryRecords = result.getArray('records');

for (var i = 0; i < queryRecords.length; i++) {
    var record = queryRecords[i];

    Console.log(record.Name + ' -- ' + record.Id);
}

Adapted from a code snippet from the AJAX Toolkit Guide.

You can use the IN filter criteria with a javascript query. Heres a demo page you can try out yourself:

<apex:page>
    <script src="/soap/ajax/32.0/connection.js" />
    <script>
        sforce.connection.sessionId = '{!$Api.Session_ID}';
        var someIds = ['00161000004jaoc', '00161000004jaol', '00161000004jaof']; // ids from Developer Edition 
        var idList = '(';

        for (var i = 0; i < someIds.length; i++) {
            idList += "'" + someIds[i] + "'";

            if ((i + 1) !== someIds.length) {
                idList += ',';
            }
        }

        idList += ')';

        result = sforce.connection.query('SELECT Name, Id from Account WHERE Id IN ' + idList);
        queryRecords = result.getArray('records');

        for (var i = 0; i < queryRecords.length; i++) {
            var record = queryRecords[i];

            console.log(record.Name + ' -- ' + record.Id);
        }
    </script>
</apex:page>

Id list from the javascript

|improve this answer|||||
  • I don't think IN is supported for queries from Javascript. – SeanGorman Aug 5 '16 at 18:35
  • 1
    Not without the missing comma, thats for sure. I've updated my code to include the missing comma, as well as added a working snippet. – battery.cord Aug 5 '16 at 18:52
  • Ok - awesome - thank you... Now I have the data queried and am able to run my condition but I do not get expected results. – SeanGorman Aug 5 '16 at 20:04
  • 1
    Is everything being updating properly inside the javascript? Is it just not saving the results to the database? – battery.cord Aug 5 '16 at 20:30
  • 1
    I found a fix... if not the issue. I changed to this: if(queryRecords[a].Send_Email__c == 'false'){ with the quotes around the false – SeanGorman Aug 5 '16 at 20:48

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.