0

I have a function that takes a Quickbase recordID and fieldID and deletes any file associated with that field. First, the function:

function deleteFiles(recid,fldid) {

    var apptoken = "xxxxxxxxxxxxxxxxxxxxxxxxx";
    $.ajaxSetup({data: {apptoken: apptoken}});

    var dbid = "xxxxxxxxx";

    var promise = $.post(dbid, {
        act: "API_EditRecord",
        rid: recid,
        _fid_NN: "",
        delfile_fid_NN: "1"
    });

    $.when(promise).then(function(xml){
        console.dirxml(xml);
    });
}

where "NN" needs to equal the fieldID (fldid) being passed. I've tried concatenating the fldid onto those two key names but that didn't work. I've seen a lot of similar questions that suggest creating objects, etc., but those won't work in my situation, either. So the question is, how do I dynamically alter those key names so that QB is happy and the files get deleted? Thanks!

2 Answers2

0

Try the following:

function deleteFiles(recid,fldid) {
    var apptoken = "xxxxxxxxxxxxxxxxxxxxxxxxx";
    $.ajaxSetup({data: {apptoken: apptoken}});

    var dbid = "xxxxxxxxx";
    var payload = {
        act: "API_EditRecord",
        rid: recid
    };

    payload['_fid_' + fldid] = '';
    payload['delfile_fid_' + fldid] = '1';

    var promise = $.post(dbid, payload);

    $.when(promise).then(function(xml){
        console.dirxml(xml);
    });
}
GPicazo
  • 6,516
  • 3
  • 21
  • 24
0

You could also do this using ES6 features as below.

function deleteFiles(recid, fldid) {

  var apptoken = "xxxxxxxxxxxxxxxxxxxxxxxxx";
  $.ajaxSetup({
    data: {
      apptoken: apptoken
    }
  });

  var dbid = "xxxxxxxxx";

  var promise = $.post(dbid, {
    act: "API_EditRecord",
    rid: recid,
    ['_fid_' + fldid]: "",
    ['delfile_fid_' + fldid]: "1"
  });

  $.when(promise).then(function(xml) {
    console.dirxml(xml);
  });
}
Sreekanth
  • 3,110
  • 10
  • 22