Search code examples
javarestapisugarcrm

SugarCRM custom field


I'am writing a software for the data-synchronization of a custom software and sugarCRM. Therefore I need an updateOrCreate() function. My Problem is, that the custom software uses other uuid´s than sugarCRM so i can´t look for the uuid to check on update or create.So I want to save the custom-uuid in a custom field of sugarCRM. But i have no idea how to do that over the REST-API of sugarCRM. By the way I wrote a java-application.

Thank you for help!


Solution

  • As far as I'm aware there is no update-or-create API (see https://your-sugarsite/rest/v10/help), howewer if you just want to use the API (rather than customize it) you could sync data like this:

    1) Fetch all ids of records that have a custom uuid by using the POST /rest/v10/<module>/filter endpoint and a payload similar to:

    {
        offset: 0,
        max_num: 1000,
        fields: ["id", "custom_uuid_c"],
        filter: [{"custom_uuid_c": {"$not_empty": ""}}],
        ]
    }
    

    or if you just need a specific custom uuid at a time:

    {
        offset: 0,
        max_num: 1000,
        fields: ["id"],
        filter: [{"custom_uuid_c": {"$equals": "example-custom-uuid"}}],
        ]
    }
    

    The response will look something like this:

    {
        next_offset: -1,
        records: [
            {"id": "example-sugar-uuid", "custom_uuid_c": "example-custom-uuid"},
            ...
        ],
    }
    

    Notes:

    • Make sure to evaluate next_offset as even with a high max_num you may not get all records at once because of server limits. As long as next_offset isn't -1 you should use its value as offset in a new request to get the remaining records.
    • You can supply all field names you need to sync in the fields array, so that you get that information early and can check whether or not an update is required at all (maybe data is still up-to-date?)
    • Sugar also always include certain fields in the response, no matter if they were requested or not. (E.g. id and date_modified). I did not include them all in the response snippets for the sake of simplicity.

    2)

    Based on the information received in the previous step you know which sugar ID belongs to which custom UUID and you can detect/prepare data for updates.

    If you need to sync all and retrieve the complete list first, I suggest you create a lookup table custom-uuid => sugar-id, so that you do not have to loop through the data array and compare fields when looking for a specific number.

    Don't forget to consider the possibility of a custom-uuid being present in one than more Sugar-record at a time, unless you enforce them being unique on the server/database side.

    3)

    Now that you have all the information you need you can update and create records as needed:

    • Update existing record: PUT /rest/v10/<module>/<record_id>

    • Create missing record: POST /rest/v10/<module>

    If want to send a lot of creates and/or updates in a single request, have a look at the POST /rest/v10/bulk API - if your version of Sugar has it.

    Final notes:

    • The filter operators definition on /rest/v10/help seems incomplete, for more info you can check the filter docs