1

I have a list of accounts which I cycle through and get the distance between two UK postcodes provided.

the relevant part is this:

integer count = 0;

for(Account a: currentRecords){
    system.Debug('count: ' + count);
    system.debug('Account1: ' + currentAccount.BillingPostalCode);
    system.debug('Account2: ' + a.BillingPostalCode);
    system.debug('distance: ' + getter('SE26 4BB','se6 2nl'));
    count += 1;
}

This does not produce the results in the order expected.

Debug Log

As you can see they aren't firing in the right order.

When I run this code:

integer count = 0;

for(Account a: currentRecords){
    system.Debug('count: ' + count);
    system.debug('Account1: ' + currentAccount.BillingPostalCode);
    system.debug('Account2: ' + a.BillingPostalCode);
    //system.debug('distance: ' + getter('SE26 4BB','se6 2nl'));
    count += 1;
}

I get the order i'd expect:

debuglog2

I'll leave the 'getter' functions code below if that is any help but it is going fundamentally against my understanding of programming.

public double getter(String pCode1, String pCode2){

    Http httpProtocol = new Http();
    HttpRequest request = new HttpRequest();
    String endpoint;
    request.setMethod('GET');
    Double lat1;
    Double lon1;
    Double lat2;
    Double lon2;
    String JSONresponse;


    endpoint = 'https://api.postcodes.io/postcodes/' + pCode1;          
    request.setEndPoint(endpoint);
    HttpResponse response = httpProtocol.send(request);

    JSONresponse = response.getBody();
    JSONParser parser = JSON.createParser(JSONresponse);


    while (parser.nextToken() != null) {
        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
            String fieldName = parser.getText();
            parser.nextToken();
            if(fieldName == 'latitude') {
                lat1 = decimal.valueOf(parser.getText());
            }
            else if(fieldName == 'longitude') {
                lon1 = decimal.valueOf(parser.getText());
            }
        }
    }

    endpoint = 'https://api.postcodes.io/postcodes/' + pCode2;          
    request.setEndPoint(endpoint);
    response = httpProtocol.send(request);

    JSONresponse = response.getBody();
    parser = JSON.createParser(JSONresponse);


    while (parser.nextToken() != null) {
        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
            String fieldName = parser.getText();
            parser.nextToken();
            if(fieldName == 'latitude') {
                lat2 = decimal.valueOf(parser.getText());
            }
            else if(fieldName == 'longitude') {
                lon2 = decimal.valueOf(parser.getText());
            }
        }
    }
    double distance;
    if(lat2 != 9999 && lon2 != 9999){
        distance = haversine(lat1,lon1,lat2,lon2);
    }
    else {
        distance = 9999;
    }

    lat2 = 9999;
    lon2 = 9999;


    return distance;
}

public double toRadians(double val){
    double newVal = val * Math.PI / 180;
    return newVal;
}


public double haversine(double lat1, double lon1, double lat2, double lon2) {
    double newVal;

    double R = 6371000;
    double la1 = toRadians(lat1);
    double la2 = toRadians(lat2);
    double deltaLat = toRadians(lat2-lat1);
    double deltaLon = toRadians(lon2-lon1);

    double a = Math.sin(deltaLat/2) * Math.sin(deltaLat/2) + Math.cos(la1) * Math.cos(la2) * Math.sin(deltaLon/2) * Math.sin(deltaLon/2);

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    newVal = (Math.round(R*c/16.09344))/100.00;
    return newVal;

}
4
  • How are you running this code? from Developer console? Jul 15, 2016 at 18:13
  • 4
    Can you explain why your first screen shot is evidence of the wrong order? It starts with the distance output of count 25 with the remainder of the count 25 output just not shown. The count 26 output looks correctly ordered.
    – Keith C
    Jul 15, 2016 at 18:17
  • Because there is no count 25 show. It's another part of it I'm not sure of. What I'd expect is the count of 0, the post code 1, post code 2, then the distance between the two. I can't underhand why adding an extra line to the end makes the loop skip printing for the first 25? And yes I'm using the developer console Jul 16, 2016 at 9:07
  • I can't see why it wouldn't print the count: 0 to count: 25 Jul 16, 2016 at 9:10

0

You must log in to answer this question.

Browse other questions tagged .