0

I have a javascript function I'm using in a button on a list view. The list view displays cases and it will return the highlighted cases in the GETRECORIDS. My goal is to make a mass reply button that will allow a user to select an email template and it will send that template to each contact in the cases that were selected. So far the script will get the CaseIDs and my next goal is to run a query to retrieve the ContactIDs of those cases and put them into an array. My code just quit after the javascript query. Any help is appreciated.

function sendMail(){
    var caseIds = {!GETRECORDIDS( $ObjectType.Case )};
    var templateId = jQuery('[id=email-template]').val();
    if(caseIds.length>0 && templateId!=''){   
        var myNewQuery = "SELECT ContactId FROM Case WHERE Id in ('"+caseIds+"')";
        var queryResults = sforce.connection.query(myNewQuery); //Execution halts here
        var contactIds = queryResults.getArray("queryResults");

        var massMailRequest = new sforce.MassEmailMessage();
        massMailRequest.targetObjectIds = contactIds;
        massMailRequest.templateId = templateId;
        massMailRequest.replyTo = 'noreply@salesforce.com';

        sforce.connection.sendEmail([massMailRequest]);

        alert('Your emails have been submitted for processing.');
    }
}
0

Your query is malformed because of this line:

    var myNewQuery = "SELECT ContactId FROM Case WHERE Id in ('"+caseIds+"')";

The resulting query will end up looking like this:

SELECT ContactId FROM Case WHERE Id in ("500000000000001,500000000000002,500000000000003")

But instead it should look like this:

SELECT ContactId FROM Case WHERE Id in ("500000000000001","500000000000002","500000000000003")

Note the extra quotes that are missing.

You could fix that with Array.join:

    var myNewQuery = "SELECT ContactId FROM Case WHERE Id in ('"+caseIds.join('","')+"')";

If you're going to use a query, a GROUP BY clause would be appropriate here to remove potential duplicate values:

var myNewQuery = "SELECT ContactId ContactId FROM Case WHERE Id in ('"+caseIds.join('","')+"') GROUP BY ContactId";

Also note that this code would not have worked:

    var contactIds = queryResults.getArray("queryResults");

Since you actually need to get into the records and pull out the ContactId values. This would have been a separate process.

Now, you can generate a query like you've done, or you could do it the easy way: sforce.connection.retrieve. This has a simple syntax that allows us to retrieve thousands of values given record IDs.

It simplifies your code a bit:

var caseIds = {!GETRECORDIDS( $ObjectType.Case )};
var templateId = jQuery('[id=email-template]').val();
if(caseIds.length>0 && templateId) {
  var queryResults = sforce.connection.retrieve('ContactId','Case',caseIds);
  var contactIds = [];
  queryResults.forEach(function(record) {
    if(contactIds.indexOf(record.ContactId)<0) { 
      contactIds.push(record.ContactId);
    }
  });
  // rest of code here
  • Holy smoke, your solution is awesome. I definitely love simple code and being new to javascript and salesforce, this is a huge help. Thanks! – Mike T Nov 10 '17 at 12:30

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.