locked
Trying to use the API to submit a post to an account RRS feed

  • Question

  • Hi,

    I'm new at using the API, but I have been able to read data from the API and also post a Task via the API!

    However now I want to submit a Post to a specific account via the API! But I get an Error: error BAD request!

    Any ides why this is!

    This is the code I use:

    var entity = {};
    entity.source = 3;
    entity.text = "API test";
    entity.type = 3;
    entity["regardingobjectid@odata.bind"] = "/(0bbb28aa-09c9-e611-80bf-00155d649604)";
    
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/posts",
        data: JSON.stringify(entity),
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
            XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        async: true,
        success: function(data, textStatus, xhr) {
            var uri = xhr.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            var newEntityId = matches[1];
        },
        error: function(xhr, textStatus, errorThrown) {
            Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
        }
    });


    I'm a bit unsure on what the put on the source and type entity??

    thanks for any help

    //Martin

    Tuesday, January 10, 2017 4:15 PM

All replies

  • Since regarding can handle multiple types you need to specify 

    entity["regardingobjectid_account@odata.bind"] = "/accounts(22FAECC2-43C8-E611-8102-C4346BAC4B68)";


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Tuesday, January 10, 2017 4:54 PM
    Moderator
  • Hi Jason,

    thanks for the reply!

    I changed the code to the one you have!

    var entity = {};
    entity["regardingobjectid_account@odata.bind"] = "/accounts(22FAECC2-43C8-E611-8102-C4346BAC4B68)";
    entity.source = 3;
    entity.text = "API Post Test";
    entity.type = 3;
    
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/posts",
        data: JSON.stringify(entity),
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
            XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        async: true,
        success: function(data, textStatus, xhr) {
            var uri = xhr.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            var newEntityId = matches[1];
        },
        error: function(xhr, textStatus, errorThrown) {
            Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
        }
    });

    however I still get the same error message! I wonder if it has something to do with the source and type entity?

    regards

    Martin

    Wednesday, January 11, 2017 8:03 AM