Created
April 7, 2020 12:33
-
-
Save YMA-MDL/7c9f131fea991fc1833b05ed5e125af2 to your computer and use it in GitHub Desktop.
Create an Express ECO to replace a selected Part with another chosen Part in all parent assemblies #aras #javascript #eco #impactmatrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MassReplace | |
// Create an Express ECO to replace a selected Part with another chosen Part in all parent assemblies | |
var inn = this.getInnovator(); | |
// Make sure only one item is selected | |
if (this.getItemCount() !== 1) { return inn.newError("Please select a single item"); } | |
// Launch a search dialog to select a replacement Part | |
// TODO: create a better UI | |
var param = { aras: top.aras, itemtypeName: "Part", multiselect: false }; | |
var options = { dialogHeight:450, dialogWidth:700, resizable:true}; | |
var dlgRes = top.aras.modalDialogHelper.show('DefaultModal', window, param, options, 'searchDialog.html'); | |
if (!dlgRes.item) { return this; } | |
var newId = dlgRes.itemID; | |
// Find all current Part BOM relationships where the selected Part is the related item | |
var partBoms = inn.newItem("Part BOM","get"); | |
partBoms.setAttribute("select","sort_order, quantity,source_id(id,keyed_name)"); | |
partBoms.setProperty("related_id",this.getID()); | |
var parentPart = inn.newItem("Part","get"); | |
parentPart.setProperty("is_current","1"); | |
partBoms.setPropertyItem("source_id",parentPart); | |
partBoms = partBoms.apply(); | |
if (partBoms.getItemCount() < 1) { return inn.newError("No parent assemblies found"); } | |
// Create a new Express ECO client cache item, then load it into an IOM item for convenience | |
var ecoItem = top.aras.newItem("Express ECO","add"); | |
var eco = inn.newItem("",""); | |
eco.loadAML(ecoItem.xml); | |
// Create an Affected Item for the selected Part with action=None (not modifying the Part itself, just replacing it everywhere it's used) | |
var ecoAffItem = inn.newItem("Express ECO Affected Item","add"); | |
eco.addRelationship(ecoAffItem); | |
var affItem = inn.newItem("Affected Item","add"); | |
ecoAffItem.setRelatedItem(affItem); | |
affItem.setProperty("item_action","None"); | |
affItem.setProperty("affected_id",this.getID()); | |
affItem.setPropertyAttribute("affected_id","keyed_name",this.getProperty("keyed_name","")); | |
// Loop through the Part BOMs with the selected Part as the child | |
for (var i=0; i<partBoms.getItemCount(); i++) | |
{ | |
var thisBom = partBoms.getItemByIndex(i); | |
var thisPart = thisBom.getPropertyItem("source_id"); | |
// Create an Affected Item for each parent assembly, with action=Revise | |
ecoAffItem = inn.newItem("Express ECO Affected Item","add"); | |
eco.addRelationship(ecoAffItem); | |
affItem = inn.newItem("Affected Item","add"); | |
ecoAffItem.setRelatedItem(affItem); | |
affItem.setProperty("item_action","Revise"); | |
affItem.setProperty("affected_id",thisPart.getID()); | |
affItem.setPropertyAttribute("affected_id","keyed_name",thisPart.getProperty("keyed_name","")); | |
// Create an Affected Relationship for the parent assembly to Remove the original Part | |
var affItemRel = inn.newItem("Affected Item Relationship","add"); | |
affItem.addRelationship(affItemRel); | |
var affRel = inn.newItem("Affected Relationship","add"); | |
affItemRel.setRelatedItem(affRel); | |
affRel.setProperty("affected_rel_id",thisBom.getID()); | |
affRel.setProperty("rel_action","Remove"); | |
// Create an Affected Relationship for the parent assembly to Attach the new Part | |
affItemRel = inn.newItem("Affected Item Relationship","add"); | |
affItem.addRelationship(affItemRel); | |
affRel = inn.newItem("Affected Relationship","add"); | |
affItemRel.setRelatedItem(affRel); | |
affRel.setProperty("rel_action","Attach"); | |
var affRelProp = inn.newItem("Affected Relationship Property","add"); | |
affRel.addRelationship(affRelProp); | |
affRelProp.setProperty("property_name","related_id"); | |
affRelProp.setProperty("new_value",newId); | |
affRelProp = inn.newItem("Affected Relationship Property","add"); | |
affRel.addRelationship(affRelProp); | |
// Set the new sort_order and quantity to the same values as the original relationship | |
affRelProp.setProperty("property_name","sort_order"); | |
affRelProp.setProperty("new_value",thisBom.getProperty("sort_order","")); | |
affRelProp = inn.newItem("Affected Relationship Property","add"); | |
affRel.addRelationship(affRelProp); | |
affRelProp.setProperty("property_name","quantity"); | |
affRelProp.setProperty("new_value",thisBom.getProperty("quantity","")); | |
} | |
// Reassign the client cache item to the IOM item node, then add to the cache | |
ecoItem = eco.node; | |
top.aras.itemsCache.addItem(ecoItem); | |
// Open the newly created Express ECO for the user to review/save | |
top.aras.uiShowItemEx(ecoItem, "tab view"); | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment