You need to sign in to do that
Don't have an account?
LisaE
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!
One other item.
As for implementing the S-control, would I actually create a Custom Button with Content Source = "Onclick JavaScript"?
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>
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();
You've done well so far with your S-Control. Here's the changes I would recommend (in red):
Code:
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 ~