4

Do you know if exists any way to get the componentId (tcm) after create it with the code placed in this link How to create a component in the Tridion JS Framework (Anguilla)??

4

If you don't get any errors after the first item.save() method and the item is correctly created you should be able to get the Id through item.getId() function.

Using the example you refer, and remember what Bart points about asynchrony, you can listen to save and savefailed events on your item:

var item = $models.createNewItem($const.ItemType.COMPONENT);

//[...]

function onSaveError(event) {
    $log.error(event.data.ExceptionDetail.Message);
}

function onSave() {
    $log.info("Item's Id: " + item.getId());
}

$evt.addEventHandler(item, "load", onLoad);
$evt.addEventHandler(item, "savefailed", onSaveError);
$evt.addEventHandler(item, "save", onSave);
item.load(true);

So because asynchronous execution you can't write your code like this:

item.save(true);
var Id = item.getId(); //it's going to be undefined because save function is asynchronous
  • Although the item is correctly created, the instruction item.getId() returns me "undefined"... Any solution? – Sergio Alonso Jun 1 '17 at 16:05
  • Its possible to get the ComponentId with OrganizationalItemID and the componentName or similar with Anguilla? – Sergio Alonso Jun 1 '17 at 16:14
  • 3
    @netosone if item.getId() returns "undefined", it sounds like the item isn't loaded, so I think you should edit your question and update it with a bit more relevant details and maybe some example code you are using, so you can say where it is failing. Keep in mind that the Anguilla framework is asynchronous, which sometimes requires a different approach, since if items are not (fully) loaded, you won't have all its properties available. – Bart Koopman Jun 1 '17 at 16:34
  • I've added more information to the response to illustrate what Bart says. – Diego Rodríguez Jun 2 '17 at 6:49
  • Great!! I just write this code and works fine!! Thank you – Sergio Alonso Jun 2 '17 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.