You need to sign in to do that
Don't have an account?

Querying 2 Objects in 1 S-Control
Could anyone please kindly advise what is wrong with my coding below? I am querying one type of data in each of 2 objects in one S-Control using the Select/From/Where Statements. I could get the output correctly for each type of data for each object but when it shows on the output it only shows one object's data in both output.Any pointer will be highly appreciated
Thanks in advance - SL
This is my code used
<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>
<script>
dojo.addOnLoad(init);
dojo.addOnLoad(init);
function init() {
var callback = { onSuccess : displayResult,
onFailure : displayError
};
sforce.connection.query("SELECT Id, Name FROM New_Type__c WHERE Id = '{!Customer_Order__c.New_TypeId__c}' " , callback );
sforce.connection.query("SELECT Id, Name FROM Final_Buyer__c WHERE Id = '{!Customer_Order__c.Final_BuyerId__c}' " , callback );
}
function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
var callback = { onSuccess : displayResult,
onFailure : displayError
};
sforce.connection.query("SELECT Id, Name FROM New_Type__c WHERE Id = '{!Customer_Order__c.New_TypeId__c}' " , callback );
sforce.connection.query("SELECT Id, Name FROM Final_Buyer__c WHERE Id = '{!Customer_Order__c.Final_BuyerId__c}' " , callback );
}
function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
html.push( "<b>New Type: </b><br> " + record.Name + " <br><br>");
html.push( "<b>Final Buyer: </b><br> " + record.Name + " <br>");
html.push("<hr>");
html.push("<br>");
html.push( "<b>Final Buyer: </b><br> " + record.Name + " <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>
document.getElementById("output-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>
</head>
<body>
<div id="output-div"></div>
</body>
</html>
</html>
You have to create two html div, say:
<div id="output1-div"></div>
<div id="output2-div"></div>
And the first callback must write to output1 and the secound callback
should write to output2.
HTHs,
Yes you will get only one object's records that is the second one only.
See in the first query sucess you call the sucess method.
Again on the second query the same result object is assigned to success method so the second is printed.
You have to store the query results in two different objects and then print the values in the required format.
Regards,
Suchitra
Thanks for your feedback that I should create 2 html div.
I have followed your guideline, but I still could not get the output correctly. I think this has something to do with the coding. I am appending the coding below and hope if my error can be pointed out. Thanks a lot in advance.
SL
Amended code;
<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 }
"output1-div" , "output2-div"
onFailure : displayError
};
sforce.connection.query("SELECT Id, Name FROM New_Type__c WHERE Id = '{!Customer_Order__c.New_TypeId__c}' " );
callback = onSuccess ("output1-div'" ) );
sforce.connection.query("SELECT Id, Name FROM Final_Buyer__c WHERE Id = '{!Customer_Order__c.Final_BuyerId__c}' " , callback = onSuccess ("output2-div" ) );
function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
html.push( "New Type:
" + record.Name + "
");
html.push( "Final Buyer:
" + record.Name + "
");
html.push("
");
html.push("
");
}
document.getElementById("output1-div").innerHTML = html.join("");
document.getElementById("output2-div").innerHTML = html.join("");
}
function displayError(error) {
document.getElementById("output1-div").innerHTML =
"oops something went wrong ... " + error;
document.getElementById("output2-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>
Code:
Thanks for your kind feedback.
I hope you dont mind advising me further as I still have some issues.
I modified my coding to yours but it could not run so I used the Ajax Toolkit to try and find the areas that may need changes
From each output, I have made some changes according to the "error" output indication from running the Ajax Tool.
The code now becomes slightly changed which I have copied below:
Where I got stuck now is at the function displayResult area where the Ajax error output indicated 2 errors from the code below:
(1) uncaught exception: onSuccess not defined in the callback
(2)this.queryResult.getArray is not a function
this.records = this.queryResult.getArray("records");
When I added onSuccess to the function displayResult I get the following error indicator
missing ( before formal parameters
function displayResult onSuccess (result, source){\n
As I try to follow the error indicators, the results gets more error messages with each step until it seems endless. Do you know where I have gone wrong from my explanation above. I am sorry to trouble you but would really appreciate your kind feedback again
Many thanks in advance
SL
Code
<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 callback1 = { onSuccess : displayResult ("output1=div"),
onFailure : displayError
};
source : document.getElementById ("output1=div" );
};
sforce.connection.query("SELECT Id, Name FROM Final_Buyer__c WHERE Id = '{!Customer_Order__c.Final_BuyerId__c}' " , callback1= ("output1-div") );
var callback2 = { onSuccess : displayResult ("output2=div"),
onFailure : displayError
};
source : document.getElementById ("output2=div" );
sforce.connection.query("SELECT Id, Name FROM Finish_Type__c WHERE Id = '{!Customer_Order__c.Finish_TypeId__c}' " , callback2 = ("output2-div"));
function displayResult(result, source){
var it = sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
html.push( "Final Buyer:
" + record.Name + " " + record.Name + "
");
html.push( "Finish Type: " + record.Name + "
");
}
source.innerHTML = html.join("");
}
function displayError(error) {
document.getElementById("output-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>
Please try this out.
<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>
<script language ="Javascript1.3">
dojo.addOnLoad(init);
function init()
{
try
{
var resultNew = sforce.connection.query("SELECT Id, Name FROM New_Type__c WHERE Id = '{!Customer_Order__c.New_TypeId__c}'");
var resultFinal = sforce.connection.query("SELECT Id, Name FROM Final_Buyer__c WHERE Id = '{!Customer_Order__c.Final_BuyerId__c}'");
var navLength=0;
var i;
var reqText = "";
var newLen = resultNew.size;
var finalLen = resultFinal.size;
//The Loop that prints the value should go till maximum of either of object
if (newLen < finalLen)
{
navLen = finalLen;
}
else{
navLen = newLen;
}
for (i=0;i < navLen;i++)
{
if (i <= newLen-1)
{
reqText = "<b>New Type:"+[i+1]+"</b><br> " + resultNew.records[i].Name + " <br><br>";
}
else
{
reqText = "<b>New Type:"+[i+1]+"</b><br> No Records Found <br><br>";
}
if (i <= finalLen-1)
{
reqText +="<b>Final Buyer:"+[i+1]+"</b><br> " + resultFinal.records[i].Name + " <br>";
}
else {
reqText +="<b>Final Buyer:"+[i+1]+"</b><br> No Record <br><br>";
}
reqText +="<hr>";
reqText += "<br>";
document.getElementById("output-div").innerHTML += reqText;
}
}
catch(e)
{
alert("There is problem" + e);
}
}
</script>
</head>
<body>
<div id="output-div"></div>
</body>
</html>
Tan also let me know your feedback once you have tried this.
Regards
Suchitra :smileyhappy:
Code:
Message Edited by SL Tan on 06-28-2007 03:27 PM
Thanks for your kind feedback and code. I run the code and this time the results are captured but it repeats for both the New Type and Finish Buyer as below:
New Type:
San
Final Buyer:
San
New Type:
Oztay
Final Buyer:
Oztay
What do you think should be amended for the WHERE statement to take effect and the output not duplicated?
I do hope you can help me again
Thanks in advance
SL
Hi Tan,
Please try to alert the query result and see. I tried to query Account and Opportunity and it worked very fine for me. Please check at each level. It should work fine.
Thanks & Regards
Suchitra :smileyhappy:
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>
<script language ="Javascript1.3">
dojo.addOnLoad(init);
function init()
{
try
{
var resultFinal = sforce.connection.query("SELECT Id, Name FROM Opportunity");
var navLength=0;
var i;
var reqText = "";
var newLen = resultNew.size;
var finalLen = resultFinal.size;
if (newLen < finalLen)
{
navLen = finalLen;
}
else{
navLen = newLen;
}
{
if (i <= newLen-1)
{
reqText = "<b>New Type:"+[i+1]+"</b><br> " + resultNew.records[i].Name + " <br><br>";
}
else
{
reqText = "<b>New Type:"+[i+1]+"</b><br> No Records Found <br><br>";
}
if (i <= finalLen-1)
{
reqText +="<b>Final Buyer:"+[i+1]+"</b><br> " + resultFinal.records[i].Name + " <br>";
}
else {
reqText +="<b>Final Buyer:"+[i+1]+"</b><br> No Record <br><br>";
}
reqText +="<hr>";
reqText += "<br>";
document.getElementById("output-div").innerHTML += reqText;
}
}
catch(e)
{
alert("There is problem" + e);
}
</script>
</head>
<div id="output-div"></div>
</body>
</html>
Further to my message earlier, I tried to make some changes to the code by duplicating this part of the code :
" function displayResult(result, source) {
var it = new sforce.QueryResultIterator(result); etc etc " as in the code pasted below - and I could get the result changed to as follows:
New Type:
Oztay
New Type:
San
This indicates the first output is capturing the Final Buyer correctly but the Label is wrong, while the second output is correct. I append below the amended code. I do hope you can have time to have a look at it and advise where should I change to get the first output's label correct - ie. Finish Type.
Next I tried to see what happens when I added the first function displayResult - html push statement "html.push( "Final Buyer:
" + record.Name + "
"); to the second function displayResult html push statement as follows:
function displayResult(result, source) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
html.push( "New Type:
" + record.Name + "
");
html.push("
");
html.push("
");
}
And I got the following output
Final Buyer:
Oztay
New Type:
Oztay
Final Buyer:
Sanforize
New Type:
Sanforize
The Final Buyer output Oztay is correct in first two output but doesn't match to only Final Buyer label while in the last two output the New Type is correct but does not match to New Type label.I am wondering if it is syntax problem somewhere along the lines but I could not figure out.
I do hope you can help me to pin point where is wrong - it is really puzzling for me, Thanks a lot in advance and sorry for the trouble caused.
SL
Amended Code:
<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 callback1 = { onSuccess : displayResult ,
onFailure : displayError,
source : document.getElementById("output1-div")
};
sforce.connection.query(
"SELECT Id, Name FROM Final_Buyer__c WHERE Id = '{!Customer_Order__c.Final_BuyerId__c}' " , callback1 );
var callback2 = { onSuccess : displayResult,
onFailure : displayError,
source : document.getElementById("output2-div")
};
sforce.connection.query(
"SELECT Id, Name FROM New_Type__c WHERE Id = '{!Customer_Order__c.New_TypeId__c}' " , callback2 );
}
function displayResult(result, source) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
html.push( "Final Buyer:
" + record.Name + "
");
html.push("
");
html.push("
");
}
source.innerHTML = html.join("");
}
function displayError(error) {
document.getElementById("output1-div").innerHTML =
"oops something went wrong ... " + error;
}
function displayResult(result, source) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
html.push( "New Type:
" + record.Name + "
");
html.push("
");
html.push("
");
}
source.innerHTML = html.join("");
}
function displayError(error) {
document.getElementById("output2-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>
Thanks a lot for your code feedback. I have tried your coding too and made the necessary changes. I think my complication encountered is because both the outputs that I am trying to get are from two different custom objects which are Lookups in another custom Object. My look up custom objects are Final Buyer and Finish Type (I have changed this from New Type) which are in the Customer Order object, so I tried to use the custom object convention eg Final_Buyer__c for the object and Customer_Order__c.Final_BuyerId__c for the "Name" value. This time, the S-controls run without any error message and there was also no error message from Firebug but there was no output. I am wondering why.
If it is ok with you, I am appending my code below to let you see if where I had gone wrong. I hope you dont mind checking where I went wrong
Many thanks again in advance
SL
My changed code as below:
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>
<script language ="Javascript1.3">
dojo.addOnLoad(init);
function init()
{
try
{
var resultFinal_BuyerId__c = sforce.connection.query("SELECT Id, Name FROM Final_Buyer__c WHERE Id = ' {!Customer_Order__c.Final_BuyerId__c} ' " );
var resultFinish_TypeId__c = sforce.connection.query("SELECT Id, Name FROM Finish_Type__c WHERE Id = ' {!Customer_Order__c.Finish_TypeId__c}' ");
var navLength=0;
var i;
var reqText = "";
var final_buyerid__cLen = resultFinal_BuyerId__c.size;
var finish_typeid__cLen = resultFinish_TypeId__c.size;
//The Loop that prints the value should go till maximum of either of object
if (final_buyerid__cLen < finish_typeid__cLen)
{
navLen = finish_typeid__cLen;
}
else{
navLen = final_buyerid__cLen;
}
for (i=0;i < navLen;i++)
{
if (i {
reqText = "Final Buyer:"+[i+1]+"
" + resultFinal_BuyerId__c.records[i].Name + "
";
}
else
{
reqText = "Final Buyer:"+[i+1]+"
No Records Found
";
}
if (i {
reqText +="Finish Type:"+[i+1]+"
" + resultFinish_TypeId__c.records[i].Name + "
";
}
else {
reqText +="Finish Type:"+[i+1]+"
No Record
";
}
reqText +="
";
reqText += "
";
document.getElementById("output-div").innerHTML += reqText;
}
}
catch(e)
{
alert("There is problem" + e);
}
}
</script>
I am looking ito your code will get back to you soon.
Best Regards
Suchitra :smileyhappy:
I found out only one error.
if (i <= finalLen-1) //There is no finalLen it should be finish_typeid__cLen
I think you have confused with naming conventions.
Change the names once you get the results so
Please use the code that I sent you second time and just change the query without any filter condition.
If it works then try to filter.
If it doesnot work the problem is with the way we write the filter.
Please let me know once you try
Regards
Suchitra
Thanks for your very kind feedback and advice.
I have altered the code to follow as per your original coding and changed only the naming conventions and taken out the filter. I had been able to get one of the results successfully ie
For the Finish Type I could get the result correctly as below:
Finish Type:2
San
For the Final Buyer I got an output like this in curly brackets "{type:'Final_Buyer__c', Id:null, Name:'Oztay ', }" It shows the result "Oztay" is correct captured but there is a full sentence here in between the curly brackets. I tried to solve this but don't seem to be making any headway.
I append below my new coding. If I change the output from "resultNew.records[i].Final_Buyer__r" to "resultNew.records[i].Final_Buyer__r.Name" as per the select statement, there is error indication "resultNew.records[i].Final_Buyer__r has no properties"
Do you think you would be able to find out what is wrong here from the code I pasted below. I am so sorry to trouble you and take up so much of your time but I really do hope you can help advise me.
Thanks once again for your kind help in advance
SL
Amended Code
html>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>
<script language ="Javascript1.3">
dojo.addOnLoad(init);
function init()
{
try
{
var resultNew = sforce.connection.query("SELECT Id, Name, Final_Buyer__r.Name FROM Customer_Order__c ")
var resultFinal = sforce.connection.query("SELECT Id, Name,Finish_Type__r.Name FROM Customer_Order__c ")
var navLength=0;
var i;
var reqText = "";
var newLen = resultNew.size;
var finalLen = resultFinal.size;
//The Loop that prints the value should go till maximum of either of object
if (newLen < finalLen)
{
navLen = finalLen;
}
else{
navLen = newLen;
}
for (i=0;i < navLen;i++)
{
if (i {
reqText = "Final Buyer:"+[i+1]+"
" + resultNew.records[i].Final_Buyer__r + "
";
}
else
{
reqText = "Final Buyer:"+[i+1]+"
No Records Found
";
}
if (i {
reqText +="Finish Type:"+[i+1]+"
" + resultFinal.records[i].Finish_Type__r.Name + "
";
}
else {
reqText +="Finish Type:"+[i+1]+"
No Record
";
}
reqText +="
";
reqText += "
";
document.getElementById("output-div").innerHTML += reqText;
}
}
catch(e)
{
alert("There is problem" + e);
}
}
</script>
It is not resultFinal.records[i].Finish_Type__r.Name and it should be just resultFinal.records[i].Name
Please alert
alert(resultFinal.records[i]); and look at the result the fieldname should be your entity after records[i].________
Best Regards
P Suchitra
I am pleased to inform you that I have been able to solve the above problem by using the Ajax Tool Explorer. The change comes from redefining the html push statements. I append below the code for your information. Now what I have to do is to filter the output to capture only one record that is required. I have some problem to handle this as it seems not so straightforward because of my custom objects and custom relationships involved. I hope you can advise me on the coding structure for the WHERE part of the Select/From statements as I tried various ways but could not get any output although there was no error reported by Firebug. FYI the Final Buyer and Finish Type are custom objects but are also the LookUps in the Customer Order object
Thanks in advance
Best Regards
SL
The amended coding:
<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 Id, Name, Final_Buyer__r.Name, Finish_Type__r.Name FROM Customer_Order__c ", callback );
}
function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
while(it.hasNext()) {
var record = it.next();
}
if (record.Finish_Type__r) {
html.push("Finish Type: " + record.Finish_Type__r.Name + "
");
}
if (record.Final_Buyer__r) {
html.push("Final Buyer: " + record.Final_Buyer__r.Name + "
");
}
html.push("
");
}
document.getElementById("output-div").innerHTML = html.join("");
}
function displayError(error) {
document.getElementById("output-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>
Thanks a lot for your feedback. I am sorry for my late response
I followed your advise and replace with resultFinal.records[i].Name but the output captures the Customer Order Name instead of the Final Buyer name and the Finish Type name. However for Finish Type if I put in the resultFinal.records[i]Finish_Type__r.Name I get the correct output. For Final Buyer I tried to change to resultFinal.records[i]Final_Buyer__c and the result shows "undefined"
I think my output complication is due to the custom object and custom relationship affecting how I should be defining the naming convention. I still cannot figure this out in this particular case. Do you think from what I mentioned above, there is some way out that you have come across?
Any feedback is highly appreciated
Thanks a lot
Best Regards
SL
resultsFinal.getArray('records')[i1]['Final_Buyers__r'].getArray('records')[i2]['Name']
Basically, the field 'Final_Buyers__r' would be an object with an array called 'records'-- The "__r" fields (or standard relationships) are enumerated as a QueryResult by themselves and have the same properties as a normal query.
~ sfdcfox ~
Thanks for your kind feedback and coding advise. I have changed the coding but there is a pop up indicating that the resultsFinal is not defined. I have changed my querying method from using S-control to a custom link url method instead. I have decided not to use the S-Control method for this particular case now because I think there is some complication involved due to my custom objects has master-detail and look up relationships and I think this complicates the coding somewhat. Nevertheless, I should like to thank you for your kind assistance and feedback.
Best Regards Always
SL
Just to let you know that I have decided to change my method of querying to using the custom link url method which is working instead of using the S-Control for this particular issue. I would like to thank you for the patience and your kind assistance and advise in providing me with the coding guidance which I appreciate very much
Best Regards Always
SL
I would like to let you know that I have changed the querying method from S-Control to custom link url method. I would like to thank you for your patience and kind help with the coding guidance which I appreciate very much
Thanks once again
Best Regards
SL
It is great pleassure helping someone. Thanks for being curtious to all of us.
Thanks & Best Regards,
P Suchitra
I have a similar issue with my code, but here i need to update the Custom Object from two diiferent objects.
can u guide me in this.
This is what i did.
Tan also let me know your feedback once you have tried this.
Regards
Suchitra :smileyhappy: