You need to sign in to do that
Don't have an account?
rajjjj
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.
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.
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.
Please post your HTML response.
This is the HTML Response i am getting in debug log.