Question T12722
Visible to All Users

Call a function to fetch data from API when other field values are updated

created 2 years ago (modified 2 years ago)

[Ticket cloned from T12717: Set dropdown default value from choice]

Hi Jane,

Is it possible to call a function or something to get the value calculated and get from API??
There is one text box which depends on one dropdown and other text box.
textbox -> thktol
dropdown -> grade
textbox -> width

so thktol value should be calculated on basis of grade and width for which i need to call one api after that.
is it possible?

Answers approved by surveyjs Support

created 2 years ago

Hi Abhishek,
I believe you may handle this using an Expression trigger and a custom function.

The expression trigger is activated when the specified expression evaluates to true.
The runExpression property allows you to specify the expression which would be activated. So, you can register a custom function which would perform an API call if the grade and width field values are not empty.

JavaScript
function getValueFromApi(props){ let grade = props[0]; let width = props[1]; // Call an API to retrieve a thktol value based on grade and width return thktolValue; } Survey.FunctionFactory.Instance.register('getValueFromApi', getValueFromApi); const json = { "logoPosition": "right", "pages": [ { "name": "page1", "elements": [ { "type": "text", "name": "thktol" }, { "type": "dropdown", "name": "grade", "choices": [ "Item 1", "Item 2", "Item 3" ] }, { "type": "text", "name": "width" } ] } ], "triggers": [ { "type": "runexpression", "expression": "{grade} notempty and {width} notempty", "setToName": "thktol", "runExpression": "getValueFromApi({grade}, {width})" } ] }

Example.

Please let me know if this configuration meets your needs.

    Show previous comments (7)

      Hello Abhishek,
      I gave you an example above. Here it is. You did not call this.returnResult callback function as I show in my example above. Here is the updated plunker.

      Thank you,
      Andrew
      SurveyJS Team

        This example works thanks for help Andrew

          You are very welcome!

          Andrew
          SurveyJS Team