+ Start a Discussion
Nik_PNik_P 

Relationship query and S-Control.....

Hey all,
 
I have a small issue with an S-Control I am writing, mostly as it deals with relationships, and I haven't been able to pull out the information I require with my query string.....
 
To put in context, I have a custom object named Site.  I also have a custom object named Site_Contact, which is to allow a many-to-many relationship for contacts to sites.  The site also has a lookup (not master) to the account.  What my s-control is going to do is allow them to press the button, and change any contacts associated to the site to be associated with the account which owns the site (if site is purchased by Company B, the contacts associated with the site who used to work for Company A (who used to own the site) would also move to Company B)...This s-control would run off of a button on the related list on the site detail.
 
I tried the inner join, but then, after reading through the APEX white paper, see that the joins do not work with a SOQL Call...
 
so, I have the following code for my query right now....
 
Code:
var qStr = "Select Id,AccountID, (SELECT Contact__c, Site__C From Site_Contact__c) from Contact";
try{
   var queryResults = sforce.connection.query(qStr);
   if (queryResults != null){
      if (queryResults.size > 0){
      var records = queryResults.getArray('records');
      }
   }
} 
catch(err) {
   alert(err)
}

 The tables are set up as:
Contact (Standard SFDC)
Site (Id, Contact__c)
Site_Contact (Id, Contact__c, Site__c)
 
So, I am thinking that I need to grab Id, AccountID from Contacts where (grab Id from Site Contact where the property__c field is equal to the Id of the Site detail you are on.

Of course, this doesn't seem to be working for me.  Would any of you gurus be able to point out my err in trying
Nik_PNik_P
Ooops....pressed too quick.
 
Thanks for reading, and for any assistance you all may be able to give.
 
Thx,
 
Nik
Nik_PNik_P

Hello all,

Well, I found out my error on how to use relationships...used the soql paper to figure that one out.  now, though, it seems that my s-control continually loops, and never completes the update....I place the code up, just in case anyone sees what i can't right now...

Code:

<html>

<head>

<script src="/soap/ajax/8.0/connection.js"></script>
<script src="/js/dojo/0.3.1/dojo.js"></script>
<script src="/soap/ajax/8.0/apex.js"></script>
<script language="javascript">

// DECLARE CONSTANTS
var ACCOUNT_ID="{!Property__c.Account__c}";
var POP_MSG="";

alert('Nik');
// window.opener.navigate(POS_LINK);
// window.close();


function closeApps(){
var records = getApps();
if (records != null) {
if (records.length > 0) {
updateApps(records);
}
}
}
// else { POP_MSG; }


// return;
// }

function getApps(){
// NOTE: This function will return the 1st batch of related Job App records
var qStr = "Select Id, AccountID, (Select Property__c from Property_Contact__r where Property__c = 'a0250000004IBqZ') from Contact";

try{
var queryResults = sforce.connection.query(qStr);
if (queryResults != null){
if (queryResults.size > 0){
var records = queryResults.getArray('records');
}
}
}
catch(err) {
alert(err)
}
}
//return records;

function updateApps(records){
alert ("UpdateApps");
var i = 0;
alert(i);
// loop through records and set the field values
for (i=0;i<records.length;i++){
records[i].AccountId = "0015000000J5Eam";
}
}

// perform the update

try
{
var iCounter = 0;
var saveResult = sforce.connection.update(records);
if (saveResult.length != records.length) throw "create failed";
var errorMsgs = "";
{
for (var x=0;x<saveResult.length;x++)
{
if (!saveResult[x].getBoolean("success"))
{
throw "failed: " + saveResult[x];
}
else
{
iCounter++;
}
POP_MSG += iCounter + "Completed Transfer";
}
}
}
catch (e)
{
POP_MSG += "\r\nERROR\r\n" + e;
}
// return;


</script>
</head>
<body onload = "closeApps();">
<center>
<br />
<table width="100%">
<tr>
<td align=center>
<span class="moduleTitle">Updating Contacts... please wait</span>
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td align=center>
<img src="/img/waiting_dots.gif" alt="Please wait..." title="Please wait..." width=196 height=20>
</td>
</tr>
</table>
<br />
<div id="output"></div>
</center>
</body>
</html>


 

(i am using a hard coded accountid right now to test...)

Thanks again,

Nik