Aaron McCausland
05/22/2026, 1:50 PMbattk
05/23/2026, 6:08 AMjen
05/25/2026, 5:43 PMAaron McCausland
05/26/2026, 1:33 PMconst sourceRec = record.load({type: record.Type.ESTIMATE, id: contextValue.id, isDynamic: false});
const targetRecId = sourceRec.getValue({fieldId: "opportunity"});
const targetRec = record.load({type: record.Type.OPPORTUNITY, id: targetRecId, isDynamic: false});
const targetLineCount = targetRec.getLineCount({sublistId: sublist});
for (let targetLine = targetLineCount - 1; targetLine >= 0; targetLine--){
const targetLineItemType = targetRec.getSublistValue({sublistId: sublist, fieldId: "itemtype", line: targetLine});
if ("EndGroup" == targetLineItemType){
log.debug(`Skipping removal of End Group line on target ${targetRec.type} with ID ${targetRec.id}`, `targetLine: ${targetLine}`);
continue; //Deleting an End Group line is not allowed. Delete the Group instead.
}
targetRec.removeLine({sublistId: sublist, line: targetLine});
log.debug(`Removed ${targetLineItemType} line ${targetLine} during sync of sublist ${sublist}`, `Target Record: ${targetRec.type} with ID ${targetRec.id}`);
changes.anyChange = true;
}
const checkEmptyTargetLineCount = targetRec.getLineCount({sublistId: sublist});
log.debug(`After removing all lines, target record has ${checkEmptyTargetLineCount} lines in sublist ${sublist}`, `Target Record: ${targetRec.type} with ID ${targetRec.id}`);
const sourceLineCount = sourceRec.getLineCount({sublistId: sublist});
let groupTotal = 0;
for (let i = 0; i < sourceLineCount; i++){
const sourceItem = sourceRec.getSublistValue({sublistId: sublist, fieldId: "item", line: i});
const sourceItemType = sourceRec.getSublistValue({sublistId: sublist, fieldId: "itemtype", line: i});
const sourceUnits = sourceRec.getSublistValue({sublistId: sublist, fieldId: "units", line: i});
const sourceQuantity = sourceRec.getSublistValue({sublistId: sublist, fieldId: "quantity", line: i});
const sourceRate = sourceRec.getSublistValue({sublistId: sublist, fieldId: "rate", line: i});
const sourceAmount = sourceRec.getSublistValue({sublistId: sublist, fieldId: "amount", line: i});
const sourceGroupSetup = sourceRec.getSublistValue({sublistId: sublist, fieldId: "groupsetup", line: i});
const sourceTaxCode = sourceRec.getSublistValue({sublistId: sublist, fieldId: "taxcode", line: i});
const sourceTaxRate = sourceRec.getSublistValue({sublistId: sublist, fieldId: "taxrate", line: i});
const inGroupVal = sourceRec.getSublistValue({sublistId: sublist, fieldId: "ingroup", line: i});
const isInGroup = ["T",true].includes(inGroupVal);
log.debug(`Processing line ${i} of sublist ${sublist} on source record`, `inGroupVal: ${inGroupVal}, isInGroup: ${isInGroup}`);
if ("EndGroup" == sourceItemType){
log.debug(`Skipping setting or adding EndGroup line from source line ${i}`);
continue;
}else if ("Group" == sourceItemType){
log.debug(`Adding Group source line ${i} as new line to target record`);
targetRec.setSublistValue({sublistId: sublist, fieldId: "item", line: i, value: sourceItem});
targetRec.setSublistValue({sublistId: sublist, fieldId: "quantity", line: i, value: sourceQuantity});
log.debug(`Committed Group line from source line ${i}`, `sourceItem: ${sourceItem}, sourceQuantity: ${sourceQuantity}, sourceAmount: ${sourceAmount}`);
continue;
}else if(isInGroup){
continue;
}else{
log.debug(`Adding Regular source line ${i} as new line to target record`);
//targetRec.selectNewLine({sublistId: sublist});
}
commonFields.forEach(field => {
const sourceItem = sourceRec.getSublistValue({sublistId: sublist, fieldId: "item", line: i});
const sourceItemType = sourceRec.getSublistValue({sublistId: sublist, fieldId: "itemtype", line: i});
if ("EndGroup" == sourceItemType && "amount" == field){
log.debug(`Line ${i} skipping amount field because it's an EndGroup line`);
return;
}
log.debug("Checking sublist field", `Sublist: ${sublist}, Field: ${field}, Line: ${i}`);
const sourceValue = sourceRec.getSublistValue({sublistId: sublist, fieldId: field, line: i});
try{
targetRec.setSublistValue({sublistId: sublist, fieldId: field, line: i, value: sourceValue});
log.debug("syncSublistFields", `Value for sublist ${sublist} field "${field}" from source line ${i} set to (${typeof sourceValue}) ${JSON.stringify(sourceValue)}`);
changes.anyChange = true;
const checkAmount = targetRec.getSublistValue({sublistId: sublist, line: i, fieldId: "amount"});
log.debug(`After setting ${field} to ${sourceValue}, checking amount field for any sourcing changes`, `Source Item (${sourceItemType}): ${sourceItem}, Sublist: ${sublist}, Line: ${i}, Amount Value: ${checkAmount}`);
}catch(syncSublistFieldErr){
log.error(`Error setting value for sublist ${sublist} field ${field} from source line ${i} to (${typeof sourceValue}) ${JSON.stringify(sourceValue)}`);
}
});
}battk
05/27/2026, 7:10 AMbattk
05/27/2026, 7:11 AMbattk
05/27/2026, 7:12 AMbattk
05/27/2026, 7:31 AMbattk
05/27/2026, 7:34 AMbattk
05/27/2026, 7:35 AMAaron McCausland
05/27/2026, 3:26 PMbattk
05/27/2026, 4:45 PMAaron McCausland
05/27/2026, 5:03 PMbattk
05/27/2026, 6:32 PM