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
Cray KaoCray Kao 

How to display field value within a javascript button script (for loop)

Here is a list button to display selected records.

 

If I want to display the contact name instead of "id of the contact" and also prevent from displaying those whom is "DoNotCall".

 

How to modify the button script?

 

And I am new to Salesforce Developer, and which board will more suitable to discuss javascript button questions. Thanks.

 

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

var records = {!GETRECORDIDS($ObjectType.Contact)};
if (records[0] == null)
{
alert("Please select at least one record")
}
else{
for (var n in records) {
alert ( records[n] ) }
}

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try below code snippet as reference:

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

 

var records = {!GETRECORDIDS($ObjectType.Contact)};

 

 

 

if(records[0]==null)

{

                alert('Please select altleat one contact');

}

else

{

                if(records.length>1)

                {

                                var querydata='';

                                querydata='\''+records [0]+'\'';

                                for(var i=1;i<records .length;i++)

                                {

                                querydata=querydata+',\''+records [i]+'\'';

                                }

                }

                else

                {

                                querydata='\''+records+'\'';

                }

                               

                var query1='select name from contact where id in('+querydata+')';

                var result =sforce.connection.query(query1);

                //for(var j=0;j<result.length;i++)

{

for(var i=0;i<result.size;i++)

{

var res=result['records'][i];

alert(res['Name']);

 

}

}

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

All Answers

Cray KaoCray Kao

If I do a little modify...

 

it will show "0", "undefined", "id of record 0", then "1", "undefined", "id of record 1"

 

How to show object__c.name in such an array?

 

Thanks

 

Here's the code:

 

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

var records = {!GETRECORDIDS($ObjectType.object__c)};
if (records[0] == null)
{
alert("Please select at least one record")
}
else{
for (var n in records) {
alert ( n );
alert ( n.name );
alert ( records[n] ) }
}

Navatar_DbSupNavatar_DbSup

Hi,

Try below code snippet as reference:

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

 

var records = {!GETRECORDIDS($ObjectType.Contact)};

 

 

 

if(records[0]==null)

{

                alert('Please select altleat one contact');

}

else

{

                if(records.length>1)

                {

                                var querydata='';

                                querydata='\''+records [0]+'\'';

                                for(var i=1;i<records .length;i++)

                                {

                                querydata=querydata+',\''+records [i]+'\'';

                                }

                }

                else

                {

                                querydata='\''+records+'\'';

                }

                               

                var query1='select name from contact where id in('+querydata+')';

                var result =sforce.connection.query(query1);

                //for(var j=0;j<result.length;i++)

{

for(var i=0;i<result.size;i++)

{

var res=result['records'][i];

alert(res['Name']);

 

}

}

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

This was selected as the best answer
Cray KaoCray Kao

Cool & Thanks

Travis DePuy 3Travis DePuy 3
I know this is an old thread, but I found it in my google searching and it was very helpful. One improvement I made was using javascript's native join array method to join the list objects into a string based on the given string. I also removed the extra for loop that was probably there for debugging. Final code:

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Contact)};
var updateRecords = [];
 
if(records[0]==null)
{
  alert('Please select at least one contact');
}
else
{

  querydata = "'" + records.join( "', '" ) + "'";
            
  var query1 = 'select name from contact where id in('+querydata+')';
  var result =sforce.connection.query( query1 );
  
  var res = "";
  for(var i=0;i<result.size;i++) {
    res += result['records'][i]['Name'] + ", ";
  
  }

  alert( res );
}