2

When I run a code snippet in console, it doesnt give any error but when I use it inside an apex method, I keep getting this error: Method does not exist or incorrect signature: void newInstance(Decimal, Decimal) from the type String. Both the lines in first code have same error. geoLatitude and geoLatitude are defined initilly.

Relevant Code:

 for(AccountContactRelation ac : accConMap){

                Location locAcc = Location.newInstance(ac.Account.BillingLatitude, ac.Account.BillingLongitude);
                Location locZip = Location.newInstance(geoLatitude, geoLongitude);
                acConDistanceMap.put(ac.Id, Location.getDistance(locZip, locAcc, 'mi') );

            }

But when i put similar code in console, it works and gives me location.

 AccountContactRelation acc = [SELECT Id, Account.BillingLatitude, Account.BillingLongitude FROM AccountContactRelation Where AccountId = '001f000001POxdiAAD' LIMIT 1];
Location locAcc = Location.newInstance(acc.Account.BillingLatitude, acc.Account.BillingLongitude);
System.debug(locAcc);

What am I missing?

0

1 Answer 1

0

You have a line of code in your class that looks like this:

String location;

This overrides the class, because it is in a local scope.

Rename this variable, or use the System class to let the compiler know you mean the class Location, not the variable:

System.Location locAcc = System.Location.newInstance(ac.Account.BillingLatitude, ac.Account.BillingLongitude);
System.Location locZip = System.Location.newInstance(geoLatitude, geoLongitude);

This effect is known as "shadowing", and can happen any time you use a variable name or local class the same as a system class. It is best to avoid this problem by not using such names. See Namespace, Class, and Variable Precedence for more information.

The "hint" used to solve this puzzle is in the error:

Method does not exist or incorrect signature: void newInstance(Decimal, Decimal) from the type String

This tells me that you've declared a variable named location with the data type of String, and you're trying to call a method that doesn't exist on that object.

4
  • I cant believe that was it :) I used System.Location as well but forgot to use it after the equal to. Thank you @sfdcfox !!
    – P.Shri
    Apr 15, 2020 at 2:40
  • I have an additional question: I might post it separately. Is it possible to use DISTANCE in SOQL for lookup field something like this? [SELECT Id, AccountId, ContactId, Account.BillingAddress, DISTANCE(Account.BillingAddress, GEOLOCATION(:geoLatitude, :geoLongitude), 'mi') dist FROM AccountContactRelation WHERE DISTANCE(Account.BillingAddress, GEOLOCATION(:geoLatitude, :geoLongitude), 'mi') <:miles ]
    – P.Shri
    Apr 15, 2020 at 2:41
  • @P.Shri I'm not sure... never tried.
    – sfdcfox
    Apr 15, 2020 at 2:42
  • It doesnt work: Gives me internal salesforce error.
    – P.Shri
    Apr 15, 2020 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.