Hi Farhan,
Please check below:
function adjustmentInvoiceApproveAmount(executionContext) {
try {
// Get the form context
var formContext = executionContext.getFormContext();
var saveEvent = executionContext.getEventArgs();
var adjustmentamount = formContext.getAttribute("case_adjustmentamount").getValue();
var amountdue = formContext.getAttribute("case_amountdue").getValue();
var stopSave=false;
// //Check Condition before checking whether it is credit or debit
// if (adjustmentamount >= amountdue) {
// saveEvent.preventDefault();
// }
// Get ajustment GUID
var adjustment = formContext.getAttribute("case_adjustment").getValue();
if (adjustment != null) {
var adjustmentGUID = adjustment[0].id.substring(1, 37);
}
console.log("GUID \"case_adjustment\" = " + adjustmentGUID + " ; " + typeof adjustmentGUID);
//WebApi call under adj primary entity (adjustment) > adjtype > expand AdjustmentType > get case_name value = Credit Notes
Xrm.WebApi.retrieveMultipleRecords("case_adjustment", "?$select=_case_adjustmenttype_value&$expand=case_AdjustmentType($select=case_name)&$filter=case_adjustmentid eq " + adjustmentGUID).then(
function success(results) {
debugger
for (var i = 0; i < results.entities.length; i++) {
console.log(results.entities[i]);
var _case_adjustmenttype_value = results.entities[i]["_case_adjustmenttype_value"];
console.log("case_name= " + _case_adjustmenttype_value);
var casename = results.entities[i]["case_AdjustmentType"]["case_name"];
console.log("case_name= " + casename);
if (casename == "Credit notes") {
if (adjustmentamount >= amountdue) {
//alert("Adjustment Amount cannot be more than Invoice Amount.");
stopSave=true;
}
else {
console.log("This can be save");
stopSave=false;
//parent.Xrm.Page.data.entity.save();
}
}
else {
//only here cannot be use
console.log("This is debit notes, can save");
//parent.Xrm.Page.data.entity.save();
}
}
if(stopSave)
{
alert("Adjustment Amount cannot be more than Invoice Amount.");
saveEvent.preventDefault();
}
},
function (error) {
Xrm.Utility.alertDialog(error.message);
}
);
} catch (error) {
console.log(error);
}
}
In Above code, i have commented the red color code part which we need to remove from code. and Added the green color code.
Now, if we read above code then it will work like below:
1. Retrieve the Case Adjustment Type
2. Check if casename == "Credit Notes" the set stopSave to true
3. Else stopSave to false.
4. After loop we have checked the stopSave field value. if true then PreventSave else proceed save.
Thanks,
Pradeep.
Please mark this as VERIFIED if it helps.