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
JohnC5674JohnC5674 

S-Control Help!!! Count related objects

We have an object called Subscribers that is tied to the account level.  I added a field called Total Lines to the account record and I am trying to create an scontrol to count all subscribers tied to an account and populate that number in the Total Lines field on the account.  Can someone please help me with this? I have pasted the scontrol below.
 
<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script>
<script>
//Query for Owner Division
var accountID = "{!Account.Id}";
var subkey = "{!Subscriber__c.SUB_KEY__c}";
{
subResult = sforce.connection.query("Select Name from Subscriber__c where Account__c='accountID and subkey<>null'");

subRecords = subResult.getArray("records");
var subRecord = subRecords.size;

//Query for Account Custom Division Field
var accountID = "{!Account.Id}";
accountResult = sforce.connection.query("Select Total_Lines__c from Account where id = '" + accountID + "'");
accountRecords = accountResult.getArray("records");
var accountRecord = accountRecords[0];

//If the Division on the Account is populated, do not update the Account
if(accountRecord.Total_Lines__c != subRecord) {
var account = new sforce.SObject("Account");
account.ID = "{!Account.Id}";
account.Total_Lines__c = subRecord;
result = sforce.connection.update([account]);
window.parent.location.href = "/{!Account.Id}";
}
}
</script>
mojeebahmedmojeebahmed
Hi John,

    the problem might be with these line of code
    subRecords = subResult.getArray("records");
    var subRecord = subRecords.size;

there is no size property in "records" try using length or the statement below

    var subRecord = subResult.size;

The rest of the code looks fine. let me know what happen after this change.
cheenathcheenath
I think the query string is wrong:

instead of:

subResult = sforce.connection.query("Select Name from Subscriber__c where Account__c='accountID and subkey<>null'");

Try this:

subResult = sforce.connection.query("Select Name from Subscriber__c where Account__c='" +
  accountID + " and " + subkey + "<>null'");

 

JohnC5674JohnC5674

I have the scontrol working now.  However, when I login as another profile and open an account to run the scontrol, the scontrol just keeps running.  What would be causing this?

 

<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script>
<script>
//Query for Owner Division
var accountID = "{!Account.Id}";

{
subResult = sforce.connection.query("Select Name from Subscriber__c where SUB_KEY__c!='' AND Account__c = '" + accountID + "'");

subRecords = subResult.getArray("records");
var subRecord = subRecords.length;

accountResult = sforce.connection.query("Select Existing_Lines__c from Account where id = '" + accountID + "'");
accountRecords = accountResult.getArray("records");
var accountRecord = accountRecords[0];

//If the Division on the Account is populated, do not update the Account
if(accountRecord.Existing_Lines__c != subRecord) {
var account = new sforce.SObject("Account");
account.ID = "{!Account.Id}";
account.Existing_Lines__c= subRecord;
result = sforce.connection.update([account]);
window.parent.location.href = "/{!Account.Id}";
}
}
</script>

TCAdminTCAdmin
It should repeat the code no matter what profile is used.  The inline code will trigger each time the page is loaded and the final command does exactly that.  What I've tried in the past is to use a comparitive command between the visible value and the new calculated value.  If they are the same then it doesn't reload the page.  If it is different then it will reload.  This has been hit-and-miss on the different browsers or versions of browsers and I haven't been able to do it reliably.  let me know if you get it to work.

Outside of this it would probably be better to put the code on the child object which will update the parent and then you don't have to reload the page.  When you look at the parent it has already been updated by the child when it was viewed.
sfdcfoxsfdcfox
When I do this, I use a workflow field update to indicate if the record has been updated, and if so, then execute a recalculate command and uncheck the field. That way, the code only runs if the system flags that an update needs to occur. In the S-Control, you would simply write:

<script>
if({!My_Object__c.Is_Updated__c})
{ recalculateTotals()
  resetUpdatedFlag()
  window.top.location = window.top.location
}

// code for recalculate and reset flag goes here. //
</script>

Hope this helps.

~ sfdcfox ~
JohnC5674JohnC5674
Thank you for your help with this.  I researched and also found that the reason it keeps running is because of sharing rules.  When a user opens the account and does not own the account, the scontrol will not update the field because they only have read access to the account if they don't own it.  Is there any way to allow the scontrol to update this field without modifying the sharing rules?
sfdcfoxsfdcfox
No, you can not update a field on a record that you can not edit; this would be a violation of security, and the API will not allow it. Instead, your application should kindly inform the user that they were unable to update the record and then not refresh the page.

For example:
results = sforce.connection.update([record])
if(!results.getBoolean('success') || !results.getArray('records')[0].getBoolean('success'))
  alert('Warning: Record could not be updated.')
else
  window.top.location = window.top.location
~ sfdcfox ~