Pre-populating a subform with Client Script in CRM

09/27/2024 04:58 PM - Comment(s) - By Jesus Sosa

This code below is an example to to use client scrip to auto pre-populate a Subform with rows fill dynamically within CRM using Client Scrips



/** 
 * log("sample logging statement") --> can be used to print any data in the browser console.
 * ZDK module can be used for customising the UI and other functionalities.
 * return false to prevent <SAVE> action
**/

const productID = ["5737555000000481005", "5737555000000485053", "5737555000000485054", "5737555000000485055", "5737555000000485056", "5737555000000485057", "5737555000000485058", "5737555000000485059", "5737555000000486053", "5737555000000486054", "5737555000000486055", "5737555000000485060", "5737555000000486056", "5737555000000486057", "5737555000000486058", "5737555000000486059", "5737555000000486060", "5737555000000486061", "5737555000000486062", "5737555000000486063", "5737555000000486064", "5737555000000486065", "5737555000000486066", "5737555000000486067", "5737555000000486068", "5737555000000486069", "5737555000000486070", "5737555000000486071", "5737555000000486072", "5737555000000486073", "5737555000000486074", "5737555000000486075", "5737555000000486076", "5737555000000486077", "5737555000000486089", "5737555000000486084", "5737555000000486085", "5737555000000486087", "5737555000000486088", "5737555000000486092", "5737555000000486081", "5737555000000486093", "5737555000000486094", "5737555000000486095", "5737555000000486096", "5737555000000485061", "5737555000000486097", "5737555000000486098", "5737555000000486099", "5737555000000486100", "5737555000000486101", "5737555000000486102", "5737555000000486103", "5737555000000486104", "5737555000000486105", "5737555000000486106", "5737555000000486107", "5737555000000486090", "5737555000000486091", "5737555000000486086", "5737555000000486108", "5737555000000486109", "5737555000000486082", "5737555000000486083", "5737555000000486110", "5737555000000486113", "5737555000000486078", "5737555000000486079", "5737555000000486080", "5737555000000486114", "5737555000000486115", "5737555000000486111", "57375550000004861","5737555000000486112", "5737555000000486116"];
let fetched_products = ZDK.Apps.CRM.Products.searchByCriteria("id:in:" + productID.join(','));
console.log("Products",fetched_products)

 let lineItemPayload = []; 
// for (i=0;i<fetched_products.length;i++)
// {
 
// // var val = ZDK.Apps.CRM.Products.fetchById(productID[i]);
// console.log(" i Value : ", i ); 
// console.log("Product Name : ", fetched_products[i].Product_Name);
// console.log("Product ID : ", fetched_products[i].id);
// console.log("Price : ", fetched_products[i].Unit_Price);
// const lineItem = {
// "Product_Name": {
// "name":fetched_products[i].Product_Name,
// "id": fetched_products[i].id
// },
// "List_Price": fetched_products[i].Unit_Price
// }
// lineItemPayload.push(lineItem); 
// }

for (var i = 0; i < productID.length; i++)
{
 for (var j = 0; j < fetched_products.length; j++)
 {
 if (productID[i] == fetched_products[j].id)
 {
 const lineItem = {
 "Product_Name": {
 "name":fetched_products[j].Product_Name,
 "id": fetched_products[j].id
 },
 "List_Price": fetched_products[j].Unit_Price,
 "Quantity":"0"
 }
 lineItemPayload.push(lineItem);
 }
 }
}

console.log("Line Item Payload : ", lineItemPayload );
ZDK.Page.getForm().setValues({'Quoted_Items': lineItemPayload});

var lockear = ZDK.Page.getSubform('Quoted_Items').getField('List_Price');
log(lockear);
lockear.setReadOnly(true);

Jesus Sosa

Share -