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
L0bster1L0bster1 

Troubleshooting help

I'm trying to write what I thought was a simple script to first search for some accounts meeting specified criteria and then update some fields on the contact objects that are related to those accounts.  My code is not throwing errors, but also not updating the Contact fields.  I'm hoping one of the experts here can easily spot something I've missed.  Thanks in advance. It may not be much, but most everything I've learned about s-controls has come from this forumn.  My code is below:
 
function updateObjects()
{
//query to get accounts associated with contacts
var queryResult = sforceClient.query("Select ID From Account where OwnerId = '{!$User.Id}' and Account_Status__c = 'Open'")
 
//move results into readable format
var account = queryResult.records[0];
 
// assign attribs to variables
var sfaccount2 = account.get("ID");
var sfaccount = new Date ();
var sfaccount1 = "Active";
 
var queryResult2 = sforceClient.query("Select ID, Automated_Date__c, Automation_Status__c From Contact where AccountId = " + sfaccount2 + "")
 
//new array to hold results of updates
var updateObjects = new Array();
 
// go through list of lhaccounts to update sfaccounts
for (var i=0;i<queryResult2.size;i++)
{
var lhaccount = queryResult2.records[i];
lhaccount.set("Automated_Date__c", sfaccount);
lhaccount.set("Automation_Status__c", sfaccount1);
 
// push updated record into new array for update process
updateObjects.push(lhaccount);
}
 
// submit new array with updated contacts
var saveResults = sforceClient.Update(updateObjects)[0];
parent.window.close();
}
PBS_IC786PBS_IC786
Try doing an "alert" on sfaccount2 before you run queryresult2 and see if it has a proper ID value...

that could be your issue, if not, I am sure there are plenty of talented folks here that will spot other things...
cheenathcheenath
Also consider using the production Ajax Toolkit instead of the beta toolkit.




L0bster1L0bster1
Thank you for your time and responses.  I tried adding alert (sfaccount2); but it didn't seem to work.  I also tried converting from beta to Ajax/9.0 and that seemed to create more problems for me (I'm sure because of me despite my best attmept to follow the Developer's Guide's guidance).  Is there reason to believe that the beta version simply can't do what I want to accomplish, or is the Ajax toolkit just easier to trtoubleshoot? Anyone recognize what might be wrong with the code above? Am I properly calling the results of the first query in the second query (' + sfaccount2 + ')?  Any further assistance would be very much appreciated.
Greg HGreg H
I am having difficulty identifying what it is that you are trying to do.  Primarily due to the fact that you initially query for what could result in a number of accounts but you anly look at the first result from the returned set.
 
As mentioned in an earlier post, I would suggest moving from the beta toolkit to the 9.0 version and this cannot be done by simply changing the toolkit link at the top of your scontrol.  You must also make changes to your query requests (i.e. change "sforceClient.query(..." to "sforce.connection.query(...") and how the results are handled.  Please see the API documentation for exact details.
 
If you'd rather just post the full scontrol, I'd be happy to take a look at it and assist with your issue using the beta toolkit.
-greg
L0bster1L0bster1
Thank you so much.  I would love to take you up on your offer, but first I want to take a stab at improving my code further and again trying to convert it to 9.0 before troubling you for more help.  Thanks.
 
 
L0bster1L0bster1
Below is some test beta code that works and if I could convert it to 9.0 I think the relationship ability in 9.0 would allow me to tweak the code below to accomplish my goal.  The goal is to create an s-control located on the home tab that when run would search for certain accounts and then update a "Last subject" field on the Account objects with the subject of the most recent task related to that account

Further below is my attempt to create 9.0 converted code.  It doesn't work.  I'm not sure what else I may be missing but I'm certain I cannot figure out what the initpage function is suppose to look like.  Any help would be greatly appreciated.


Test Beta Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script>
<script language="JavaScript">

function initPage()
{
sforceClient.registerInitCallback(updateObjects);
sforceClient.setLoginUrl("https://www.salesforce.com/services/Soap/u/7.0");
sforceClient.init("{!API.Session_ID}", "{!API.Partner_Server_URL_70}", true);
}

function updateObjects()
{
//query to get accounts to be updated
var queryResult3 = sforceClient.query("Select ID, Last_Activity_Subject__c, Name From Account where OwnerId = '{!$User.Id}' and Account_Status__c = 'Automated'")

//new array to hold results of updates
var updateObjects = new Array();

// go through list of accounts to update
for (var i=0;i<queryResult3.size;i++)
{
var temp = queryResult3.records[i];
var name = temp.get("Name");
var acc = queryResult3.records[i];

acc.set("Last_Activity_Subject__c", name);

// push updated record into new array for update process
updateObjects.push(acc);
}
// submit new array with updated contacts
var saveResults = sforceClient.Update(updateObjects)[0];

parent.window.close();
}
</script>
</head>
<body onload="initPage()">
</body>
</html>

Attempt at converting to 9.0 Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
<script language="JavaScript">

function initPage()
{
sforce.connection.registerInitCallback(updateObjects);
sforce.connection.setLoginUrl("https://www.salesforce.com/services/Soap/u/7.0");
}

function updateObjects()
{
//query to get accounts to be updated
var queryResult3 = sforce.connection.query("Select ID, Last_Activity_Subject__c, Name From Account where OwnerId = '{!$User.Id}' and Account_Status__c = 'Automated'")

//new array to hold results of updates
var updateObjects = new Array();

// go through list of accounts to update
for (var i=0;i<queryResult3.size;i++)
{
var temp = queryResult3.records[i];
var name = temp.get("Name");
var acc = queryResult3.records[i];
acc.set("Last_Activity_Subject__c", name);

// push updated record into new array for update process
updateObjects.push(acc);
}
// submit new array with updated contacts
var saveResults = sforce.connection.Update(updateObjects)[0];
parent.window.close();
}
</script>
</head>
<body onload="initPage()">
</body>
</html>
Greg HGreg H

Thanks for posting the code.  However, I am still not getting exactly what you are trying to do.  According to what you wrote you want to update a field on the account with the subject line from the most recent activity.

However, at no point do you go out to the tasks or events object to get a list of those activities.

So what I will do is provide a quick bit of code to at least get you querying for your accounts and displaying the results to the page.  With this code you should be able to get pointed in the right direction.

Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Testing AJAX v9.0</title>
<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
<script language="JavaScript">
<!--
function initPage() {
 var sql = sforce.connection.query("Select Id, Last_Activity_Subject__c, Name From Account where OwnerId = '{!$User.Id}' and Account_Status__c = 'Automated'"); //query for accounts owned by running user where Account_Status__c field is equal to "Automated"
 sqlResults = sql.getArray("records"); //return results to an array
 
 for (var i=0; i< sqlResults.length; i++) { //for each record in the returned array
  var HTML = "<table><tr><th>ID</th><th>Last Activity Subject</th><th>Name</th></tr>";
  var record = records[i]; //assign current record to variable record
  HTML += "<tr><td>"+record.Id+"</td><td>"+record.Last_Activity_Subject__c+"</td><td>"+record.Name+"</td></tr>"; //push information from this record to an HTML variable
 }
 HTML += "</table>"; //close table so display looks correct
 document.getElementById("results").innerHTML = HTML; //send resulting HTML to results DIV
}
//-->
</script>
</head>
<body onload="initPage()"> <!--when page is loaded call initPage() function-->
<div id="results"><!-- tag for holding results of initPage function -->
 <span style="background-color: #FF0000; font-weight: bold;">Loading...</span><!-- display loading until results are ready from AJAX then this text will be overwritten -->
</div>
</body>
</html>

 

Hopefully, this can be of some assistance,
-greg 
L0bster1L0bster1
Thank you Greg. I really appreciate your help. I am trying to create an S-Control that will live on the Home tab
and that will be executed on a click.  The code should  search the most recent task for each account, grab that
task's subject, and populate the Last Activity Subject field on the account.  This code should update multiple
accounts with a single click and only show a pop-up window that will close when the code is done running.  After
hours of reading documentation and piecing together pieces of code from you and others I have developed the
code below.  It has two issues that I know about (but maybe more).

1) I'm having problems grabbing the Task Subject field with "var Subject=records.get("Subject");" How should I
be referencing a child field?
2) The code throws the following error in Firebug. Anyone know what is causing this error?:

uncaught exception: {faultcode:'sf:INVALID_FIELD', faultstring:'INVALID_FIELD: No such column 'Tasks' on entity
'account'. ', detail:{fault:{exceptionCode:'INVALID_FIELD', exceptionMessage:'No such column 'Tasks' on entity
'account'. ', row:'-1', column:'-1', }, }, }


_invoke("update", [Object name=sObjects value=[3] isArray=true], true, UNDEFINED, [Object ns=urn:partner.soap.
sforce.com
, Object ns=sobject.partner.soap.sforce.com prefix=ns1]
, "/services/Soap/u/9.0", "urn:partner.soap.
sforce.com"
, "sobject.partner.soap.sforce.com")

My Code:
<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script>
<script language="javascript">

function do_code()
{
var qrdr = sforce.connection.query("SELECT Id, Last_Activity_Subject__c, Name, (SELECT Id, Subject FROM
Tasks order by ActivityDate desc limit 1) FROM Account where Account_Status__c = 'Automated' and
OwnerID = '{!$User.Id}'");

var updateObjects = new Array();

for (var i = 0; i < qrdr.getArray("records").length; i++)
{
var records = qrdr.records[i];
var Subject=records.get("Subject");

records.set("Last_Activity_Subject__c", Subject);
updateObjects.push(records);
}

var saveResults = sforce.connection.update(updateObjects)[0];
parent.window.close();
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body onLoad="do_code();">
</body>
</html>

Greg HGreg H

Okay.  I believe I understand what you're trying to do.  Your code has some errors and I don't know if the code I have below will work but it should get you close.

Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <title>Testing AJAX v9.0</title>
<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
<script language="JavaScript">
<!--
function initPage() {
 var sql = sforce.connection.query("SELECT Id, Last_Activity_Subject__c, (SELECT Subject FROM Tasks ORDER BY ActivityDate DESC LIMIT 1) FROM Account WHERE Account_Status__c='Automated' AND OwnerID='{!$User.Id}'");
 sqlResults = sql.getArray("records");
 var updateArray = new Array();
 for (var i=0; i< sqlResults.length; i++) {
  var accountUpdate = new sforce.SObject("Account");
  accountUpdate.Id = sqlResults[i].Id;
  accountUpdate.Last_Activity_Subject__c = sqlResults[i].Subject;
  updateArray.push(accountUpdate);
 }
 var saveChanges = sforce.connection.Update(updateArray);
 document.getElementById("results").innerHTML = saveChanges+"<br><a href=\"#\" onClick=\"JavaScript&colon; window.close();\">Click her to close this window...</a>";
}
//-->
</script>
</head>
<body onload="initPage()">

<div id="results"><span style="background-color: #FF0000; font-weight: bold;">Running...</span></div>

</body>
</html>

 

In order to troubleshoot, I added some stuff to display the results of the save request in the popped window.  When you get this to work you can simply call the window.close() function and eliminate that code.

Let me know how this goes,
-greg
L0bster1L0bster1
Thanks a lot Greg, the time you have put in helping me is greatly appreciated . I had not seen some of the
syntax you used in the many examples I've seen and it all seems to work great.  For those following at
home I just had to tweak sforce.connection.Update to sforce.connection.update.  There is still one issue.

In line:
  accountUpdate.General_Notes__c = sqlResults[i].Subject;

The code just does not seem to recognize  Subject because it is part of the child query.  If I replace it
with a field from the parent portion of the query it works fine.  I put alerts on alert(sqlResults) and it
shows that the variable is grabbing the children fields. However, the alert(sqlResults[i].Subject) says that
sqlResults[i].Subject is undefined.  I've tried sqlResults[i].Task.Subject and sqlResults[i].Tasks.Subject
but neither worked.

Any ideas?