You are viewing limited content. For full access, please sign in.

Question

Question

Web API Usage - How to get specific data into a field on a form

asked on April 3, 2024

Hi, 

I am new to using APIs so I am testing using the XKCD comic API so I can learn the skills I will need to use with exchange rates and more relevant data in future.  I am using the Modern Form Designer on Laserfiche Cloud.

I have got data returned from the API into a field on my form, but I have hard coded the value in the URI on the Web Request Rule, I don't know how to specify a value as a parameter that I can pass in from a Laserfiche form, or how to display only a part of the content that is returned (the title).

For example, in this case I would like to be able to:
- Enter the comic id into a single line field in a Laserfiche form
- And have the "title" of the comic to be displayed in a second field (I have the Web Request lookup Rule setup and working on this field, but it currently returns all the content in JSON format) 

This is my current setup:
Web Service Connection:
base URL: https://xkcd.com/
Default HTTP Request Headers: GET (but I have not added any value)

Web Request:

URI: 3/info.0.json (this gets me the comic with the comicId of 3, I would like to parametrise this)
Method: GET
No HTTP request headers have been added

I get the following response:

{"month":"1","num":3,"link":"","year":"2006","news":"","safe_title":"Island (sketch)","transcript":"[[A sketch of an Island]]\n{{Alt:Hello, island}}","alt":"Hello, island","img":"https://imgs.xkcd.com/comics/island_color.jpg","title":"Island (sketch)","day":"1"}

My first single line field would contain the value "3" for the comicId
My second single line field would return only the title "Island (sketch)"

Thank you.

0 0

Answer

SELECTED ANSWER
replied on April 4, 2024

Hi Kevin,

Thanks for your reply, I've used that approach for another scenario, your note about the initiator is useful as I got stuck on that point.  However, in this case I wanted the API calls to be dynamic (to call the API when I selected a value from a drop down on a form). 

I worked it out in the end, I've added my solution below just in case it is helpful to anyone else.

In the web request in Rules I added a token in place of the hard coded value:



This allowed me to specify a dropdown with the comicId param I wanted to pass in from the Lookup rule in my form:



I then used Javascript to parse the JSON string to get just the title and put that into another field:

//parses the XKCD JSON string to extract 
//the Title
let source = {fieldId: 2};
let destination = {fieldId: 9};

//enclose this in "on field change"
LFForm.onFieldChange(
    () => { 
    //Get the json string
    let json_String = LFForm.getFieldValues(source);
    let json_Object = JSON.parse(json_String);
    let selected_Value = json_Object.title;

    //Put the value into the destination field:
    LFForm.setFieldValues(destination, selected_Value);

    }, 
{fieldId: 2});

0 0

Replies

replied on April 3, 2024

I would use a workflow to parse the return from the API and start a Forms business process with those fields set to their respective values. This would create a pre-filled form in the user task. If you needed a form initiator to be the one to get assigned this task, you'd start with an initial form that gathers the data needed for the API call, pass that data to the workflow, then the workflow passes the data back to the Forms business process. To the initiator, it would only appear that they submit the kickoff form and then the filled out form would immediately be displayed. Is that what you had in mind?

0 0
SELECTED ANSWER
replied on April 4, 2024

Hi Kevin,

Thanks for your reply, I've used that approach for another scenario, your note about the initiator is useful as I got stuck on that point.  However, in this case I wanted the API calls to be dynamic (to call the API when I selected a value from a drop down on a form). 

I worked it out in the end, I've added my solution below just in case it is helpful to anyone else.

In the web request in Rules I added a token in place of the hard coded value:



This allowed me to specify a dropdown with the comicId param I wanted to pass in from the Lookup rule in my form:



I then used Javascript to parse the JSON string to get just the title and put that into another field:

//parses the XKCD JSON string to extract 
//the Title
let source = {fieldId: 2};
let destination = {fieldId: 9};

//enclose this in "on field change"
LFForm.onFieldChange(
    () => { 
    //Get the json string
    let json_String = LFForm.getFieldValues(source);
    let json_Object = JSON.parse(json_String);
    let selected_Value = json_Object.title;

    //Put the value into the destination field:
    LFForm.setFieldValues(destination, selected_Value);

    }, 
{fieldId: 2});

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.