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
LisaELisaE 

Need S-Control to sum fields on custom object and save/display sum on lookup standard object

I have created a custom object called Location and intended to define a Master-Detail field for the Account object so that I could utilize the new summary functionality to total the Number of Employees for all Locations associated with an Account.
 
However, I also need the ability to update the Master-Detail field on the Location so that I can move the Location to another Account, which forced me to change the Master-Detail relationship to a Lookup relationship.
 
From many of the posts, it appears that I should be able to use an S-control to sum the Number of Employees for all Locations associated with a particular Account and either display that value on the Account detail page, or actually create and update a custom field on the Account page.
 
Can anyone provide the S-control logic that would accomplish this task?  Any other ideas you may have would be much appreciated!
 
LisaELisaE

One other item.

As for implementing the S-control, would I actually create a Custom Button with Content Source = "Onclick JavaScript"?

LisaELisaE

Here is the current code that I have as an Account customer button:

<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script>
//Total Employees for Locations
test();
function test() {
/*

   subResult = sforce.connection.query("Select SUM(Employees__c) from Location__c where Account__c = '{!Account.Id}' + ");
   subRecords = subResult.getArray("records");
   var subRecord = subRecords.length;
   var total = 100;
   var account = new sforce.SObject("Account");
   account.ID = "{!Account.Id}";
   account.Total_Employees__c = total;
   result = sforce.connection.update([account]);
   window.parent.location.href = "/{!Account.Id}";
  sforce.connection.update;
*/
}

Here is the message I see at the top of the account detail page right before my customer button:

') } catch (e) { alert('A problem with the OnClick JavaScript for this button or link was encountered:\n\n' + (e.message ? e.message : e)) } }; element.invokeAction(); }</SCRIPT>

LisaELisaE

Ok...from reading multiple posts and trying to supplement my limited JS knowledge, I have come up with the following...  Unfortunately, there still seems to be a problem.  If anyone can help determine how this needs to be modified, I'd appreciate it!

 

{!REQUIRESCRIPT("/soap/ajax/8.0/connection.js")}

queryResult = sforce.connection.query("Select Employees__c from Location__c where Account__c =
               '{!Account.Id}'");

total = 0;
if (queryResult.size > 0) {
   records = queryResult.getArray("records");
   for (var i = 0; i < records.length; i++) {
       total += records[i].Employees__c;
}
}

   account = new sforce.SObject("Account");
   account.ID = "{!Account.Id}";
   account.Total_Employees__c = total;
   result = sforce.connection.update([account]);

   window.location.reload();

sfdcfoxsfdcfox

You've done well so far with your S-Control. Here's the changes I would recommend (in red):

Code:

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
queryResult = sforce.connection.query(
      "Select Employees__c from Location__c where Account__c = '{!Account.Id}'")
queryResultIterator = new sforce.QueryResultIterator(queryResult)
total = 0
while(queryResultIterator.hasNext())
{ Account_Sub = queryResultIterator.next()
   total += Account_Sub.getInt('Employees__c')
}
account = new sforce.SObject("Account")
account.Id = "{!Account.Id}"
account.Total_Employees__c = total
result = sforce.connection.update([account])
window.location.reload()

You don't need to use the latest version of the API, but there's no harm in upgrading. Other than that, I used the queryResultIterator to reduce the amount of logic and complexity needed, as well as potentially handling any number of records (automatic queryMore support for records with a large number of related records). Note how I didn't need to do anything special (eg. check to see if there were any records, get an array length, etc).

~ sfdcfox ~

LisaELisaE
Thanks sfdcfox.  I always like to know that there are multiple options. 
 
We are getting ready to review the Professional edition.  It will be interesting to see if the below functionality is supported...