function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SMasterSMaster 

Using Javascript code .. how can i display all the sucess records???

Hi All,

 

I have a Javascript code to send email to the selected records... Now i simply want to to display all those records with their Employee code for which an Email has been sent sucessfully...Pls Help... below is my JS code:

 

 

{!requireScript("/soap/ajax/20.0/connection.js")}
{!requireScript("/soap/ajax/20.0/apex.js")}

 
var recordsSelected = {!GETRECORDIDS( $ObjectType.RMG_Employee_Master__c)};

 

if (recordsSelected.length < 1)
{
  alert('Please select atleast one recrod');
}
else if(confirm('Are you sure to send email to the selected records?'))
{
        for (var j=0; j< recordsSelected.length; j++)
{

 


               var result = sforce.connection.query('select Email_Address__c, EmpCode__c,Name,Send_Email__c  from RMG_Employee_Master__c    where id = '+"'" +recordsSelected[j] + "'");

              var employee = new sforce.SObject("RMG_Employee_Master__c");
              employee.Id = recordsSelected[j];
              employee.Send_Email__c = 'True';
              result = sforce.connection.update([employee]);

              employee.Send_Email__c = 'False';
              result = sforce.connection.update([employee]);
 

// i have tried using this code to achieve the same.. but its giving me undefined........
var myarray=new Array();
myarray[j]=employee.EmpCode__c;
alert(myarray[j]);
                

              
}

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

In your code you are creating a new array every time in the loop. You just create array outside the loop and add the elements in the array inside the loop :

 

            var myarray = new Array();

 

            for (var j=0; j< recordsSelected.length; j++)

            {

myarray[j]=employee.EmpCode__c;

            }

All Answers

jhurstjhurst

This should do what you want, though if you send an email to 20 people in the List view, you would get 20 alert messgaes....

 

 

{!requireScript("/soap/ajax/20.0/connection.js")}
{!requireScript("/soap/ajax/20.0/apex.js")}
 
var recordsSelected = {!GETRECORDIDS($ObjectType.RMG_Employee_Master__c)};
 
if (recordsSelected.length < 1) {
  alert('Please select at least one record');
}
else if(confirm('Are you sure to send email to the selected records?')) {
  var recordsSelectedString = "";
  for (var j=0; j< recordsSelected.length; j++) {
    if(j == 0) {
      recordsSelectedString += "'" + recordsSelected[j] + "'";
    } else {
      recordsSelectedString += ", '" + recordsSelected[j] + "'";
    }
  }
  result = sforce.connection.query("select Id, Email_Address__c, EmpCode__c, Name, Send_Email__c from RMG_Employee_Master__c where id in (" + recordsSelectedString + ")");
  records = result.getArray("records");
  for (var i=0; i< records.length; i++) {
    var employee = records[i];
    employee.Send_Email__c = 'True';
    result = sforce.connection.update([employee]);
    employee.Send_Email__c = 'False';
    result = sforce.connection.update([employee]);
    alert("Employee ID: " + employee.Id + " -- Employee Code: " + employee.EmpCode__c);
  }   
}

 

Hope this helps.

Jay

 

Pradeep_NavatarPradeep_Navatar

In your code you are creating a new array every time in the loop. You just create array outside the loop and add the elements in the array inside the loop :

 

            var myarray = new Array();

 

            for (var j=0; j< recordsSelected.length; j++)

            {

myarray[j]=employee.EmpCode__c;

            }

This was selected as the best answer