Our production environment only has been updated to the new Customer Insights - Journeys app, which means we are not able to use a calculated field in a segment. This article summarizes how to implement Customer Insights - Data which should allow us to use a calculated field in a segment. We followed the steps exactly.
Unfortunately, the result of this was severely problematic. To skip all of the consequences and jump to the main issue, we started getting the following exception in our code when querying the Dynamics Dataverse Web API, but only in our production environment. We have two other environments (Dev, UAT) which have identical code and solutions, and the error is only occurring in production.
The error is
Sql error: Generic SQL error. CRM ErrorCode: -2147204784 Sql ErrorCode: -2146232060 Sql Number: 207
Looking up those errors, I found the following information:
Sql Number 207: In SQL Server, error number 207 typically indicates "Invalid column name". This means the SQL query is referencing a column that does not exist in the table or view being queried.
- CRM ErrorCode -2147204784: This is a generic CRM error often associated with database query issues, such as missing columns, incorrect schema, or configuration problems in Dynamics CRM.
- Sql ErrorCode -2146232060: This is a SQL Server error code that aligns with issues like invalid object references or schema mismatches.
We get the error when trying to do an expand on a lookup column without specifying any columns to select which will return all columns (the lookup is for a record on the contact table). Here is an example of the request that is causing an error:
/api/data/v9.0/sh_frlocationcontacts?$select=sh_frlocationcontactid,sh_name,sh_contact,_sh_contact_value,sh_frlocation,_sh_frlocation_value,statecode&$expand=sh_frlocation($select=sh_frlocationid,sh_address_latitude,sh_address_longitude,sh_name,statecode,sh_address_line1,sh_address_line2,sh_address_province,sh_address_city,sh_address_postalcode,rb_fragencytraveldistance,sh_frfreezertype,sh_frfridgetype,sh_fsidate,sh_hasvalidfsi,sh_nofsiexplanation,sh_hasrefrigeratedvehicle,sh_doesredistributerescuefood,rb_fragencyfoodprogramtype,_sh_account_value,rb_hastruck,rb_istruckdocklevel,rb_istruckrefrigerated,_sh_locowner_value,sh_locationownerexempt;$expand=sh_LocOwner)&$filter=_sh_contact_value eq '9dac648e-aa51-41b5-93b0-ae1556250e59' and statecode eq 0 and sh_frlocation/statecode eq 0
when we try the same query on our dev dynamics environment (just with different ids due to the different data in different environment), it works.
/api/data/v9.0/sh_frlocationcontacts?$select=sh_frlocationcontactid,sh_name,sh_contact,_sh_contact_value,sh_frlocation,_sh_frlocation_value,statecode&$expand=sh_frlocation($select=sh_frlocationid,sh_address_latitude,sh_address_longitude,sh_name,statecode,sh_address_line1,sh_address_line2,sh_address_province,sh_address_city,sh_address_postalcode,rb_fragencytraveldistance,sh_frfreezertype,sh_frfridgetype,sh_fsidate,sh_hasvalidfsi,sh_nofsiexplanation,sh_hasrefrigeratedvehicle,sh_doesredistributerescuefood,rb_fragencyfoodprogramtype,_sh_account_value,rb_hastruck,rb_istruckdocklevel,rb_istruckrefrigerated,_sh_locowner_value,sh_locationownerexempt;$expand=sh_LocOwner)&$filter=_sh_contact_value eq '9dac648e-aa51-41b5-93b0-ae1556250e59' and statecode eq 0 and sh_frlocation/statecode eq 0
When we try the exact same query, but add a select statement to the expand, such as $select=contactid, it works in both environments. __It is only when no columns are specified in the expand statement (which will return all columns) that the error is happening in production. T__he following WORKS in prod. I have bolded the only part that was changed from the query above
/api/data/v9.0/sh_frlocationcontacts?$select=sh_frlocationcontactid,sh_name,sh_contact,_sh_contact_value,sh_frlocation,_sh_frlocation_value,statecode&$expand=sh_frlocation($select=sh_frlocationid,sh_address_latitude,sh_address_longitude,sh_name,statecode,sh_address_line1,sh_address_line2,sh_address_province,sh_address_city,sh_address_postalcode,rb_fragencytraveldistance,sh_frfreezertype,sh_frfridgetype,sh_fsidate,sh_hasvalidfsi,sh_nofsiexplanation,sh_hasrefrigeratedvehicle,sh_doesredistributerescuefood,rb_fragencyfoodprogramtype,sh_account_value,rb_hastruck,rb_istruckdocklevel,rb_istruckrefrigerated,sh_locowner_value,sh_locationownerexempt;$expand=sh_LocOwner($select=contactid))__&$filter=_sh_contact_value eq '9dac648e-aa51-41b5-93b0-ae1556250e59' and statecode eq 0 and sh_frlocation/statecode eq 0)
This indicates to us that it is an issue with D365, likely a schema mismatch or metadata issue, as both environments have identical solutions and all column and tables will match between enviornments.