Search code examples
trello

Trello API: how to populate a Checklist?


I successfully created the card and the checklist but i'm unable to populate the newly created checklist with the needed items. Trello does respond to the POST with 400 "invalid value for name" I'm getting crazy trying to find what's wrong in the following code. Is there anyone that can Help?

function CreateChecklist(card) { // this work fine
    var newList = 
        {
        idCard:card.id,
        name:"Cicli di lavorazione",
        pos:"top"
        }
    Trello.post('/checklists/', newList, CreateCheckListItems, t_error);
}


function CreateCheckListItems(checkList) { // 400 'invalid value for name'
    console.log("Checklist created (id="+checkList.id+"):");
    console.dir(checkList);
    //var newItem = "TESTNAME"; // WRONG version
    var newItem = {name:"TESTNAME"}; // SOLUTION! Thanks Casey
    Trello.post('/checklists/'+checkList.id+'/checkItems/', newItem, null, t_error);    
}

Here is the full code with sample data. The scope is to create cards in a specific Trello list. In every card there is a checklist with the production cicles (please change Trello client key and LISTAid to use the code)

<html>
<head>
    <link rel="stylesheet" type="text/css" href="creaSchede.css">
    <script src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="https://trello.com/power-ups/power-up.min.js"></script>
    <script src="https://api.trello.com/1/client.js?key=XXXXXXX-YOUR-KEY-XXXXXXXX"></script>
</head>
<body>
    <script type="text/javascript">//<![CDATA[
$(window).load(function(){

var ordini;
var LISTAid = "5739a2b4c5dc9d398b1c7143"; // <<<---------- PUT HERE YOUR LIST ID
var scheda = {};
var orp = {};
var fasi = {};


// TRELLO success on card creation
var tcard_success = function(successMsg) {
    scheda = {};
    scheda = successMsg;
    console.log("Creata scheda: "+scheda.url);
    console.dir(scheda);
    $("#output").append(function(n){
        return "<a class='board' id='"+ scheda.id +"'target='trello' href='" + scheda.url + "'>" + scheda.name + "</a>";
    });
    CreateChecklist(scheda);
};

// TRELLO error (generale)
var t_error = function(errorMsg) {
  console.log(errorMsg.responseText);
  console.log("Error msg data:")
  console.dir(errorMsg);
};


var onAuthorize = function() {
    updateLoggedIn();
    $("#output").empty();
    Trello.members.get("me", function(member){
        $("#fullName").text(member.fullName);
    });
};

var updateLoggedIn = function() {
    var isLoggedIn = Trello.authorized();
    $("#loggedout").toggle(!isLoggedIn);
    $("#loggedin").toggle(isLoggedIn);        
};

var logout = function() {
    Trello.deauthorize();
    updateLoggedIn();
};


var doCreate = function() {
    var dataFinePrevista = new Date();
    var el = document.getElementById("dataText");
    ordini = JSON.parse(el.value);
    console.dir(ordini);

    $("#form").hide();

    for (var i = 0; i<ordini.length; i++) {
        orp = ordini[i];
        fasi = orp.FASI;
        var orpTipo = (orp.flag_matricola == "1") ? "M" : (orp.flag_lotto == "1") ? "L" : "A";
        if (orp.data_fine_prevista!="") {
            var arrData = orp.data_fine_prevista.split(".");
            dataFinePrevista.setFullYear(arrData[2],arrData[1]-1,arrData[0]);
        } else {
            dataFinePrevista = null;
        }
        nomeScheda = orp.doc_id + " | " + orp.cod_art + " | " + orp.quant_da_prod + " | " + orpTipo;
        var newCard = 
            {
            name: nomeScheda, 
            desc: "Data documento: "+orp.data_doc +"\n"+ ((orpTipo=="L")?"Lotto: "+orp.cod_lot+"\n":"") + JSON.stringify(orp),
            pos: "top", 
            due: dataFinePrevista.toISOString(),
            idList: LISTAid
            };
        Trello.post('/cards/', newCard, tcard_success, t_error);
    }  
};

function CreateChecklist(card) {
    var newList = 
        {
        idCard:card.id,
        name:"Cicli di lavorazione",
        pos:"top"
        }
    Trello.post('/checklists/', newList, CreateCheckListItems, t_error);
}

function CreateCheckListItems(checkList) {
    console.log("Checklist created (id="+checkList.id+"):");
    console.dir(checkList);
    //var newItem = "TESTNAME"; // WRONG version
    var newItem = {name:"TESTNAME"}; // CORRECTED version. Thanks Casey
    Trello.post('/checklists/'+checkList.id+'/checkItems/', newItem, null, t_error);    
}



Trello.authorize({
    interactive:false,
    success: onAuthorize
});

$("#connectLink")
.click(function(){
    Trello.authorize({
        type: "popup",
        scope: { read: true, write: true },
        success: onAuthorize
    })
});

$("#disconnect").click(logout);

$("#btn").click(doCreate);


});//]]> 

</script>

<div id="loggedout">
    <a id="connectLink" href="#">Connect To Trello</a>
</div>

<div id="loggedin">
    <div id="header">
        Logged in to as <span id="fullName"></span> 
        <a id="disconnect" href="#">Log Out</a>
    </div>
    <div id="form">
      <h3>ORDINI</h3>
      <textarea id="dataText" name="testo" rows="12" cols="80">[
  {
    "doc_id": "2011-ORP-0000126",
    "data_doc": "24.03.2011",
    "cod_art": "4010103000",
    "quant_da_prod": "1500",
    "data_fine_prevista": "20.04.2011",
    "flag_matricola": "0",
    "flag_lotto": "0",
    "cod_lot": "",
    "FASI": [
      {
        "riga": "1",
        "flag_ciclo_est": "1",
        "cod_ciclo": "029",
        "des_ciclo": "COSTRUZIONE",
        "cod_cf": "000261",
        "rag_soc_cf": "SOME COMPANY SRL"
      }
    ]
  }
]</textarea><br/><br/>
      <a id="btn" href="#">ELABORA</a>
    </div>
    <div id="output"></div>
</div>    
</body>
</html>

Solution

  • It doesn't look like you are populating the item with properly formatted JSON.

    Try changing newItem to below.

    var newItem = { "name": "TESTNAME" };
    

    See the API documentation for the other input values: https://developers.trello.com/advanced-reference/checklist#post-1-checklists-idchecklist-checkitems

    Hope this helps!