XRM-OSS / Xrm-WebApi-Client

A promise-based JavaScript library for the Microsoft Dynamics CRM WebApi - TypeScript type definitions available
https://xrm-oss.github.io/Xrm-WebApi-Client/
MIT License
100 stars 28 forks source link

Can't create entity with filled lookup attribute #26

Closed jarmoosh closed 6 years ago

jarmoosh commented 6 years ago

Hi,

I want to create an appointment from phonecall entity and I am passing some values on to lookup fields (regardingobjectid, requiredattendees and other custom lookup) and on every each of them I get error:

Error: Bad Request: The property 'sii_is_perdix_kontaktid' does not exist on type 'Microsoft.Dynamics.CRM.appointment'. Make sure to only use property names that are defined by the type.

is this a bug? or I am doing something wrong?

createAppointment: function () {
        debugger;
        var request = {
            entityName: "appointment",
            entity: {
                subject: phonecallRibbonController.returnFieldValue("subject"),
                regardingobjectid: phonecallRibbonController.returnPartyList("regardingobjectid"),
                sii_eventtype: 907000000,
                requiredattendees: phonecallRibbonController.returnContact(),
                sii_is_perdix_kontaktid: phonecallRibbonController.returnFieldValue("sii_perdix_kontaktid")
            },
            headers: [{ key: "Prefer", value: "return=representation" }]
        };

        WebApiClient.Create(request)
            .then(function (response) {
                Xrm.Utility.openEntityForm("appointment", response.id);
            })
            .catch(function (error) {
                alert(error);
            });
    },
    returnFieldValue: function (fieldName) {
        return Xrm.Page.getAttribute(fieldName).getValue();
    },
    returnContact: function () {
        var isIncoming = this.returnFieldValue("directioncode");
        if (!isIncoming) {
            return this.returnFieldValue("to");
        } else {
            return this.returnFieldValue("from");
        }
    },
    returnPartyList: function (field) {

        var fieldProp = phonecallRibbonController.returnFieldValue(field);
        var partlist = new Array();

        partlist[0] = new Object();
        partlist[0].id = fieldProp.id;
        partlist[0].name = fieldProp.name;
        partlist[0].entityType = fieldProp.entityType;

        return partlist;
    }
DigitalFlow commented 6 years ago

Hey @jarmoosh,

please go to Settings > Customizations > Developer Resources and choose to download OData Metadata. You'll get an XML file showing you all the appropriate field names on each entity.

As for your original question: For the Web API there is no such field as regardingobjectid, as for multi entity lookups, such as objectid and regardingobjectid, you have to suffix the field name with _entityName, so if you want to connect your phone call to an account, you'll have to name the property regardingobjectid_account. You'll also find this field name inside the OData Metadata if you take a closer look.

Please give me a note, once you have further questions or did it get to work.

Kind Regards, Florian

DigitalFlow commented 6 years ago

Hey @jarmoosh,

I updated the ReadMe to include a FAQ, which should help you fixing your issues, you can find it here. Please give me a note, whether you need any further help. I'll close this issue for now.

Kind Regards, Florian