Creating customer Deposit automatically whenever a sales order is created.
const afterSubmit = (scriptContext) => {
try {
if ((scriptContext.type == scriptContext.UserEventType.CREATE)) {
const total = scriptContext.newRecord.getValue({
fieldId: 'total' // gets the value of the total field
})
if (Number(total) > 0) {
const salesRec = record.load({
type: 'salesorder',
id: scriptContext.newRecord.id,
isDynamic: true
});
const soId = salesRec.id;
const entity = salesRec.getValue({
fieldId: 'entity' // gets the value of the customer field
})
const location = salesRec.getValue({
fieldId: 'location'
});
const currency=salesRec.getValue({
fieldId:'currency'
})
salesRec.setValue({
fieldId: 'paymentmethod',
value: ''
});
salesRec.save();
const customerDeposit = record.create({
type: record.Type.CUSTOMER_DEPOSIT,
isDynamic: true
});
customerDeposit.setValue({
fieldId: 'customer',
value: entity
})
customerDeposit.setValue({
fieldId:'currency',
value:currency
})
customerDeposit.setValue({
fieldId: 'salesorder',
value: soId
})
customerDeposit.setValue({
fieldId: 'location',
value: location
})
customerDeposit.save();
log.debug("customer Deposit", customerDeposit.id);
}
}
} catch (e) {
log.debug("error", e);
}
}
return {afterSubmit}
});
Note:
If we create a sales order in currency other than the primary currency of the customer, by default the script will take the primary currency of the customer as the currency in customer deposit. This will throw an error while running the script. That is why we set the currency in the customer deposit as the same currency in the sales order.