My company uses Field Service and I have a single webpage our company uses to allow 3rd Party contractors to accept or decline work order assignments. No changes have been made to our processes or to the code for this app, but yesterday we began getting errors on the webpage.
The error is that an "Object reference not set to an instance of an object." The thing is I'm having trouble finding any null objects where this error is being thrown.
On Page_Load I have a method that instantiates a private IOrganizationService variable. This method executes successfully and returns a connection. I updated this method a few months ago when WS-TRUST was deprecated and it seems to have been working with no issues:
public IOrganizationService GetOrganizationService() { string serverUrl = ConfigurationManager.AppSettings["ServerUrl"]; string userName = ConfigurationManager.AppSettings["UserName"]; string password = ConfigurationManager.AppSettings["Password"]; string errorMsg = String.Empty; try { ClientCredentials clientCredentials = new ClientCredentials(); clientCredentials.UserName.UserName = userName; clientCredentials.UserName.Password = password; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; this.organizationService = new CrmServiceClient("AuthType=OAuth;Username=CREDUSER;Password=CREDPWD;Url=ORGURL;AppId=AZUREAPPID;RedirectUri=AZUREREDIRECTURI;LoginPrompt=Auto"); } catch (Exception exception) { errorMsg = exception.Message; } return this.organizationService; }
Then, based on a querystring variable in the URL there is a lookup to retrieve the Work Order record in CE:
public void RetrieveWorkOrder() { if (base.Request.QueryString["workorder"] != null) { string QEmsdyn_workorder_msdyn_name = Convert.ToString(base.Request.QueryString["workorder"]).Trim(); this.hdn_workOrderNumber.Value = QEmsdyn_workorder_msdyn_name; QueryExpression QEmsdyn_workorder = new QueryExpression("msdyn_workorder"); QEmsdyn_workorder.ColumnSet.AddColumns(new string[] { "dxc_primarytechnician", "dxc_backuptechnician", "msdyn_name", "createdon", "dxc_scheduledbooking", "dxc_assignedtechnician", "msdyn_substatus" }); QEmsdyn_workorder.Criteria.AddCondition("msdyn_name", 0, new object[] { QEmsdyn_workorder_msdyn_name }); // debugging NullReferenceException string msg = String.Empty; if (Object.ReferenceEquals(QEmsdyn_workorder, null)) { msg = "QEmsdyn_workorder is null"; } if (Object.ReferenceEquals(this.organizationService, null)) { msg = "organizationService is null"; } if (Object.ReferenceEquals(QEmsdyn_workorder_msdyn_name, null)) { msg = "QEmsdyn_workorder_msdyn_name is null"; } // try calling a method of organizationservice just as an extra step msg = this.organizationService.ToString(); // end debuggin EntityCollection workOrderCollection = this.organizationService.RetrieveMultiple(QEmsdyn_workorder); // <--- ERROR THROWN HERE if (workOrderCollection.Entities.Count > 0) { this.workOrderEntity = workOrderCollection.Entities[0]; this.hdn_workOrderCreatedOn.Value = Convert.ToString(this.workOrderEntity["createdon"]); this.hdn_scheduledBooking.Value = Convert.ToString(this.workOrderEntity["dxc_scheduledbooking"]); this.hdn_assignedTechnician.Value = Convert.ToString(((OptionSetValue)this.workOrderEntity["dxc_assignedtechnician"]).Value); } } }
The organizationService object is not null, the QEmsdyn_workorder QueryExpression object is not null, the ColumnSet and Criteria members of the QueryExpression are not null, yet this line continues to produce the error:
EntityCollection workOrderCollection = this.organizationService.RetrieveMultiple(QEmsdyn_workorder);
I've also checked the ColumnSet values just to make sure they are correct, although in my head I know they are correct and also again this has been working for over a year with the only change being to change authentication when WS-TRUST was deprecated a few months ago. But even after that change this app has been used daily for months with no issues.
I'm feel like I've hit a wall with this for the moment so wanted to reach out to see if anyone else has any ideas or are experiencing a similar issue. The answer may be right in front of my eyes but I've looked at this long enough that I can't see it.