{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":37457,"path":"order-management/enrichments_on_bom_response_scripts.md","title":"The On BOM Response enrichment","bundle":null,"doc_type":null,"release":"australia","body":"\n# The On BOM Response enrichment\n\nYou can use this enrichment to work with ProductList items whenever there is an update to the bill of materials.\n\nThe On BOM Response enrichment is enabled by default in environments and can be used to manipulate items in the ProductList whenever there is an update to the bill of materials \\(BOM\\).\n\n**Note:** The CPQ environment must include rules with product actions, or there will be nothing for the enrichment to loop through.\n\n\\[Omitted image \"cpq-enrichments-on-bom-response.png\"\\] Alt text: BOM response\n\nIn the following sample, the On BOM Response enrichment loops through products and changes the price of a product.\n\n```\n//Loop over products and change price of a product\nlet dealProduct = cfg.blueMoonSelected;\nif (dealProduct == true) {\n        for(var prod of ProductList) {\n                let price = prod.price * .8;\n                prod.price = price;\n        }\n}\n```\n\nOn BOM Response enrichments may request external REST services via GET or POST.\n\nIn the following sample script, the On BOM Response enrichment uses an external connection to query a powerPricing API with customer data. With the customer-specific rate data retrieved from the service, the enrichment adjusts the pricing of existing ProductList records.\n\n```\nvar powerInputs = {\"membershipCode\":cfg.eCMembershipCode, \"icp\":cfg.eCICPNumber};\nlet powerResponse = External.powerPricing(powerInputs);\nlet dailyCharge = 0; let ratesArr = [];\n\nif(powerResponse.status == 200) {\n\tfor(var record of powerResponse.body) {\n\t\tif(record.chargeType == cfg.expectedUsage) {\n\t\t\tdailyCharge = record.dailyCharges;\n\t\t\tratesArr = record.rates;\n\t\t}\n\t}\n\tfor(var prod of ProductList) {\n\t\tif(prod.id==\"electricBillEstimator\") {\n\t\t\tlet addedPrice = dailyCharge * cfg.serviceDurationInDays;\n\t\t\tprod.price = addedPrice;\n\t\t}\n\t\t\tif(prod.id==\"Standard Rate\" || prod.id==\"Low Rate\") {\n\t\t\tprod.price = dailyCharge;\n\t\t}\n\t}\n\tfor(var rateVal of ratesArr) {\n\t\tProductList.id = \"Additional Charge Per KWH: \" + rateVal.name;\n\t\tProductList.quantity = 1;\n\t\tProductList.bomType = \"Manufacturing\";\n\t\tProductList.orderNumber = 2;\n\t\tProductList.price = rateVal.rate;\n\t\tProductList.notes = rateVal.measure;\n\t\tProductList.parentProduct = \"electricBillEstimator\";\n\t\tProductList.next();\n\t}\n}\n\nreturn ProductList;\n```\n\n## The difference between the rules engine BOM and the enriched BOM\n\nThere are two versions of the BOM during runtime: the rules engine BOM, which is based on the results of product rules during runtime, and the enriched BOM, which is the result of the BOM enrichment.\n\nThe BOM enrichment's ProductList object \\(referenced in the script\\) is the result of the rules engine BOM, not the enriched BOM. This is to make sure there are no infinite loops. Suppose that the script were to reference its own ProductList that it returns, and the script were set up to increase the price of certain objects by a percentage on BOM update. In this case, the next time the contents of the BOM were updated, the price increase would happen again and again.\n\nTo prevent this situation, the enriched BOM is not referenced by any rule or enrichment other than the validation enrichment's ProductList. While this makes sure that the BOM enrichment can function outside the rules engine, it also means that if a condition is met in the BOM enrichment, those conditions can be met again the next time the BOM is updated, because any change to the BOM in the BOM enrichment script that stops that condition will not be referenced.\n\nWith external connections, this is not a problem as long as the response is consistent. However, if the response changes when fired multiple times, such as when generating a string or saving a certain time, it will update each time the BOM is updated.\n\nThe BOM Response enrichment might be used to:\n\n-   dynamically change the price of products in the BOM. This can be either a static or dynamic change rate, such as a shipping fee being static and taxes being dynamic.\n-   change attributes of products in the BOM that are reliant on some condition in the BOM, such as the quantity of free stickers given out once $100+ of tax has been quoted.\n-   remove a line item from the BOM.\n\n"}}