From ccb19d7efc70ddaf54d4ff99371b944f50df131b Mon Sep 17 00:00:00 2001 From: Andrew Butenko Date: Mon, 10 Dec 2018 15:24:22 -0500 Subject: [PATCH] When address is not resolved there will be 0 results and code will fail with null-reference pointer exception --- CoreOperations/System/GeoCodeAddress.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/CoreOperations/System/GeoCodeAddress.cs b/CoreOperations/System/GeoCodeAddress.cs index f89ecb1..b8246a3 100644 --- a/CoreOperations/System/GeoCodeAddress.cs +++ b/CoreOperations/System/GeoCodeAddress.cs @@ -1,6 +1,7 @@ using System; using System.Activities; using System.IO; +using System.Linq; using System.Net; using Newtonsoft.Json; using Microsoft.Xrm.Sdk; @@ -55,9 +56,18 @@ protected override void ExecuteWorkflowLogic() throw new InvalidPluginExecutionException($"BingMaps Endpoint call failed - {string.Join(Environment.NewLine, response.ErrorDetails)}{Environment.NewLine}{response.StatusDescription}"); } + var geocodePoint = response.ResourceSets.FirstOrDefault()?.Resources.FirstOrDefault()?.GeocodePoints + .FirstOrDefault(); + + if (geocodePoint == null) + { + IsResolved.Set(Context.ExecutionContext, false); + return; + } + IsResolved.Set(Context.ExecutionContext, true); - Latitude.Set(Context.ExecutionContext, Convert.ToDecimal(response.ResourceSets[0].Resources[0].GeocodePoints[0].Coordinates[0])); - Longitude.Set(Context.ExecutionContext, Convert.ToDecimal(response.ResourceSets[0].Resources[0].GeocodePoints[0].Coordinates[1])); + Latitude.Set(Context.ExecutionContext, Convert.ToDecimal(geocodePoint.Coordinates[0])); + Longitude.Set(Context.ExecutionContext, Convert.ToDecimal(geocodePoint.Coordinates[1])); } }