Hi guys,
I am new in Javascript and D365. My objective is : When adjustment type = Credit notes it will triggered the JS in adjustment invoice (adjustment amount >= invoice amount) alert will prompt, and prevent the adjustment invoice form from save. For adjustment type = debit notes, it will save no mater what condition of the adj amount / invoice amount.
My issue was when i choose adjustment type = debit notes , it triggered the preventDefault. How can i disable it for the debit notes. I have included my code for review. Thank you so much to this community.
Regards,
Farhan
//Issue: On Debit notes when adjamount > invoice it should be save. But its not. 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(); //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."); } else { console.log("This can be save"); 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(); } } }, function (error) { Xrm.Utility.alertDialog(error.message); } ); } catch (error) { console.log(error); } }
Hi @Farhan,
Please try below code:
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(); } else{ formContext.data.entity.save(); } }, function (error) { Xrm.Utility.alertDialog(error.message); } ); saveEvent.preventDefault(); } catch (error) { console.log(error); }
In above code i made small changes added prevent default inside the try block at LINE 112.
So, When above code executed it will not save the record. Once WebAPI response return then only it will save the record.
Thanks,
Pradeep.
Please mark this as VERIFIED if it helps.
Hi @Pradeep Rai,
Your code almost correct but for situation "credit notes > triggered alert > prevent save" , it saved on my side. Other condition is good except that one. The alert message is triggering but it save i assume it auto save for whole page.
Hi Farhan,
Condition to compare Adjustment Amount and Amount Due at line #12 should me pushed inside the retrieve multiple success function where Adjustment Type is checking. Please check if the below code can help to address your issue.
On a side note, you can replace retrieveMultiple with retrieveRecord function as you already know the GUID of the record to be retrieved.
var adjustmentamount, amountdue; function adjustmentInvoiceApproveAmount(executionContext) { try { // Get the form context var formContext = executionContext.getFormContext(); var saveEvent = executionContext.getEventArgs(); adjustmentamount = formContext.getAttribute("case_adjustmentamount").getValue(); amountdue = formContext.getAttribute("case_amountdue").getValue(); // 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); Xrm.WebApi.retrieveMultipleRecords("case_adjustment", "?$select=_case_adjustmenttype_value&$expand=case_AdjustmentType($select=case_name)&$filter=case_adjustmentid eq " adjustmentGUID).then( function success(results) { 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" && adjustmentamount >= amountdue) { alert("Adjustment Amount cannot be more than Invoice Amount."); saveEvent.preventDefault(); } else { console.log("This can be save"); parent.Xrm.Page.data.entity.save(); } } }, function (error) { Xrm.Utility.alertDialog(error.message); } ); } catch (error) { console.log(error); } }
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.
Daivat Vartak (v-9d...
225
Super User 2025 Season 1
Muhammad Shahzad Sh...
106
Most Valuable Professional
Eugen Podkorytov
102