{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":37301,"path":"order-management/cpq-sample-scripts.md","title":"Sample scripts","bundle":null,"doc_type":null,"release":"australia","body":"\n# Sample scripts\n\nView a selection of commonly requested sample scripts.\n\nThis article shows some commonly requested sample scripts.\n\nSample number field determination:\n\n```\nvar count64 = 0;\nvar count32 = 0;\nvar perCoreProcessing = 40; //assuming 8 processors with 5 TFlops compute\nvar cpu64Processing = 64*perCoreProcessing;\nvar cpu32Processing = 32*perCoreProcessing;\nvar cpu16Processing = 16*perCoreProcessing;\nvar remainingCompute = cfg.estPeakOps;\nif(cfg.allowCPU64 == true) { //64 core allowed so divide by that\n\tcount64 = remainingCompute/cpu64Processing;\n\tremainingCompute = remainingCompute%cpu64Processing;\n}\nif(cfg.allowCPU32 == true) {\n\tcount32 = remainingCompute/cpu32Processing;\n\tremainingCompute = remainingCompute%cpu32Processing;\n}\nreturn count32;\n```\n\nSample text field determination:\n\n```\nvar retStr = \"\";\nretStr = retStr + \"64 vCPUs: \" + Math.round(cfg.cpu64Count);\nretStr = retStr + \"\\\\n32 vCPUs: \" + Math.round(cfg.cpu32Count);\nretStr = retStr + \"\\\\n16 vCPUs: \" + Math.round(cfg.cpu16Count);\nretStr = retStr + \"\\\\n8 vCPUs: \" + Math.round(cfg.cpu8Count);\nretStr = retStr + \"\\\\n1 vCPUs: \" + Math.round(cfg.cpu1Count);\nreturn retStr;\n```\n\nSample product rule script:\n\n```\n//Base Product\nvar baseProductP2ID = \"BP_ID\";\nvar inputs = {\"g2BaseProduct\": cfg.g2BaseProduct};\nvar resultSet = lookup(\"SELECT Product2Id, ProductName FROM Dummytable WHERE ProductCode = :g2BaseProduct\", inputs);\nfor (var row of resultSet) {\n\tProductList.id = row.get(\"Product2Id\");\n\tProductList.quantity = cfg.orderQty;\n\tProductList.bomType = \"SALES\";\n\tProductList.uom = \"EA\";\n\tProductList.level = 1;\n\tProductList.notes = row.get(\"ProductName\");\n\tProductList.parentProduct = \"01t5f000000uJgoAAE\"; //parent product identifier. In this case top product being configured from SFDC\n\tbaseProductP2ID = row.get(\"Product2Id\");\n\tProductList.uniqueIdentifier = baseProductP2ID; //Parent\n\t...SF ProductID; not req'd if this has no other children\n\tProductList.next();\n\tbreak;\n}\n```\n\nSample product rule script with `for` loop:\n\n```\n//var timesReplicate = cfg.numberOfComputeClusters; //this doesn't work yet.. If it does, we will replicate that many times easier\nvar timesReplicate = [1,1,1,1,1,1,1,1,1,1]; //dummy 10 times loop based on 10x boolean true, Double loop for 100\nfor (var parentStep of timesReplicate) {\n\tfor (var step of timesReplicate) {\n\t\tif(cfg.cpu16Count > 0) {\n\t\t\tProductList.id = \"01t5f000000v50CAAQ\";\n\t\t\tProductList.quantity=Math.round(cfg.cpu16Count);\n\t\t\tProductList.notes=\"Provisioning \" + Math.round(cfg.cpu16Count) + \" number of 16 core vCPUs\";\n\t\t\tif(cfg.sendDirectlyToProvisioning == true) {\n\t\t\t\tProductList.bomType = \"Manufacturing\";\n\t\t\t\tProductList.level = 2;\n\t\t\t} else {\n\t\t\t\tProductList.bomType = \"SALES\";\n\t\t\t}\n\t\t\tProductList.next();\n\t\t}\n\t\tif(cfg.cpu8Count > 0) {\n\t\t\tProductList.id = \"01t5f000000v50DAAQ\";\n\t\t\tProductList.quantity=Math.round(cfg.cpu8Count);\n\t\t\tProductList.notes=\"Provisioning \" + Math.round(cfg.cpu8Count) + \" number of 8 core vCPUs\";\n\t\t\tif(cfg.sendDirectlyToProvisioning == true) {\n\t\t\t\tProductList.bomType = \"Manufacturing\";\n\t\t\t\tProductList.level = 2;\n\t\t\t} else {\n\t\t\t\tProductList.bomType = \"SALES\";\n\t\t\t}\n\t\t\tProductList.next();\n\t\t}\n\t\tif(cfg.cpu32Count > 0) {\n\t\t\tProductList.id = \"01t5f000000v50BAAQ\";\n\t\t\tProductList.quantity=Math.round(cfg.cpu32Count);\n\t\t\tProductList.notes=\"Provisioning \" + Math.round(cfg.cpu32Count) + \" number of 32 core vCPUs\";\n\t\t\tif(cfg.sendDirectlyToProvisioning == true) {\n\t\t\t\tProductList.bomType = \"Manufacturing\";\n\t\t\t\tProductList.level = 2;\n\t\t\t} else {\n\t\t\t\tProductList.bomType = \"SALES\";\n\t\t\t}\n\t\t\tProductList.next();\n\t\t}\n\t\tif(cfg.cpu64Count > 0) {\n\t\t\tProductList.id = \"01t5f000000v50JAAQ\";\n\t\t\tProductList.quantity=Math.round(cfg.cpu64Count);\n\t\t\tProductList.notes=\"Provisioning \" + Math.round(cfg.cpu64Count) + \" number of 64 core vCPUs\";\n\t\t\tif(cfg.sendDirectlyToProvisioning == true) {\n\t\t\t\tProductList.bomType = \"Manufacturing\";\n\t\t\t\tProductList.level = 2;\n\t\t\t} else {\n\t\t\t\tProductList.bomType = \"SALES\";\n\t\t\t}\n\t\t\tProductList.next();\n\t\t}\n\t\tif(cfg.cpu1Count > 0) {\n\t\t\tProductList.id = \"01t5f000000v50SAAQ\";\n\t\t\tProductList.quantity=Math.round(cfg.cpu1Count);\n\t\t\tProductList.notes=\"Provisioning \" + Math.round(cfg.cpu1Count) + \" number of 1 core vCPUs\";\n\t\t\tif(cfg.sendDirectlyToProvisioning == true) {\n\t\t\t\tProductList.bomType = \"Manufacturing\";\n\t\t\t\tProductList.level = 2;\n\t\t\t} else {\n\t\t\t\tProductList.bomType = \"SALES\";\n\t\t\t}\n\t\t\tProductList.next();\n\t\t}\n\t}\n}\nconsole.log(\"Added line items 100 times\");\nreturn ProductList;\n```\n\nSample array and map functions:\n\n```\nvar productidArr = [\"test123\", \"chtest123\"];\nvar inputs = {\"productids\": productidArr};\nvar resultSet = lookup(\"SELECT uniqueidentifier, bomtype FROM bominformation WHERE productid IN (:productids)\", inputs);\n\nvar prodTypeMap = new Map();\nfor (var row of resultSet) {\n\tprodTypeMap.set(row.get(\"uniqueidentifier\"), row.get(\"bomtype\"));\n}\nvar itemTypeManuf = productidArr.every((value) => {return prodTypeMap.get(value) != \"Manufacturing\";});\nif (isGood == false) {\n\treturn \"Sales type item found\";\n}\nvar summary = \"\";\nproductidArr.forEach((item, index) => {summary += index + \" \" + item + \". \";});\nreturn summary;\n```\n\nHow to pull multi-select picklist values from SFDC to twinned multi-select picklist field in CPQ in the On Configure/Reconfigure enrichment:\n\n```\nlet x = (cfgRequest.yourTwinnedLogikField.value).split(\";\");\ncfgRequest.yourTwinnedLogikField.set(\"value\", x);\nreturn cfgRequest;\n```\n\nHow to concatenate field values into a string:\n\n```\nstringVar = \"\";\nstringVar = stringVar.concat(cfg.varible1, cfg.variable2, cfg.variable3);\nreturn stringVar;// returns selected values of \"variable1variable2variable3\"\n```\n\nHow to imitate a CONTAINS function:\n\n```\nvar yourVariable = \"Field Value\"; // Replace with your actual Field value\nif (yourVariable.includes(\"CU\")) {\n\t// Code you wish to execute when Field Value contains \"CU\"\n} else if (yourVariable.includes(\"AL\")) {\n\t// Code you wish to execute when Field Value contains \"AL\"\n} else {\n\t// Code you wish to execute when Field Value doesn't contain \"CU\" or \"AL\"\n};\n```\n\n**Related topics**  \n\n\n[CPQ scripting language reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown)\n\n"}}