Skip to content

Instantly share code, notes, and snippets.

@cgillis-aras
Created October 11, 2019 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgillis-aras/051bf7581ac7fcff440cf1fbdab7a373 to your computer and use it in GitHub Desktop.
Save cgillis-aras/051bf7581ac7fcff440cf1fbdab7a373 to your computer and use it in GitHub Desktop.
Example of rolling up a property on the relationships to the parent
/*
* NOTE: This code is a sample to demonstrate how to rollup one level of a relationship.
* It is has not been fully tested and should not be used in a production environment as-is.
*/
/* Query for item and relationships happens above */
parentPart = parentPart.apply();
// Get the relationships from your item
Item partBoms = parentPart.getRelationships("Part BOM");
// Add all of the costs of the related parts together
double totalCost = 0;
for (int i = 0; i < partBoms.getItemCount(); i++)
{
var partBom = partBoms.getItemByIndex(i);
var childPart = partBom.getRelatedItem();
// Quick validation so our parsing logic doesn't fail
string childCost = childPart.getProperty("cost", "");
if (!string.IsNullOrEmpty(childCost))
{
totalCost += double.Parse(childCost);
}
}
// Set the total cost on our parent part
parentPart.setProperty("cost", totalCost.ToString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment