In the previous posts, we had seen different script types and how to trigger scripts from different places in PLM. The next few posts will focus on the code itself. We will see how to use script create an item, update the bill of materials, add a milestone, etc. First of all, let’s start with the most important object in PLM script: Item.
Item object is the representation of an item in scripting. In each item’s web page, we can easily acquire the relevant information about the item, such as the value in each field, the item’s owner, all the bill of materials, etc. We also can update the item by creating a relationship with another item, performing a workflow action and so on. All of those operations can also be done with Item object in scripting. So Item object will be our next big topic. In the each of coming posts, we will focus on one aspect of this object. In today’s post, I will show you how to load, create, and delete an item.
Load Item: loadItem(dmsId)
To load an existing item, we need use the built-in function: loadItem. It takes the item’s DMS ID as the parameter, and returns the corresponding Item object. For example, if we want to load the existing item whose DMS ID is 1234 into a variable called myItem, then this is what we need:
var myItem = loadItem(1234);
Since every PLM script only can be triggered within an item’s scope, a reserved keyword “item” is used to represent the item which the script is triggered from. We can call that item the owner item of the script. In each script, you can assume that the owner item the script has been loaded into a variable called “item” for you, so you can use “item” directly to work on the owner item. If you need any other items, then you should use loadItem function.
Create Item: createItem(workspaceId)
createItem can be used to create a new item in a certain workspace. The particular workspace should be specified using the parameter “workspaceId”; the created Item object will be returned. This “workspaceId” is not the numerical workspace identity, but the string identity, which can be found in each workspace’s setting page. [Administration -> Workspace Manager -> workspace –> Workspace Settings]. Then you can assign value to each field of the returned item variable, which will be saved into this new item.
For example, to create an item in workspace “Project”:
var newItem = createItem(“WS_PROJECT”);
newItem.NAME = “PLM”; // set String value to a Text field
newItem.STARTDATE = new Date(); // set Date value to the Date field
Delete Item: item.deleteItem()
To delete the owner item of the script, we only need call the delete function from the owner item:
item.deleteItem();
To delete other item, we need load the item first, and then delete it:
var itemToDelete = loadItem(1234);
itemToDelete.deletetItem();
--Michal