cancel
Showing results for 
Search instead for 
Did you mean: 

Using object datatypes

enable_services
Deputy Chef I
Deputy Chef I

Hello,

I am trying to perform a HTTP request with a JSON payload containing a subset of data but I cannot work out how to get this to work. In my scenario and example data structure to build would be as shown below with the "email" data being what I am trying to build.

{
    "first_name": "John",
    "last_name": "Hancock",
    "email": [
        {
            "email_address": "test@test.co.uk",
            "primary_address": true,
        },
        {
            "email_address": "test2@test.co.uk",
            "primary_address": false,
        },
    ]
}

 I have a recipe that is given an email address string so in this example it would be "test@test.co.uk; test2@test.com" which splits by the semicolon. It then needs to build the array data to return to the primary recipe that calls this and that is where I have been having issues. I tried using a list variable which I added to in a loop but it doesn't allow me to select the data, it looks like it assumes I will be wanting to select each row of data.

I realise I probably haven't explained that brilliantly but if anybody can help that would be appreciated.

Many thanks,

Liam

1 ACCEPTED SOLUTION

Oh, i would recomment in the http connector -> body field to be type normal text which would allow you to actually use formula mode and in that formula mode you can directly put the array value 

View solution in original post

9 REPLIES 9

kkamal
Deputy Chef III
Deputy Chef III

IMHO, I have found the solution for such use cases is using javascript connector which takes in the variables to process. You could wrap the logic of getting the JSON in  javascript connector function. That JSON could directly be added as value in the HTML request body formula. 
considering first_name, last_name, emails (; delimited), 
- Consider below as an example to retrieve json from javascript connector. 

exports.main =  (input) => {

const { first_name, last_name, emails} = input

return { first_name, last_name, emails: emails.split(";").map((email, i) => ({ email_address: email ? email.trim(): '', primary_address: i ==0 ? true : false}))}

}

Presumably that would mean I have to update all of my current recipes as I use the SugarCRM app connector and use a custom event action to define my own URL. So all of the logic across the recipes for both my helper functions and other datasets work using the usual mapping setup of selecting datapills. Or can I use the javascript connector for my helper function recipe to return JSON? I had historically tried returning JSON using the .to_json array modifier on the list I built and returning that which seemed to build correctly however in the main recipe where I define the request body fields the only suitable type was text but that resulted in the 'email' data being quoted if I recall meaning it wasn't valid JSON anymore.

e.g.

{
    "email": "[{"email_address":"test@test.com","primary_address":true}]"
}

I don't have that example to hand to confirm it was like that but it wasn't working (I can get the exact example if needed). 

If I understand correctly - you have to transform emails in a specific data structure and in multiple recipes. 

Preferred way would be : 
Utilize callable sync function where  the transformation of emails to json occurs as already described in my previous reply and call that function with input: ; seperated email and outputs desired json. This way you can call it from multiple recipes. 

Yes that is correct so I already had a function recipe setup to do the email transformation that builds an empty list variable and then adds items to it, but that is where  my problems started. In trial and error attempts to get it to work I made the function recipe return the below fields.

  1. "addresses" string type field which is the compiled list converted to JSON using the .to_json method
  2. "emailObj" object type field
  3. "addressesList" array type field

However in the function return step it will not allow me to select my list variable for the object or array type fields. I want to return an array type field of the compiled list of email addresses data so I get it to return an array of data like in my original question for the list of email addresses and then select that returned data in the primary recipe that called the function recipe.

However it doesn't look immediately to me like this is possible which is why I have got stuck as I tried using the "addresses" to_json converted data as a naff workaround into a string field on my primary recipe but then it sends the sub email JSON as a quoted string meaning it isn't valid JSON anymore.

I can do a lookup up of a remote system that returns a primary record will related child records that I can then loop over using the repeat action, this is just the reverse of this that I am trying to do where I am trying to push a data a parent with children to the remote system. If I can't get it to work I will have to just send in the data in an unformatted source string value and then add code into the remote system to handle this special case but I will be disappointed if I have to resort to this.