# Update field using PreProcess scripts

**URL:** <https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025>\
**Category:** Server-Side Scripting\
**Created:** [January 12, 2016, 3:47pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025 "2016-01-12T15:47:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_2012](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/matt_2012/32/420_2.png) [@Matt\_2012](https://community.dreamfactory.com/u/Matt_2012)\
**Post date:** [January 12, 2016, 3:47pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/1 "2016-01-12T15:47:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andersonmuller](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@andersonmuller](https://community.dreamfactory.com/u/andersonmuller)\
**Post date:** [January 12, 2016, 8:33pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/2 "2016-01-12T20:33:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![toddappleton](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/toddappleton/32/159_2.png) [@toddappleton](https://community.dreamfactory.com/u/toddappleton)\
**Post date:** [January 12, 2016, 8:50pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/3 "2016-01-12T20:50:08Z")

</div>

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

[https://wiki.dreamfactory.com/DreamFactory/Tutorials/Posting\_records](https://wiki.dreamfactory.com/DreamFactory/Tutorials/Posting_records)

---

<div class="post-metadata">

**Author:** ![Matt\_2012](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/matt_2012/32/420_2.png) [@Matt\_2012](https://community.dreamfactory.com/u/Matt_2012)\
**Post date:** [January 12, 2016, 10:40pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/4 "2016-01-12T22:40:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![toddappleton](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/toddappleton/32/159_2.png) [@toddappleton](https://community.dreamfactory.com/u/toddappleton)\
**Post date:** [January 13, 2016, 2:42pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/5 "2016-01-13T14:42:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matt\_2012](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/matt_2012/32/420_2.png) [@Matt\_2012](https://community.dreamfactory.com/u/Matt_2012)\
**Post date:** [January 13, 2016, 3:06pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/6 "2016-01-13T15:06:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![toddappleton](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/toddappleton/32/159_2.png) [@toddappleton](https://community.dreamfactory.com/u/toddappleton)\
**Post date:** [January 13, 2016, 5:14pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/7 "2016-01-13T17:14:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmontes](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jmontes](https://community.dreamfactory.com/u/jmontes)\
**Post date:** [January 27, 2016, 10:20pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/8 "2016-01-27T22:20:26Z")

</div>

Any date, for this issue ?

I have the same with php script

---

<div class="post-metadata">

**Author:** ![Anil\_Thakkar](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@Anil\_Thakkar](https://community.dreamfactory.com/u/Anil_Thakkar)\
**Post date:** [March 9, 2018, 11:58am UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/9 "2018-03-09T11:58:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ved\_Prakash](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/ved_prakash/32/1019_2.png) [@Ved\_Prakash](https://community.dreamfactory.com/u/Ved_Prakash)\
**Post date:** [June 6, 2018, 5:24am UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/10 "2018-06-06T05:24:55Z")

</div>

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

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

4. Put in all of the above in **Prescript**

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

---

<div class="post-metadata">

**Author:** ![jsolbrig](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jsolbrig](https://community.dreamfactory.com/u/jsolbrig)\
**Post date:** [October 10, 2018, 9:38pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/11 "2018-10-10T21:38:38Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![jsolbrig](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jsolbrig](https://community.dreamfactory.com/u/jsolbrig)\
**Post date:** [October 11, 2018, 5:13pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/12 "2018-10-11T17:13:05Z")

</div>

Here is an example script that works in Python: [https://pastebin.com/u4qsWAub](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.

---

<div class="post-metadata">

**Author:** ![luca](https://avatars.discourse-cdn.com/v4/letter/l/51bf81/32.png) [@luca](https://community.dreamfactory.com/u/luca)\
**Post date:** [January 2, 2019, 5:18pm UTC](https://community.dreamfactory.com/t/update-field-using-preprocess-scripts/2025/13 "2019-01-02T17:18:31Z")

</div>

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 ?
