You need to sign in to do that
Don't have an account?
JohnC5674
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>
<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>
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.
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'");
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>
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.
<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 ~
For example:
~ sfdcfox ~