jqueryajaxweb-servicesvtigervtigercrm

Vtiger Web Services API error when trying to create Opportunity


I'm attempting to create a javascript handler for a push to a vTiger CRM instance. I've been able to login to the webservice and can query and other function just fine, but I'm hitting a roadblock when I try to create a new 'Opportunity' in the CRM.

Here is the section of code that handles the ajax post:

this.create = function (module, data) {

    var postdata = {
        'operation': 'create',
        'sessionName': sessionName,
        'elementType': module,
        'element': JSON.stringify(data)
    };

    $.ajax({
        url: vTigerRESTurl,
        type: 'POST',
        contentType: 'application/x-www-form-urlencoded',
        crossDomain: true,
        data: postdata,
        success: function (data) {
            console.log(data);
            return data;
        },
        error: function (errorText) {
            console.log("AJAX Error: " + JSON.stringify(errorText));
        }
    });
};

The result of this POST is the following error:

error: code: "MANDATORY_FIELDS_MISSING" message: "potentialname does not have a value" success: false

Given that the element I'm sending in the data is:

potentialname=Test08162020&potential_no=TEST&related_to=PRO+TEST+CO&closingdate=00-00-0000&leadsource=--None--&sales_stage=Prospecting&assigned_user_id=19x1&cf_704=--None--

The error doesn't make any sense to me. Because that required field is supplied to the API.

I've been working under the theory that my encoding is incorrect, but according to the documentation I've been able to find, it seems to be correct.

I've tried a number of different encodings just to be sure, such as $.Param() or doing URI encoding to the JSON string, but both those methods just result in a permissions error that seems to be the default when the API doesn't understand the data sent.

Does anyone have any experience with using the vTiger web services or perhaps have a successful example of how they handled creating records using the web services?

The vTiger instance is version 5.4.

Thank you in advance for any insight you can provide.


Solution

  • So it turns out that when I would use $.Param(data) to encode the data, I would get the Missing mandatory fields message. When I used the the JSON.stringify(data), I would get an access denied message.

    Both these messages were actually very misleading because the actual problem was that I was I was setting the required field related_to=PRO+TEST+CO, when that field actually was looking for a vtiger customer account id that looked like '11x4567'.

    So the fix was to change:

    related_to=PRO+TEST+CO

    to:

    related_to=11x4567

    The way vTiger formats its ids are Module x id, so 11th module (accounts) x account id #.

    Hopefully this helps anyone else running into this problem moving forwards.