cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Helper III
Helper III

How to build HTTP request that builds an array in the body from Dynamic content

Hi all,

 

So I'm building a Flow below:

 

High level view of the flowHigh level view of the flow

 

The first step after the trigger is a list record CDS step. I'd be getting an array of contact details from the CRM.

 

The last step is a HTTP POST request to an API endpoint that accepts a batch of contacts in objects within an array.

 

image.png

 

 

The above sample JSON is below:

 

{
  "contacts": [
    {
      "FirstName": "Slarty",
      "LastName": "Bartfast",
      "Email": "test@slarty.com"
    },
    {
      "FirstName": "Jerry",
      "LastName": "Seinfeld",
      "Email": "jerry@seinfeld.com"
    },
    {
      "FirstName": "Elaine",
      "LastName": "Benes",
      "testField": "testttsdfsdfsdfsdf",
      "Email": "elaine@seinfeld.com"
    }
  ]
}

 

 

I've got two questions.

 

1. How do I first take select fields from the Contact entity in CDS for example first name, last name, email and maybe a test field and concatenate them together into something like the  sample body above? The number of objects within the array will vary depending on how many contacts are returned in the list records step. So I'd need to perform essentially an apply to each on the returned contacts from CDS and concatenate some select fields into the array object required to process the contacts into another system via the API.

2. Second question is that note with the Elaine Benes object that I have an additional field value called "testField". What will happen is that some returned objects will have values that need to be passed and therefore will have a key value pair and some contacts won't and therefore there shouldn't be a key value pair.

 

Any help would be greatly appreciated.

 

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @Mick282, 

 

So the issue is some additional key value pair will make the HTTP request failed, right?

You could use the removeProperty() function to remove the additional key value pair when you append it into the array variable, for example:

removeProperty(item(),'testField')

Annotation 2020-04-16 154845.png

Best Regards,
Community Support Team _ Lin Tu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Helper III
Helper III

Ok so I've figured out how to create the array.

 

I'd initialize an array variable and then append to the array variable an object for each contact.

 

image.png

 

I can then add that whole variable to the HTTP step:

 

image.png

The question still remains. I do I construct a dynamic object for each item in the array knowing at times I might have additional key value pairs for certain fields for a specific contact and then at other times I don't have any and therefore that key value pair need not even be in the object itself. 

 

You can see even in my append array step, that I've hardcoded the key value part of the JSON and only added dynamic content for the value area.

Hi @Mick282, 

 

So the issue is some additional key value pair will make the HTTP request failed, right?

You could use the removeProperty() function to remove the additional key value pair when you append it into the array variable, for example:

removeProperty(item(),'testField')

Annotation 2020-04-16 154845.png

Best Regards,
Community Support Team _ Lin Tu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

@Mick282 

 

" I construct a dynamic object for each item in the array knowing at times I might have additional key value pairs for certain fields for a specific contact and then at other times I don't have any and therefore that key value pair need not even be in the object itself. "

 

So, your apend to array value will probably try to add all possible attributes to the new element appended to your array (let's call it intermediateArray). Does it crash when the source CDS record does not have one of such attributes defined? If so, I believe you can assign as the missing attribute value a coalesce() based expression, setting a string default value ('MISSING')

 

Now, following @v-litu-msft advise, as you iterate all through your intermediate array, you probably need to implement a check one by one of all the possible properties, if its value is Missing, then use removeProperty() based expression to you append current element to your outputArray

 

Hope this helps



Each time you click on any of our inspiring answers 'Thumb up' icon...
...an ewok scapes from the stormtroopers.

Be grateful, Thumbs up! Save the Galaxy for free!


Escribo sobre Power Automate en:
https://medium.com/anyone-can-automate/

Proud to be a Flownaut!



Community Support
Community Support

Hi @Mick282,

 

Has your problem been solved?
If your problem has been solved, You could go ahead and mark the post as solved by clicking “Accept as Solution” so that this thread will be marked for other users to easily identify!

Best Regards,
Community Support Team _ Lin Tu

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

MPA Community Blog

Power Automate Community Blog

Check out the community blog page where you can find valuable learning material from community and product team members!

Top Solution Authors
Top Kudoed Authors
Users online (9,689)