+ Start a Discussion
finalistfinalist 

Making the transition from S-Control page to snippet, or at least not loading a page

I have an S-Control that mimics the functionality of a roll-up summary field for a lookup rather than a Master/Detail relationship.  I'd like the option of being able to call it from a couple of locations, which suggests making it a snippet, or at minimum being able to run it without an interim page load - if it can run and then refresh the page when it's completed, that's all that's required.
 
If it were contained in a snippet, then I could use an 'Execute Javascript' button to pass it the parameter I need, or slip the call into the end of another process as long as I passed in the correct initial Id, correct?
 
Here is the entirety of the S-Control - it's mostly functions, but there are a few variables and includes (or what will be includes, I presume) that would need updating.
I have the cookbook, but I don't see it going into the detail of making this transition.  Any and all help would be appreciated.
 
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html>
<head>
<script type="text/javascript" src="/js/functions.js"></script>
<script type="text/javascript" language="javascript" src="/soap/ajax/11.1/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script type="text/javascript" language="javascript">

// Salesforce parameter Ids:
// Time Entry: Customer Subscription = 00N70000002JgwU
// Time Entry: Subscription & Service Block = 00N70000002Jgyz

// Assignment of a field: "&CF" + field id + "=" & field
// Example #1: &CF00N70000002Jgxx={!User.Name}
// Example #2: &CF00N70000002Jgyg=0.25

var finishedLocation;
customerSubscriptionName = new String;
serviceBlockId = new String;
hoursRemaining = new Array();
results = new Array();

var conn = sforce.connection;

function displayError(error) {
      // displays a friendlier error message to the user
      alert("An error occurred while processing your request: \n" + error);
};

function getActiveServiceBlocks() {

    try {

              results = conn.query("Select Name, Id, Active__c, Hours_Remaining__c from Subscription_And_Service_Block__c where Customer_Subscription__c = '{!Customer_Subscription__c.Id}' And Active__c = True");

              if (results.size > 0) {
                    var ri = new sforce.QueryResultIterator(results);
                    while (ri.hasNext()) {

                            parseServiceBlock(ri.next());
                    };

                   // - All of the records have been processed and the total hours summed; 
                   //   update the Customer Subscription with the total amount

                   updateTotalHoursRemaining();

            } else {

                  // no records were returned, i.e. there are no active Service blocks
                 alert("There are no active Subscription & Service blocks for this Subscription");

                 noResultsLocation();

           }

      } catch(ex) {
             displayError(ex);
      }
}

function parseServiceBlock(record) {

        // - Capture the start date and Id of the Subscription and Service Block
        subAndServiceBlockName = record.Name;
        serviceBlockId = record.Id;
        var hours = record.Hours_Remaining__c;

        hoursRemaining.push(hours);
}

function updateTotalHoursRemaining() {

       try {
                   var totalHoursRemaining;
                   for(i = 0; i < hoursRemaining.length; ++i) {
                             if (i == 0) {
                                   totalHoursRemaining = hoursRemaining[i];
                             } else {
                                   totalHoursRemaining = parseFloat(totalHoursRemaining) + parseFloat(hoursRemaining[i]);
                             };
                   }

                   updateCustomerSubscription = new sforce.SObject("Customer_Subscription__c");
                   updateCustomerSubscription.Id = '{!Customer_Subscription__c.Id}';
                   // - set Active Hours Remaining using HoursRemaining
                   updateCustomerSubscription.Active_Hours_Remaining__c = totalHoursRemaining;

                   result = conn.update([updateCustomerSubscription]);

                   noResultsLocation();

          } catch(ex) {
                 displayError(ex);
       }

}

function noResultsLocation() {

    try {
        // - Determine whether initial location is in the Console (DesktopPage) or in a tab, to return user to same place
        if(window.parent.location.href=="https://na5.salesforce.com/ui/desktop/DesktopPage")
            {
                finishedLocation = '/{!Customer_Subscription__c.Id}&isdtp=mn';
            } else {
                finishedLocation = '/{!Customer_Subscription__c.Id}';
            };

           window.location = finishedLocation;

     } catch(ex) {
          displayError(ex);
     }
// end function noResultsLocation
}

function init() {
       getActiveServiceBlocks();
}

dojo.addOnLoad(init);

</script>
</head>
<body>
</body>
</html>

 
Thanks!


Message Edited by finalist on 08-21-2008 08:34 PM