'An undeclared property' when trying to create record via Web API

后端 未结 2 1422
梦如初夏
梦如初夏 2021-01-18 12:51

I am getting an error which I just cannot seem to debug. I am trying to create a custom activity entity via custom HTML/JavaScript web resource.

The user clicks a b

相关标签:
2条回答
  • 2021-01-18 13:16

    After a good night's sleep I realised my error. To set the value of a lookup field, you need to use the relationship scheme name, and not the property name.

    Once I changed that, all worked fine.

    0 讨论(0)
  • 2021-01-18 13:18

    When you want to set the value of a lookup field during the creation or update of a (new) record via the web API, you have to use either the Schema Name or the Logical Name of the lookup followed by the bind annotation.

    • For default fields like primarycontactid the logical name has to be used (first column in the screenshot).
    • For custom fields like rob_FaqId the schema name has to be used (second column in the screenshot).
    var params = {
        'rob_FaqId@odata.bind': '/rob_faqs(guid-here)',
        'rob_source': 180840000,
        'subject': 'Signpost',
        'actualstart': new Date(),
        'actualend': new Date()
    };
    

    Screenshot of a solution > entities > your entity > fields:

    So the general structure to create a new record with an already set lookup field via the web API is this:

    {
      "logicalorschemaName@odata.bind": "/relatedentitys(guid)" //don't forget the plural 's'
    }
    

    Or another example from the official documentation. How to create a new account record and directly assign an already existing contact as the primary contact.

    var newAccountRecordObj = {
      "name": "Sample Account",
      "primarycontactid@odata.bind": "/contacts(00000000-0000-0000-0000-000000000001)"
    }
    
    0 讨论(0)
提交回复
热议问题