How to handle ‘A null value was found for the property’ exception
In a couple of Power Automate integration projects I was experiencing some challenges around null value exception handling. In this blog I share how I worked around this type of exception.
A bit of context
In this instance I was trying copy Dynamics 365 Customer Engagement (CE) Account record details to Dynamics 365 Finance and Operations (FO) system. The Power Automate flow is triggered when a Account record gets activated.
The error
One of the challenges can be that certain fields in CE could potentially be empty because they are not mandatory. When I was using those null values and tried to create a record in the target system (FO) I was running into the following error message:
An error has occurred. Exception has been thrown by the target of an invocation. A null value was found for the property named 'OrganizationNumberofEmployees', which has the expected type 'Edm.Decimal[Nullable=False]'. The expected type 'Edm.Decimal[Nullable=False]' does not allow null values
In this case a field called Number of Employees is left empty.
The workaround for this exception
I used this expression as a workaround for this exception.
In this expression I am using four different functions.
1.Data conversion to string
First of all I am using String(), a Conversion function. This is converting the value from the OrganizationNumberofEmployees field of the Get Account Record action into a string.
2.Checking if it’s empty
Next part is the check if the OrganizationNumberofEmployees field is empty. I am using Empty(), a Collection function for this. This function only works with String, Array, or Object.
3.If true use 0
Third part of the expression is If(), a Logical Comparison function. If it is empty I want to apply some logic, in this case I am setting a value of 0. If it’s not empty we also want to apply some logic, in this case I am using the original value of the OrganizationNumberofEmployees field.
4.Convert back into an integer
In the last part of the expression I am using Int(), a Conversion function. This converts the value back into an integer. This makes it possible to copy it into the field in FO, which is also of type integer.