Platform.api.patch

Hello,

I’m struggling with getting my platform.api.patch script working. When trying it in API docs, it’s working:

I’m using: updateRecordsByFilter() - Update (patch) one or more records
Table_name = Status
body = {“Status_status”:“new”,“Status_approver”:“Johny”}
filter = Status_TT = ‘12345’

With this it should update the items with Status_TT = 12345 with the information in the body. As i said, it works.

When trying to make this server side, i’ve tried many things and the closest i get is:

var queryupdate = '{"Status_status":"new","Status_approver":"Johny"}';
var string3 = JSON.parse(queryupdate);
platform.api.patch("db/status?filter=Status_TT = '12345' ", string3);

The problem is that it updates all the records in that table, including the ones thatdon’t have 12345 under Status_TT

I’ve also tried to do something with

platform.api.patch("db/status", string3, {"filter" : "Status_TT = '12345' "} );

but that doesn’t work at all

It’s probably something very simple and would appreciate if someone could help me with some code examples, i can work out the rest how to apply it to my project

Thanks

Someone must be using this :)?

Maybe try URL-encoding the filter like API Docs does for you.

Hi!

I’m trying to do the same and get stuck: when i do the patch through APIDocs, it work well, but when i try patch inside a server-side script, it returns:

{“error”:{“code”:400,“context”:null,“message”:“The request contains no valid record fields.”,“status_code”:400}}

I set a var_dump of the request on the pre-patch event and when i run the patch from the APIDocs, it shows the payload, but when i run from the script, it shows nothing inside the payload… what should be happening?

Any thoughs? thanks in advance.

What works for me is the following when I do server side scripting

var updatesomething = platform.api.patch(“mysql/_table/profile?filter=Profile_id%20%3D%20” + profile_id, {“resource”:[{“Profile_LastUpdate”: newdate}]});

Where profile_id and data are variables containing some information

Hi, when i use what u wrote (assuming the method is: platform.api.patch(url, payload)) it returns:
{"error":{"message":"callback is not a function","code":500}}. Is there any other thing to consider?

Thanks for ur advice,

Are you using a customer scripting service?

Many things can cause that error. Maybe it’s what you send client side, the arguments, that are not understood server side?

Would need to see more code to see what you’re doing to troubleshoot it. What are you using client side to send the request to the server?

im pasting here all the code inside the method:

var url, result, options, tfa, payload;

options = {
    'headers': {
        'X-DreamFactory-Api-Key': platform.session.api_key,
        'X-DreamFactory-Session-Token': platform.session.session_token,
        'Content-Type': 'application/json'
    }
};
 
// update a record using internal URL
url = 'bd/_table/users/{user.id}';
tfa = {
    secret: '',
    nonsecret: ''
};
payload = {
    "tfa" : JSON.stringify(tfa)
};

console.log('paso 4 - url: ' + url);
console.log('paso 4 - payload to be saved on BD: ' + JSON.stringify(payload));
platform.api.patch(url, payload, null, function(body, response) {
    result = JSON.parse(body);
    console.log('paso 4 - BD answer: ' + body);

});

event.setResponse({'result': 'ok'}, 200, 'application/json');

This is what prints on the log:

Thanks for any advice

Ok, just to be clear how we did this.

First of all we only use POST type of requests that trigger a server side script. We never PATCH the DB directly. If that is what you’re doing, meaning using the API directly to patch… I would need to test if that works as well.

Second, if you would be OK to do a POST to a server side custom script and inside that server side script you patch what you need to patch it works as I suggested, at least for us.

$.ajax({
type: ‘POST’,

    beforeSend: function(request) {
        request.setRequestHeader('X-DreamFactory-Api-Key', AP_id);
        request.setRequestHeader('X-DreamFactory-Session-Token', UT);
        request.setRequestHeader('Access-Control-Allow-Origin', '*');
    }, 

    url: AP_api + 'UpdateUserDetails?n1=' + variable1+ '&n2=' + variable2 + '&n3=' + variable3,
    dataType: "TEXT", 
    contentType: "JSON",

    success: function(response) {
        console.log(response);
    },

    error: function(xhr, thrownError) { 
        console.log(thrownError); 
    }
});

On the server side we do the patch. Seems a bit of an overkill maybe but since we patch mutliple tables / records this is the easiest

oh, ur code is about the calling from the client, right? the code i posted is from de server side script. are we talking about the same? thanks for the reply.