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
IZavalunIZavalun 

Help to pass the parramentr to the function

Hello - Please help to fix my syntax problem here to pass the parametr, I need to get the contact in function cm(contact)...
Please see in red:
==============================================
 
<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="Link: " type="text/javascript"></script>
<script language="javascript">
var fn = "cm(this)";
function do_code()
{
//////////////////////////////////////
var cust_name =  '{!Account.Name}';
var qrdr5 = sforce.connection.query("Select c.Id from Account c  where c.Name= '" + cust_name + "'");
var records = qrdr5.getArray("records");
for (var x = 0; x < records.length; x++)
{
tt5 = records[x];
var ttt = tt5.get("Id");
}
/////////////////////////////////////
var qrdr2 = sforce.connection.query("Select Name from Contact c  where AccountId= '" + ttt + "'order by createdDate");
var records = qrdr2.getArray("records");
document.write("<b><u>Please Select Customer Contact:</u></b><BR><BR>");
document.write("<HR>");
for (var y = 0; y < records.length; y++)
{
tt2 = records[y];
var t=tt2.get("Name");
document.write("<BR>");
output = "<input id=btn1 type=button name=btn1 onclick=cm(obj); value='" + t + "'>";
document.write(output);
document.write("<BR>");
}
document.write("<BR>");
document.write("<HR>");
}
////////////////////////////////////
function cm(contact) {
  alert(contact);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Customer Contacts...</title>
</head>
<body onLoad="do_code();">
</body>
</html>
Greg HGreg H

Since you were using the {!Account.Name} parameter in your original code, I figure you are accessing this sControl from the Account object.  If this is not the case then this code may not work for you.

Code:

<html>
<head>
 <title>Customer Contacts...</title>
<script src="/soap/ajax/10.0/connection.js"></script>
<script language="javascript"> 
<!--
//pulls back all contacts on an account ordered by created date
function do_code() {
 var docHTML = "<b><u>Please Select Customer Contact:</u></b><BR><BR>"; //start a variable for holding all HTML to eventually be placed on the page
 docHTML += "<HR>"; //append more HTML to variable
 var sql = sforce.connection.query("SELECT Name FROM Contact WHERE AccountId='{!Account.Id}'ORDER BY createdDate"); //SOQL query statement
 if (sql.getBoolean("done") && sql.size!=0) { //if done and results found
  var records = sql.getArray("records"); //get all results from sql array
  for (var a=0; a<records.length; a++) { //for all records
   var contactName = records[a].Name; //assign the current record name to contactName variable
   docHTML += "<BR>"; //append more HTML
   docHTML += "<input id=\"btn"+(a+1)+"\" type=\"button\" name=\"btn"+(a+1)+"\" value=\""+contactName+"\">"; //append more HTML
  }
 docHTML += "<HR>"; //append more HTML
 document.getElementById("results").innerHTML = docHTML; //write HTML variable to page
 } else {
  document.getElementById("results").innerHTML = "no contact records<br><br>"+sql; //write no records to page
 }
}
//-->
</script>
</head>
<body onLoad="do_code();">
<div id="results">Loading...</div>
</body>
</html>

 

Sorry to rewrite so much of your original work but I couldn't follow it.  So copy and paste this code in a new sControl and let me know if this doesn't assist you with the issue you've been encountering.
-greg

IZavalunIZavalun

Hi Greg,

I did override my code with your code and it works same way it did for me but….

 

  1. you don’t have the function cm() where I need to pass the btn value==> this is where I had a problem
  2. when the value is passed to the function I need to update the “IS_Mach” custom object:

                        update IS_Mach

                  set CustomerContact=btnValue (passed to the function)

                  WHERE AccountId='{!Account.Id}

 

I will really appreciate if you will help me with this syntax

 

Greg HGreg H

Try this:

Code:

<html>
<head>
 <title>Customer Contacts...</title>
<script src="/soap/ajax/10.0/connection.js"></script>
<script language="javascript"> 
<!--
//pulls back all contacts on an account ordered by created date
function do_code() {
 var docHTML = "<b><u>Please Select Customer Contact:</u></b><BR><BR>"; //start a variable for holding all HTML to eventually be placed on the page
 docHTML += "<HR>"; //append more HTML to variable
 var sql = sforce.connection.query("SELECT Name FROM Contact WHERE AccountId='{!Account.Id}'ORDER BY createdDate"); //SOQL query statement
 if (sql.getBoolean("done") && sql.size!=0) { //if done and results found
  var records = sql.getArray("records"); //get all results from sql array
  for (var a=0; a<records.length; a++) { //for all records
   var contactName = records[a].Name; //assign the current record name to contactName variable
   docHTML += "<BR>"; //append more HTML
   docHTML += "<input id=\"btn"+(a+1)+"\" type=\"button\" name=\"btn"+(a+1)+"\" value=\""+contactName+"\" onclick=\"JavaScript&colon; cm('"+contactName+"')\">"; //append more HTML
  }
 docHTML += "<HR>"; //append more HTML
 document.getElementById("results").innerHTML = docHTML; //write HTML variable to page
 } else {
  document.getElementById("results").innerHTML = "no contact records<br><br>"+sql; //write no records to page
 }
}

//handles click from above buttons
function cm(ContactName) {
 alert("You selected the contact: "+ContactName);
}
//-->
</script>
</head>
<body onLoad="do_code();">
<div id="results">Loading...</div>
</body>
</html>

This will allow you to send the contact name to the function cm() when each button is clicked.  You can do whatever you want in that function as all I did was alert that the name was passed.
-greg

IZavalunIZavalun

Greg,

I am having same problem now the message box doesn't appear with the ContactName.

Any idea?

Greg HGreg H
My bad.  It looks like some of my code got encoded when I saved it.  The HTML encoded colon characters is causing the problem.
 
You'll need to find and replace the portion of the code reading "&colon;" with ":".
-greg
IZavalunIZavalun

GREG - it works. Thanks.

The last thing I want to acomplish here is to use the update (red line). Would you please give me an idea how in salesforce I can do it.

I really appriciate your help.

========================================================

//handles click from above buttons
function cm(contactName) {
 alert(contactName);
alert('{!IS_Mach__c.Name}');

update IS_Mach__c set Customer_contact__c= 'contactName' where Name='{!IS_Mach__c.Name}'

}

Greg HGreg H
Code:
//handles click from above buttons
function cm(ContactName) {
 //alert("You selected the contact: "+ContactName);
 //alert('{!IS_Mach__c.Name}');
 if (ContactName) { //if value passed for contact name
  var _IsMatch = new sforce.SObject("IS_Mach__c");
  _IsMatch.Id = "{!IS_Mach__c.Id}"; //set Id
  _IsMatch.Customer_contact__c = ContactName; //set contact name value
  var updateIsMatch = sforce.connection.update([_IsMatch]); //actually perform update
  if (updateIsMatch[0].getBoolean("success")) { //if successful update of existing record
   alert("Successfully updated contact name!"); //indicate that the update was successful
  } else {
   alert("Error:\n"+updateIsMatch[0]); //display the error
  }
 }
}

This should work but depending on how this sControl is being accessed, it may not.  My primary concern is that the {!IS_Mach__c.Id} value may not be available.  If it is not then you could query for that value based on the {!IS_Mach__c.Name} value stored in your custom object prior to actually making the update.
 
Basically, you must have the Id of the record you are updating in order for the update to happen.
 
Happy to help,
-greg
IZavalunIZavalun
:smileyvery-happy: GREG - IT WORKS LIKE A CHARM!!! THANK YOU