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
J4J4 

Adding a Summary field using an Ajax query

SControl: AttendeeCount
Desc: Attendee count for hidden field on a custom object (Conference) that is related to Contacts

This one is a bit tricky but useful if you do not have a master child relationship between your two objects. The trick is to add a hidden SControl on the parent object that is going to execute each time the page is viewed. It should query the related child data and if your query data does not match update your summary field and refresh the page. Warning: Don't get caught in a loop.


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/10.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
dojo.addOnLoad(init);

function init() { 
  countResult = sforce.connection.query("SELECT id FROM Contact WHERE Conference__c = '{!Conference__c.Id}'");
  //alert(countResult.size);
  //alert({!Conference__c.AttendeeCountHidden__c});
  if (countResult.size != {!Conference__c.AttendeeCountHidden__c}) {
    conf = sforce.connection.retrieve("AttendeeCountHidden__c", "Conference__c", ["{!Conference__c.Id}"]);
    conf[0].AttendeeCountHidden__c = countResult.size;
    conf = sforce.connection.update(conf);
    if (conf[0].getBoolean("success")) {
      window.top.location.href = "/{!Conference__c.Id}"
    } 
  }
}
</script>

 





JohanLiljegrenJohanLiljegren
Hi J4,

Thanks for posting solutions alongside all of the questions here on the board.

I've tried doing something similar to this solution in the past and I always ended up with two big issues, performance and accuracy due to security.
  1. Performance
    Performing a query, updating a field and (potentially) refreshing the screen every time a user visits the detail page of the master object is a performance/usability killer. Remember S-Controls are run client side.

  2. Accuracy due to security
    In addition to the fact S-Controls are run client side, they are also executed in the user's context, i.e. are subject to the user's role and profile and their security settings. This can cause a problem in this situation. An real-life example:
    I wanted to created a field on the Account object counting all open opportunities on the Account. Since it's not possible to do via a Rollup Summary field, I decided to try an S-Control being run each time a user views the Account. I did get it to work, but the number kept changing back and forth. The S-Control counting the opportunities can only see as many opportunities as the user running the S-Control can see due to security settings (i.e. via their position in role hierarchy and sharing rules) and therefor, the number will always be exactly as many open Opportunities as the user can so and not (as expected) the total number of open Opportunities.
I ended up having to create an off-site nightly run Java program (run as a System Admin type access user) that went through all accounts updating it's Account's number.
Before you move any further, these issues might be interesting to take into consideration.