+ Start a Discussion
Yam AdminYam Admin 

Help updating s-control to API 8.0

I'm trying to update the "recalcQuoteAmount.htm" s-control in the "Sales Quote with Product Line Items" app on the Appexchange to work with API 8.0, but I keep getting the following error:
Line: 1084
Char: 13
Error: Exception thrown and not caught
Code: 0

Here is my updated version of the s-control:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
    <script type="text/javascript" src="/js/functions.js"></script>
    <script src="/soap/ajax/8.0/connection.js"></script>
<script type="text/javascript">
<!--
// just run a total
function jumpbackQuote() { jumpback("/{!SFDC_520_Quote_ID}") }
function initPage() {
setTimeout("setup()",300);
}
var quoteLineTotal = 0;
function totalQuote(q) { quoteLineTotal += q.get("Ext_Net_Price__c"); }
function setup() {
var QuoteLineQ = sforce.connection.query(
"select Ext_Net_Price__c from SFDC_520_QuoteLine__c where Quote__c = '{!SFDC_520_Quote_ID}'");
if ( QuoteLineQ.className != 'QueryResult') {
alert ( "Query failed for quote lines") ;
if (QuoteLineQ.className == 'Fault' ) alert('fault: '+QuoteLineQ.toString());
jumpbackQuote();
}
if ( QuoteLineQ.size < 1 ) {alert("No Line Items in this Quote ?");jumpbackQuote();}

QuoteLineQ.records.map(totalQuote); // alert("total is "+quoteLineTotal);

// write Quote_Amount__c
var upd = new sforce.SObject("SFDC_520_Quote__c");
upd.set("Id","{!SFDC_520_Quote_ID}");
upd.set("Quote_Amount__c",quoteLineTotal);
var result = sforce.connection.create([upd]);

jumpbackQuote() ;
}

// util functions
function jumpback(retUrl) {
window.opener.location.href = retUrl;
window.close();
//window.parent.parent.location.href = retUrl; // works when in a frame of sfdc
}
Array.prototype.map = function(func) { // function called on each element of the array
var ret = [];
for(var x=0;x<this.length;x++) {
func(this[x]);
ret.push(this[x]); // return the entire list
}
return ret.length>0?ret:null;
}
</script>
<body onload="javascript:initPage();" >
<center>
<p><h3>Please wait, updating quote amount : {!SFDC_520_Quote_Name}</h3>
<br>
<img src="/img/waiting_dots.gif" border="0" width=156 height=25>
</center>
</body>
</html>

Any help would be greatly appreciated
cheenathcheenath
In 8.0 this code looks something like the one given below. I did not run/test this code.

Code:
function setup() {

  try {
  var QuoteLineQ = sforce.connection.query(
     "select Ext_Net_Price__c from SFDC_520_QuoteLine__c where Quote__c = '{!SFDC_520_Quote_ID}'");

    if ( QuoteLineQ.size < 1 ) {
      alert("No Line Items in this Quote —");
      jumpbackQuote();
    } else {
      var quoteLineTotal = 0;
      var records = QuoteLineQ.getArray("records");
      for (var i=0; i<records; i++) {
        quoteLineTotal += records[i].Ext_Net_Price__c;
      }

      // write Quote_Amount__c
      var upd = new sforce.SObject("SFDC_520_Quote__c");
      upd.Id = "{!SFDC_520_Quote_ID}";
      upd.Quote_Amount__c = quoteLineTotal;
      var result = sforce.connection.create([upd]);
      
      jumpbackQuote() ;
    }

  } catch(error) {
    alert("Query failed for quote lines: " + error);
  }
}


 

Yam AdminYam Admin

Thanks for your help, but when I try to run the code as a custom button, I get the following error:

Query failed for Quote Lines: [object error]

Any ideas?

Thanks

AltiumForceAltiumForce
Here's my working version:
Code:
function setup() 
{ 
    var quoteLineTotal = 0;
    var quoteLineNetTotal = 0;

    try
    {
        var QuoteLineQ = sforce.connection.query("SELECT Tax_Incl_Price__c, Ext_Net_Price__c FROM SFDC_520_QuoteLine__c WHERE Quote__c = '{!SFDC_520_Quote_ID}'");
    }
    catch(error)
    {
        alert(error);
        jumpbackQuote(); 
    }    
    if ( QuoteLineQ.size < 1 )
    {
       alert("No Line Items in this Quote —");
       jumpbackQuote();
    }

    var records = QuoteLineQ.getArray("records")
    for (var i=0; i<records.length; i++) 
    {
       quoteLineTotal    += records[i].getFloat("Tax_Incl_Price__c");
       quoteLineNetTotal += records[i].getFloat("Ext_Net_Price__c");
    } 
    
    // write Quote_Amount__c
    var upd = new sforce.SObject("SFDC_520_Quote__c");
    upd.set("Id","{!SFDC_520_Quote_ID}");
    if (quoteLineTotal == 0)
    {     
       upd.set("Quote_Amount__c","0.00");
    }   
    else
       upd.set("Quote_Amount__c",quoteLineTotal);
    
    try
    {
        sforce.connection.update([upd]);
    }
    catch(error)
    {
        alert("Error updating Quote: " + error)
    }

    // Update Opportunity Amount
    if ({!SFDC_520_Quote_Primary} == true)
    {
        var upd = new sforce.SObject("Opportunity");
        upd.set("Id","{!SFDC_520_Quote_Opportunity_ID}");
        if (quoteLineNetTotal == 0)
           upd.set("Amount","0.00");
        else
           upd.set("Amount", quoteLineNetTotal);
        try
        {
            sforce.connection.update([upd]);
        }
        catch(error)
        {
            alert("Error updating Opportunity: " + error)
        }
    }
    
    jumpbackQuote();
}