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)??

1 Answer 1

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
5
  • Although the item is correctly created, the instruction item.getId() returns me "undefined"... Any solution? Commented Jun 1, 2017 at 16:05
  • Its possible to get the ComponentId with OrganizationalItemID and the componentName or similar with Anguilla? Commented Jun 1, 2017 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. Commented Jun 1, 2017 at 16:34
  • I've added more information to the response to illustrate what Bart says. Commented Jun 2, 2017 at 6:49
  • Great!! I just write this code and works fine!! Thank you Commented Jun 2, 2017 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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