Hello all,
While creating Part, Document will be created with same as Part Number and Description. I have written server method (OnAfterAdd) for this.
Dim myInnov As Innovator = Me.newInnovator()Dim partID As String = Me.getID()
Dim partItem As Item = myInnov.newItem("Part", "get")partItem.setAttribute("id", partID)partItem.setAttribute("select", "id,item_number,name,major_rev,state") partItem = partItem.apply()
Dim id As String = partItem.getProperty("id")Dim itemNumber As String = partItem.getProperty("item_number")Dim name As String = partItem.getProperty("name")Dim revision As String = partItem.getProperty("major_rev")Dim state As String = partItem.getProperty("state")
Dim doc As Item = myInnov.newItem("Document","add")doc.setProperty("source_id", id)doc.setProperty("item_number", itemNumber)doc.setProperty("keyed_name", itemNumber)doc.setProperty("name", name)doc.setProperty("state", state)doc.setProperty("major_rev", revision)partItem.addRelationship(doc)partItem = partItem.apply()
1. I want to know how to add/ relate that created Document in Part Documents tab
2. While renaming the Part Number, Document Number should also be renamed automatically.
Can anyone help me to fix the above issues??
Thanks & Regards,
Sherene
1. You need to create the "Part Document" relationship item. Try this code:
Dim relItem = Me.newItem("Part Document","add")relItem.setProperty("source_id",Me.getID())Dim docItem = relITem.createRelatedItem("Document","add")docItem.setProperty("item_number", Me.getProperty("item_number",""))docItem.setProperty("name", Me.getProperty("name",""))relItem = relItem.apply()If (relItem.isError()) Then Return relItemEnd If
Also, note that "keyed_name", "state' and "major_rev" will be set automatically when the Document is added. If you want these values to match the Part then you should make sure the Keyed Name Order, default Life Cycle and Revisions match between the Part and Document ItemTypes (they do by default).
2.You can do this with an OnAfterUpdate on the Part ItemType. Keep in mind that the user can attach another Document to the Part, so you'll need to make sure you update the correct related Document.
Rob
Thanks Rob for your quick reply..
I'm facing eror in updating the Document Number while renaming the Part Number. Can you tell me when I've gone wrong?
Dim partItem As Item = myInnov.newItem("Part", "get")partItem.setProperty("id", partID)partItem.setAttribute("select", "id,item_number") partItem = partItem.apply()
Dim relItem = Me.newItem("Part Document","get")relItem.setProperty("source_id",partID)relItem = partItem.fetchRelationships("Part Document").getRelationships("Part Document").getItemByIndex(0)relItem.setAttribute("action","edit")Dim docItem As String= relItem.getRelatedItemID()docItem.setProperty("item_number", Me.getProperty("item_number",""))relItem = relItem.apply()
Thanks,
The most straightforward way of doing this is to first find the id of the Document you want to edit, then perform the edit (2 separate transactions). It is possible to do it in one transaction (using a where attribute) but it's probably not worth it for this case.
The code should look something like this (untested):
Dim relItem As Item = Me.newItem("Part Document","get")relItem.setProperty("source_id",Me.getID())relItem = relItem.apply()If (relItem.isError) Then Return relItemEnd IfDim docId As String = relItem.getItemByIndex(0).getProperty("related_id","-")
Dim docItem = Me.newItem("Document","edit")docItem.setID(docId)docItem.setProperty("item_number",Me.getProperty("item_number",""))docItem = docItem.apply()
Remember that there can be more than one Document attached, so be sure to have some way of selecting the right one.