API_setfieldproperty not setting drop down options

  • 0
  • 1
  • Question
  • Updated 4 years ago
  • Answered

My code as below.

It runs but fields do not get drop down options and seeing alert saying that non-complete setup.


function UpdateDrpDwnField(toTbl, targetField, lbtxt, txtOptions, tkn) {

    ChangeTypeToMultipleChoice(toTbl,targetField,tkn);

$.get(toTbl, {

        act: "API_SetFieldProperties",

        apptoken: tkn,

allow_new_choices:0,

        fid: targetField, // it is optional, should be careful

        label: lbtxt,

        choices: txtOptions.split(",").join("\n")

    });

Photo of Kp

Kp

  • 14 Points

Posted 4 years ago

  • 0
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
You have to call API_FieldAddChoices  repeatedly setting one new option at a time. Likewise you have to call API_FieldRemoveChoices repeatedly to remove existing choices. You could use API_SetFieldProperties to set field choices in one call (along with other field properties) but you would then have to create your post body as an XML formatted string as there is no way I know of to set multiple choices using the attribute/value format of of a GET or POST. Specifically this does not work:

choices: txtOptions.split(",").join("\n")

Also, if you are using an AJAX call withing a function which passes parameters such as {toTbl, targetField, lbtxt, txtOptions} you should return a promise from the function

var apptoken = "<your apptoken>";
$.ajaxSetup({data: {apptoken: apptoken}});

function UpdateDrpDwnField(toTbl, targetField, lbtxt, txtOptions) {
  return $.get(toTbl, {
    act: "API_SetFieldProperties",
    allow_new_choices: 0,
    fid: targetField,
    label: lbtxt
  });
}

UpdateDrpDwnField(<arguments>).then(function(xml) {
  //process xml
});

If you return a promise from UpdateDrpDwnField the value returned will be "thenable" (yes it is a real word) as indicated by the last three lines in the above code fragment.

Finally, don't include the application token as an explicit parameter to the AJAX call - rather set it up once through $.ajaxSetup(...) and jQuery will automatically as it to the AJAX calls.
Photo of Kp

Kp

  • 14 Points
Thank you so much. I always like your post and they help me a lot.
Good to know that it has to be done in this way. And surprise that the choices parameter does not work by nature.
Photo of Kp

Kp

  • 14 Points
Updated my codes with your suggestion.
Build the xml body, but it only updates the label, not the choices. Please help.

    var targetField="1576";
var lbtext="Sec-I-1.hdr.2 Check Type:123";

$.ajaxSetup({data: {apptoken: tkn}});

var tmc = txtOptions.replace(/,/g,"</choice><choice>") ;

var  request  ="";
request +="<qdbapi>";
//request +="<fid>'+targetField+'</fid>";
request += "<allow_new_choices>0</allow_new_choices>" ;
request += "<label>Sec-I-1.hdr.2 Check Type:</label>";
request +="<choices>" ;
request +="  <choice>test option</choice>";
        request +="</choices>";
request += "</qdbapi>";

console.log("req:   "+  request);

url="domain?a=API_SetFieldProperties&fid=1576&apptoken="+tkn;

$.ajax({
  type: "POST",
contentType: "text/xml",
  async: false,
  url: url,
  dataType: "xml",
  pro