function getItemPricing(recordObj) {
try {
let customerName = recordObj.getValue({
fieldId: 'entity'
})
console.log('customer', customerName)
let itemName = recordObj.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
console.log('itemName', itemName)
let salesCurrency = recordObj.getValue({
fieldId: 'currency'
})
let price;
var customrecord_jj_cust_price_levelSearchObj = search.create({
type: "customrecord_jj_cust_price_level",
filters:
[
["custrecord_jj_pricelevel_customer", "is", customerName],
"AND",
["custrecord_jj_pricelevel_item", "is", itemName],
"AND",
["custrecord_jj_pricelevel_currency", "anyof", salesCurrency]
],
columns:
[
search.createColumn({ name: "custrecord_jj_pricelevel_customer", label: "Customer" }),
search.createColumn({ name: "custrecord_jj_pricelevel_item", label: "Item" }),
search.createColumn({ name: "custrecord_jj_pricelevel_unit_price", label: "Unit Price" })
]
});
var searchResultCount = customrecord_jj_cust_price_levelSearchObj.runPaged().count;
console.log("customrecord_jj_cust_price_levelSearchObj result count", searchResultCount);
customrecord_jj_cust_price_levelSearchObj.run().each(function (result) {
price = result.getValue({ name: "custrecord_jj_pricelevel_unit_price", label: "Unit Price" })
// .run().each has a limit of 4,000 results
return true;
});
return price
}
catch (e) {
console.error('error@getItemPricing', e)
}
}
/**
* Function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @since 2015.2
*/
function fieldChanged(scriptContext) {
try {
let recordObj = scriptContext.currentRecord; // Access the current record
let stCurrField = scriptContext.fieldId;
let stCurrSublist = scriptContext.sublistId;
// Handle changes in the 'item' sublist
if (stCurrSublist === 'item') {
let priceLevel = recordObj.getCurrentSublistText({
sublistId: 'item',
fieldId: 'price'
});
let customPricing = recordObj.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_custom_pricing'
});
console.log(`Field Changed: ${stCurrField}, Price Level: ${priceLevel}, Custom Pricing: ${customPricing}`);
// When 'price' field changes
if (stCurrField === 'price') {
if (priceLevel !== 'Custom' && customPricing) {
// Clear custom pricing if price level is not "Custom"
recordObj.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_custom_pricing',
value: '',
ignoreFieldChange: true // Prevent retriggering fieldChanged
});
console.log('Cleared custom pricing because price level is not "Custom".');
} else if (priceLevel === 'Custom' && customPricing) {
// Update rate if price level is "Custom" and custom pricing exists
updateRateBasedOnCustomPricing(recordObj);
}
}
// When 'custcol_jj_custom_pricing' field changes
if (stCurrField === 'custcol_jj_custom_pricing') {
if (priceLevel === 'Custom' && customPricing) {
updateRateBasedOnCustomPricing(recordObj);
} else if (!customPricing) {
// Clear rate if custom pricing is removed
recordObj.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: '',
ignoreFieldChange: true
});
console.log('Cleared rate as custom pricing was removed.');
}
}
// When an item is added and price level is populated
if (stCurrField === 'item' && priceLevel) {
if (priceLevel === 'Custom' && customPricing) {
updateRateBasedOnCustomPricing(recordObj);
}
}
}
} catch (e) {
console.error('Error @fieldChanged:', e.message, e.stack);
}
}
/**
* Function to calculate and set the rate field based on custom pricing.
* @param {Object} recordObj - Current record object
*/
function updateRateBasedOnCustomPricing(recordObj) {
try {
// Calculate price value using custom logic
let priceValue = getItemPricing(recordObj);
// Validate priceValue
if (priceValue !== null && priceValue !== undefined) {
console.log('Calculated Price Value:', priceValue);
// Set the rate field with the calculated value
recordObj.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: priceValue,
ignoreFieldChange: true // Prevent retriggering fieldChanged
});
console.log('Rate field updated successfully based on custom pricing.');
} else {
console.log('Price calculation failed. Verify getItemPricing logic.');
}
} catch (e) {
console.error('Error @updateRateBasedOnCustomPricing:', e);
}
}