+ Start a Discussion
Miss AmyMiss Amy 

Pull in Contact fields using S-Control

I have a custom object tracking professional services engagements based on accounts. I have added a field to select the contact for the engagement from the contacts associated with the account. I would like to add an S-Control to pull in the phone number and email address but the following code is not bringing back anything.
 
<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
dojo.addOnLoad(init);

function init() {
var callback = {
onSuccess : displayResult,
onFailure : displayError
};
sforce.connection.query("SELECT Name, Phone, Email FROM Contact where ID = '{!Contact.Id}', callback);
}

function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
html.push("Name = " + record.Name + "<br>");
html.push("Phone = " + record.Phone + "<br>");
html.push("Email = " + record.Email + "<br>");
html.push("<hr>");
html.push("<br>");
}

document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) {
document.getElementById("output-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>


</head>
<body>

<div id="output-div"></div>

</body>
</html><html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
dojo.addOnLoad(init);

function init() {
var callback = {
onSuccess : displayResult,
onFailure : displayError
};
sforce.connection.query("SELECT Name, Phone, Email FROM Contact where ID = '{!Contact.Id}', callback);
}

function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
html.push("Name = " + record.Name + "<br>");
html.push("Phone = " + record.Phone + "<br>");
html.push("Email = " + record.Email + "<br>");
html.push("<hr>");
html.push("<br>");
}

document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) {
document.getElementById("output-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>


</head>
<body>

<div id="output-div"></div>

</body>
</html>
 
Do I need to display the contact ID on the page? If so how would i associate it with the contact?
 
Any help or suggestions are greatly appreciated.
 
Thanks!
Amy
CapricornCapricorn
Hi,

The only error i see in the code is the unterminated string in the query. You are missing a double quote after the merge field '{!Contact.Id}'

Putting that in, i've tested and it works fine.

Regards

SL TanSL Tan
Dear Capricorn I refer to your reply to Ms Amy on her coding for pulling in Contact fields using S-Control. I am new to Salesforce and I try to use the same coding to pull fields from my Customer Object to my Customer Contact object. The idea is when a user is in Customer Contact and they select by way of Look Up Relationship the name of a Customer, certain fields from the Customer Object will populate the fields in Customer Contact eg Street 1, City 1, Zip/Postal Code 1, State/Province 1, Country 1 (which is a picklist), Phone (1) and Phone (2). However my coding does not seem to work. Do you think this has to do with my coding Hope you can help me Thanks a million in advance I append my coding below SLTan <script src="/soap/ajax/9.0/connection.js"></script> <script src="/js/dojo/0.4.1/dojo.js"></script> <script> dojo.addOnLoad(init); function init() { var callback = { onSuccess : displayResult, onFailure : displayError }; sforce.connection.query("SELECT Customer Name, Street 1, City 1, State/Province 1, Zip/Postal Code 1, Country 1, Phone (1), Phone (2) FROM Customer where ID = {!Customer__c.Name}{!Customer__c.Street_1__c}{!Customer__c.City_1__c}{!Customer__c.State_Province_1__c}{!Customer__c.Zip_Postal_Code_1__c}{!Customer__c.Country_1__c}', callback"); } function displayResult(result) { var it = new sforce.QueryResultIterator(result); var html = []; while(it.hasNext()) { var record = it.next(); html.push("Customer Name = " + record.Customer Name + "
"); html.push("Street = " + record.Street 1 + "
"); html.push("City = " + record.City 1 + "
"); html.push("State/Province = " + record.State/Province 1 + "
"); html.push("Zip/Postal Code = " + record.Zip/Postal Code 1 + "
"); html.push("Country = " + record.Country 1 + "
"); html.push("Phone (1) = " + record.Phone (1) + "
"); html.push("Phone (2) = " + record.Phone (2) + "
"); html.push("
"); html.push("
"); } document.getElementById("output-div").innerHTML = html.join(""); } function displayError(error) { document.getElementById("output-div").innerHTML = "oops something went wrong ... " + error; } </script>
Miss AmyMiss Amy
I added the double quote and it still doesn't work. It's blank. If I put a contact ID in the where clause it pulls back the right data. I'm unable to pull the contact id using the {!Contact.Id}  reference.  If I remove the single quotes around {!Contact.Id} I get a null value error message.
BritishBoyinDCBritishBoyinDC
One thing to try is to replace the '{!Contact.Id}' merge field in the query with a declared variable, so you end up with something like this:

var contactId = "{!Contact.Id}";
var result = sforce.connection.query("Select  Name, Phone, Email from Contact where ContactId = '" + contactId + "' limit 10");