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
Ken_m3Ken_m3 

Converting in-line s-control to Apex trigger - looking for help

Hi,

I'm not a developer but I have been able to copy an S-control someone wrote for me to update certain fields across objects when a page is viewed.  Unfortunately this requires someone view the page (when most likely they will simply run reports to view information)

What I'm looking for is a sample trigger (complete with test coverage if possible) to use as a basis to convert some of my inline s-controls.  Below is a summary of the problem and the s-control that currently meets the need (as long as someone visits the page).  Any help in converting this to a trigger would be appreciated.  My intention is then to replicate the trigger in a similar fashion to the way I have the S-control

Cheers
Ken

In the following example (which is just one of the scontrols, calculated fields, and workflows used in the process):
  • project is master detail to stock
  • project is lookup on forecast
  • stage is an attribute of both stock and forecast.

The inline s control:
  • sums the total number of stock in a stage
  • sums the total price of stock in a stage
  • stamps these values on the forecast record
(there may be many forecast records for a stage as the forecasts are also broken down by period .. eg stage 1 forecast for 2007, stage 1 forecast for 2008 etc)

Ideally the update would be triggered if the number of stock or the price of any stock item changed.

Code:
<script
src="/soap/ajax/10.0/connection.js"
type="text/javascript"></script>

<script>


queryResult = sforce.connection.query(
"SELECT Id, Price__c, Counttotal__c, Stage__c, Project__r.Id FROM Stock__c where Project__r.Id = '{!Projects__c.Id}' and Stage__c = '{!ProjectForecast__c.Stage__c}' ")
queryResultIterator = new sforce.QueryResultIterator(queryResult)

count = 0;
pricesum = 0;


while(queryResultIterator.hasNext())
{ stagelots_Sub = queryResultIterator.next()

count += stagelots_Sub.getFloat('Counttotal__c');
pricesum += stagelots_Sub.getFloat('Price__c');

}
fcast = new sforce.SObject("ProjectForecast__c")
fcast.Id = "{!ProjectForecast__c.Id}"
fcast.Stagepricesum__c = pricesum;
fcast.StageLots__c = count;

result = sforce.connection.update([fcast])


</script>