web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Allow to pass null for a parameter in a custom built web service in Dynamics 365 (AIF)

(0) ShareShare
ReportReport
Posted on by 245

Hi DAXers,

I recently came across this issue where I built a custom API in Dynamics 365, that accepts a list as one of the parameters, the list is used to drop values and update them in a table. The problem is one of the business requirements is that if the list is null no update should happen, whereas if we receive an empty list, all records should be dropped.

The request looks like this : 

{
    "dataAreaId""xxxx",
    "parameter1""*****",
    "elements1": [
        {
            "elementParameter1""AAAA",
            "elementParameter2""123456789",
            "elementParameter3""BBB",
            "elementParameter4""Yes"
        },
        {
            "elementParameter1""BBBB",
            "elementParameter2""987654321",
            "elementParameter3""CCC",
            "elementParameter4""No"
        }
    ,
    "elements2": {
        "elementParameter5""Bold",
        "elementParameter6"15
    }
}
In order to drop all records in the table, by sending a empty list the request looks like this : 
{
    "dataAreaId""xxxx",
    "parameter1""*****",
    "elements1": [,
    "elements2": {
        "elementParameter5""Bold",
        "elementParameter6"15
    }
}
In order to NOT UPDATE (and not drop) the records the query then looks like this :
{
    "dataAreaId""xxxx",
    "parameter1""*****",
    "elements1"null,
    "elements2": {
        "elementParameter5""Bold",
        "elementParameter6"15
    }
}
Ideally, we'd even want the parameter to be optional, so we can also pass a request like this (and have elements1 considered as null) : 
{
    "dataAreaId""xxxx",
    "parameter1""*****",
    "elements2": {
        "elementParameter5""Bold",
        "elementParameter6"15
    }
}
The custom built service method looks like this : 
[AifCollectionTypeAttribute("elements1", Types::Class, classStr(SLRClassElements1))]
    public FDXItemMaintenanceResultMessage createOrUpdateItem(DataAreaId dataAreaId,
                                   str parameter1 = "",                       
                                   List elements1 = null,                               
                                   SLRClassElements2 element2 = null) 
    {
        Table                     table;
        
        ttsBegin;
        
        //some code for updates...
        
        if(elements1 != null)
        {
            delete_from table where table.Id == parameter1
            
            ListIterator it = new ListIterator(elements1);
            
                while (itB.more())
                {
                    table.clear();

                    SLRClassElements1 element1      = it.value();
                        
                    table.initValue();
                    table.field1    = element1.elementParameter1();
                    table.field2    = element1.elementParameter2();
                    table.field3    = element1.elementParameter3();
                    table.field4    = element1.elementParameter4();
                    
                    if (table.validateWrite())
                    {
                        table.update();
                    }

                    it.next();
                }
                
            //some other code for updates...
            
        }
        
        ttsCommit
        
    }
Unfortunately when sending this request we get the following error : 
{
    "Message""An exception occured when deserializing a parameters - Exception occurred when parsing and deserializing parameter 'elements1' - 'Object reference not set to an instance of an object.'",
    "ExceptionType""XppServicesDeserializationException",
    "ActivityId""a6578e54-de99-0004-7ec2-5fa699ded601"
}
Also when we omit the parameter "element1":
{
    "dataAreaId""xxxx",
    "parameter1""*****",
    "elements2": {
        "elementParameter5""Bold",
        "elementParameter6"15
    }
}
even though it's set as "element1 = null" we get this error : 
{
    "Message""An exception occured when deserializing a parameters - Exception occurred when parsing and deserializing parameter 'elements1' - 'Parameter 'elements1' is not found within the request content body.'",
    "ExceptionType""XppServicesDeserializationException",
    "ActivityId""a6578e54-de99-0005-fe3e-5ca699ded601"
}
How can we allow a parameter to accept null as a value, or have the parameter optional, in a custom built API in Dynamics 365 ?
Thanks!
I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 171 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 83

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans