Skip to main content

How can I add all the information of the content selected in the relational data selection to the response?

You can create a custom process to retrieve the information of the corresponding content from the ID of the relational data in the response, and add it to the response of endpoint by setting it as a post-processing of the endpoint.

Set up custom function

Create a function with the following code on custom function editor page.

info

Below code assumes that you set relational data selection to ext_1 item.
Change it according to your own settings.

If module_type is topics

{*  Endpoint settings parameter *}
{assign_array var='method_params' values=''}
{assign var='method_params.topics_id' value=$json.details.ext_1.module_id}
{api_method
var='response'
model='Topics'
method='details'
version='1'
method_params=$method_params}

{append var='json.details.ext_1' value=$response.details index='details'}
{assign var='processed_json' value=$json}

If module_type is member

{*  Endpoint settings parameter *}
{assign_array var='method_params' values=''}
{assign var='method_params.member_id' value=$json.details.ext_1.module_id}
{api_method
var='response'
model='Member'
method='details'
version='1'
method_params=$method_params}

{append var='json.details.ext_1' value=$response.details index='details'}
{assign var='processed_json' value=$json}

If module_type is form

{*  Endpoint settings parameter *}
{assign_array var='method_params' values=''}
{assign var='method_params.inquiry_id' value=$json.details.ext_1.module_id}
{api_method
var='response'
model='InquiryForm'
method='details'
version='1'
method_params=$method_params}

{append var='json.details.ext_1' value=$response.details index='details'}
{assign var='processed_json' value=$json}

Set to the post-processing of endpoint

Set the custom function you created to the desired endpoint's post-processing.

Image from Gyazo

Operation check

If you check the endpoint response in Swagger UI, you can see that the details of the related information linked with the item name details are added under ext_1 as shown below.

Image from Gyazo

In the case of the list endpoint

The code example for the Topics::list endpoint would be as follows.

{assign_array var='empty_array' values=""}
{assign var='empty_object' value=$empty_array|@to_object}

{foreach from=$json.list key=key item=details}
{assign var='topics_id' value=$details.ext_1.module_id}
{assign var='module_type' value=$details.ext_1.module_type}
{assign var='details.ext_1' value=$empty_object}
{if $topics_id && $module_type == "topics"}
{* Endpoint settings parameter *}
{assign_array var='method_params' values=''}
{assign var='method_params.topics_id' value=$topics_id}
{api_method
var='response'
model='Topics'
method='details'
version='1'
method_params=$method_params}
{append var='details' value=$response.details index='ext_1'}
{/if}
{assign_array_set var="json.list" key=$key value=$details from=$json.list}
{/foreach}


{assign var='processed_json' value=$json}

Support

If you have any other questions, please contact us or check out Our Slack Community.