Update field using PreProcess scripts

I want to modify a field in a preprocess script but it is not working.

The script is

if (event.request.payload.resource) {

    lodash._.each (event.request.payload.resource, function( record ) {
        
    record.snapTypeID = 2;  

However that is never run because
event.request.payload.resource is null.

when i log:

var_dump(event.request.payload);
var_dump(event.request.payload.resource);

I get for (event.request.payload):

object(Object)#2016481607 (2) {
[“title”] =>
string(4) “ssss”
[“description”] =>
NULL
}

and for (event.request.payload.resource):

NULL

1 Like

Hi @Matt_2012,

Try this way:

//check if u have a payload
if (event.request.payload) {
    //check if u have the key snapTypeID
    if (!event.request.payload.snapTypeID) {
        throw 'snapTypeID cannot be empty';
    } else {
        //we have the key, so change it
        event.request.payload.snapTypeID = 2;
    }
}

I hope it helps

If you’re trying to create records, your payload should contain an array named ‘resource’.

https://wiki.dreamfactory.com/DreamFactory/Tutorials/Posting_records

Hi should the resource be where I update the data ?

I am getting closer but still not able to modify (change the title) in the record before it is added to the table.

I now have a resource array in the payload

dump_var(event.request.payload.resource)

array(1) {
  [0] =>
  object(Object)#220134100 (2) {
    ["title"] =>
    string(5) "Alice"
    ["description"] =>
    string(5) "Jones"
  }
}

but I cant update the record in the script.

if (event.request.payload.resource) {

    lodash._.each (event.request.payload.resource, function( record ) {
        var_dump(record);
        record.title = 'not alice';
        record['title'] = 'not alice 2';
        var_dump(record);
    });
}

event.request.payload.resource[0].title = 'not alice 3';
var_dump(event.request.payload.resource);
event.request.payload.resource[0]['title'] = 'not alice 4';
event.request.payload['title'] = 'not alice 5';
var_dump(event.request.payload.resource);

payload and resource are being changed but alice is still being submitted into the database.

Make sure you are editing the correct script depending on POST or PATCH, and that the scripts are marked as Active.

For example,

db._table.contact.post.pre_process

vs

db._table.contact.patch.pre_process

Hi Todd

Script definitely active and I am using post to add a new record.
It wouldn’t be appearing in my logs otherwise.

I can modify the resource object but not what gets added to mysql table.

What would be very helpful here is a working example where data is modified at the preprocess stage.

Many Thanks

Matt

I confirmed there is an issue here. I’m working with the dev team to resolve and will report back here with updates.

Any date, for this issue ?

I have the same with php script

1 Like

Hi There,

I am still getting similar kind of issue. I am unable to update resource from event. below here is my event and code within event. I can validate the value but when I update the value of it, its not reflecting in db.

Event: mysql mysql._table.{table_name} mysql._table.{table_name}.post.pre_process mysql._table.employee.post.pre_process

Code:
console.log(event.request); // outputs to file in storage/log of dreamfactory install directory

// use NPM to install lodash
event.request.content_changed = true;
var lodash = require(“lodash”);

event.request.payload.resource[0].salary = 90;

Note: I have enabled both check boxes “Active” and “Allow script to modify request payload”. I am sure that Event is triggering because I can validate the data and throw errors.

To update the Payload here are things to do

  1. Tick Allow script to modify request payload to True.

  2. Modify your data something like this

    $event['request']['payload']['resource'][0]['fullname']=$firstname." ".$lastname;

    There are few instances of data which doesn’t contain resource as array, specially system/user based api so you can go like

    $event['request']['payload']['resource']['fullname']=$firstname." ".$lastname;

  3. The most important step which actually modifies the payload content.

    $event['request']['content']=json_encode($event['request']['payload']);

  4. Put in all of the above in Prescript

Done. Can help who are still looking for answer. Cheers.

5 Likes

Sorry to resurrect this topic, but this process is not at all clear from the documentation, especially for Python scripting. It would be very helpful to add this information to the documentation and provide some examples of how to do this in the example scripts. Maybe I can help?

From reading the documents, I guessed that I needed to update event.request.payload.resource[0], but that has no effect on the payload that is actually inserted into the database. Instead, I need to modify the payload, then do event.request.content = json.dumps(event.request.payload).

Is there a reason that this isn’t an automatic process? When payload is updated and “Allow script to modify request payload” is set to True, shouldn’t content automatically update to match payload?

Here is an example script that works in Python: https://pastebin.com/u4qsWAub

This script is used as a pre-post request script. It takes the input document, looks for “start”, “end”, “start_datetime”, and “end_datetime” and creates any that don’t exist based on the ones that do. So, if “start” exists as a timestamp, “start_datetime” is created based on that timestamp.

2 Likes

Hi I’m struggling for some weeks against a post_request pre process script that modifies my payload and I found this post that helps me. The dreamfactory documentation is not so clear for me…
If I make a post request with the following payload without any script activated it works great and all the fields are inserted into my sql database:

{"resource": [{"Time":"2018-12-21T07:49:23","Gas":"111111","Pressure":"1111","Temperature":"11111"}]}

But I’m unable to modify only field “Gas” before to insert data into database.
I made a script triggered as pre_process with post request like this:

<?php

$event['request']['payload']['resource'][3]['Gas']="00000";
$event['request']['content']=json_encode($event['request']['payload']);

?>

And I receive this error:

{"error":{"code":1000,"context":{"error":["Gas"],"resource":{"0":{"messageID":32},"Gas":{"code":500,"message":"array_merge(): Argument #1 is not an array"}}},"message":"Batch Error: Not all requested records could be created.","status_code":500}}

Please could you help me to solve the problem and fix my script? How can I index the resource field that I need to modify in my payload ?