+ Start a Discussion
rajjjjrajjjj 

System.TypeException when doing callout

Hey I am getting an Invalid decimal exception.

Fatal error:system.typeexception..invalid decimal:B4DODC

Here is my class:

 

  @future(callout=true)
  public static void updateAccounts(List<Id> accountIds)
  {
  // get the custom city, state and country fields from account
    //List<Account> accounts = [select Id, Name, BillingCity, BillingState, BillingCountry
      //from Account where Id in :accountIds ];
    for(Account theAccount: [select Id, Name, BillingCity, BillingState, BillingCountry from Account where Id in :accountIds ]) { 
      
      List<String> address = new List<String>();
      address.add('');// empty street
      address.add(theAccount.BillingCity);
      address.add(theAccount.BillingState);
      address.add(theAccount.BillingCountry);
      String[] coordinates = GeoUtilities.getCoordinates(address);
      if(coordinates != null)
      {
        Decimal pos1 = Decimal.valueOf(coordinates[0].trim());
        theAccount.Latitude__c = pos1;
        Decimal pos2 = Decimal.valueOf(coordinates[1].trim());
        theAccount.Longitude__c = pos2;
        system.debug(Logginglevel.ERROR,'GeoUtilities coordinates ' + pos1 + ' ' + pos2 );    
      }
      else
      {
        system.debug(Logginglevel.ERROR,'GeoUtilities no coordinates!!! for address' );          
      }
      update theAccount; 
    }
    
  }

   
   public static String[] getCoordinates(String[] addressParts)
    { 
      String[] Coordinates; 
      String address = '';
      boolean needComma = false;
      system.debug(''+address);
      system.debug(''+address.length());
      
      for(Integer i = 0; i<10; i++)
        {
          if(needComma)
            address = address + ',';
          address = address + EncodingUtil.urlEncode(address,'UTF-8');
          needComma = true;
        }
    
	  if(address.length() == 0)
	  {
	    system.debug(Logginglevel.ERROR,
	      'GeoUtilities getCoordinates no address provided. Return null');    
	    return null; 
	  }
      system.debug(''+address);
      system.debug(''+address.length());
      
	  String url = 'http://geocoder.us/member/service/csv/geocode?q=street=701+First+Ave&city=Sunnyvale&state=CA&country=USA';
	  
	  //url += 'q=' + address; 
	  //url += '&output=csv'; 
	  
	  system.debug(Logginglevel.ERROR,'GeoUtilities getCoordinates url: ' + url);    
	  
	  Http h = new Http(); 
	  HttpRequest req = new HttpRequest();
	  
	  req.setHeader('Content-type', 'application/x-www-form-urlencoded'); 
	  req.setHeader('Content-length', '0'); 
	  req.setEndpoint(url); 
	  req.setMethod('POST');
	  String responseBody;
	  if (!Test.isRunningTest())
	  { 
	  // Methods defined as TestMethod do not support Web service callouts
	    HttpResponse res = h.send(req); 
	    responseBody = res.getBody();
	  }
	  else 
	  {
	    // dummy data
	    responseBody = '200,4,48.5,-123.67';
	  } 
	  String[] responseParts = responseBody.split(',',0); 
	  // the response has four parts: 
	  // status code, quality code, latitude and longitude
	  Coordinates = new String[2];
	  Coordinates[0] = responseParts[2];
	  Coordinates[1] = responseParts[3];
	    
	  return Coordinates; 
	 
	  }
  
  static testMethod void  testGetGeo()
  {
    String[] addressParts;
    String[] coordinates;
    
    addressParts = new List<String>();
    addressParts.add('');
    addressParts.add('San Diego');
    addressParts.add('CA');
    addressParts.add('USA');
    
    coordinates = GeoUtilities.getCoordinates(addressParts);
    System.assert(coordinates != null);
    System.assertEquals(2, coordinates.size()); 
  }
}

 

I have added the URL in the req.setEndpoint.

I was trying to use Trim() and Decimal.valueOf().
But none of them is working.

Here i had a Latitude__c and Longitude__c as Number fields.So i am trying to convert the result to decimal value.

Have anybody faced this issue. 

JitendraJitendra

Hi,

The error says that you are truing to conert string to Decimal. Please enclose your code in try catch and in case of exception, through fault message to client calling webservice.

rajjjjrajjjj

That worked for me....And after that i was getting the HTML Response but i am not able to parse it correctly.

Do u have any code which parses an HTML Response.

Like i was trying to get the latitude and longitude of an address. I want to parse the result and get those 2 values.

JitendraJitendra

Please post your HTML response.

rajjjjrajjjj
14:17:46.320 (320263000)|USER_DEBUG|[97]|DEBUG|printing responseBody-----------><html>
<head>
    <title>geocoder.us: a free US geocoder</title>
    <link rel="stylesheet" href="/style.css" />
</head>
<body>
<!-- this div is opened here, closed in the footer or somewhere else -->
<div align="center">
<script type="text/javascript"><!--
google_ad_client = "pub-1219510847949987";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = ["336699","B4D0DC","DDB7BA","FDEFD2"];
google_color_bg = ["FFFFFF","ECF8FF","FFF5F6","FDEFD2"];
google_color_link = ["0000FF","0000CC","0000CC","0000CC"];
google_color_url = "008000";
google_color_text = ["000000","6F6F6F","6F6F6F","000000"];
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<!--
<h2><a href="/blog">Read the Geocoder.US Blog</a></h2>
-->
 <hr>

  <div id="header"
  <a href="/"><h1>geocoder.us</a>
  find the latitude &amp; longitude of any US address - for <em>free</em></h1>
  </div>


<!-- you have to manually import the navigation.html file here -->
  <div id="navi">

			<a href="http://geocoder.us/"><div class="naviitem" id="naviactive">HOME</div></a>
			<a href="http://geocoder.us/help/"><div class="naviitem" >Documentation/Help</div></a>
			<a href="http://geocoder.us/help/faq.shtml"><div class="naviitem" >FAQ</div></a>
			<a href="http://geocoder.us/help/member.shtml"><div class="naviitem" >Member Info</div></a>
			<a href="http://geocoderus.blogspot.com" target=top><div class="naviitem" >Geocoder Blog</div></a>
			<a href="http://geocoder.us/about_us.shtml" target=top><div class="naviitem" >About us/looking foward</div></a>
			<!-- a href="./about.html"><div class="naviitem" >About</div></a -->
		</div>


<div align="center">
    <h2>Authorization Required</h2>
</div>

<div style="font-size: 10pt; width: 50%">
<p>A valid user account is required to access this feature. You can <a
href="/user/signup">sign up for a new account</a>, <a
href="/member/account">log in to an existing account</a> or <a
href="/user/reset">request a new password</a> for your account.</p> </div>

<p>
If you want to use the web services interface to the free service use these URL's, and do not add your username and password.
<p>

The service URLs are as follows:
<pre>
    http://geocoder.us/service/xmlrpc
    http://geocoder.us/service/soap
    http://geocoder.us/service/rest/...
    http://geocoder.us/service/csv/...
</pre>

The absolute simplest way to get a geocode from the free service is to use the csv interface. Entering this in your browser:
<p>
 http://rpc.geocoder.us/service/csv?address=1600+Pennsylvania+Ave,+Washington+DC
<p>
Returns this result:
<p>
38.898748,-77.037684,1600 Pennsylvania Ave NW,Washington,DC,20502
<p>

<hr noshade="1" width="90%" />
<div class="footer">
    = <a href="/">geocoder.us</a> = &copy; 2004-8 <a
href="http://locative.us/">Locative Technologies</a> = <a
href="/terms.shtml">terms &amp; conditions</a> = <a
href="mailto:contact@geocoder.us">contact us</a> = 
</div>
<div class="footer">

<!-- Open Knowledge Link -->
<a href="http://okd.okfn.org/"> <img alt="This material is Open Knowlege" border="0" src="http://m.okfn.org/images/ok_buttons/ok_80x23_blue.png" height="31" vspace="5" /></a> <!-- /Open Knowledge Link -->
<a href="http://www.perl.org"><img src="/img/rectangle_power_perl.png" width="88" height="31" hspace="5" vspace="5" border="0" /></a>
<a href="http://creativecommons.org/licenses/by-nc-sa/2.0/"><img src="/img/somerights20.png" width="88" height="31" hspace="5" vspace="5" border="0" /></a>
<!-- PayPal Logo --><table border="0" cellpadding="10" cellspacing="0" align="center"><tr><td align="center">

<tr><td align="center"><a href="#" onclick="javascript&colon;window.open('https://www.paypal.com/us/cgi-bin/webscr?cmd=xpt/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=400, height=350');"><img  src="https://www.paypal.com/en_US/i/logo/PayPal_mark_60x38.gif" border="0" alt="Acceptance Mark"></a></td></tr></table><!-- PayPal Logo -->

</td></tr>
</div>
</div> 
</body>

</html>

 This is the HTML Response i am getting in debug log.