Set Field Error Notification using JavaScript by checking related parent entity record info in Dataverse Model driven app power apps
Sometimes you need to show error against form fields if some field value of parent record not met the required value. You can use below script and modify for your project.
function setFieldMandatoryOnType(executionContext)
{
var formContext = executionContext.getFormContext();
var transactionType = formContext.getAttribute("hsbc_type").getValue();
if(transactionType == 768280000 || transactionType == 768280001)
{
formContext.getAttribute("hsbc_amount").setRequiredLevel("required");
formContext.getAttribute("hsbc_bankaccount").setRequiredLevel("required");
formContext.getAttribute("hsbc_customer").setRequiredLevel("required");
}
else
{
formContext.getAttribute("hsbc_amount").setRequiredLevel("none");
formContext.getAttribute("hsbc_bankaccount").setRequiredLevel("none");
formContext.getAttribute("hsbc_customer").setRequiredLevel("none");
}
var baccount = formContext.getAttribute("hsbc_bankaccount").getValue();
var bankaccid = baccount[0].id.replace("{","").replace("}","");
alert(bankaccid);
// check balance and show error
Xrm.WebApi.retrieveRecord("hsbc_hsbcbankaccount", bankaccid, "?$select=hsbc_balance").then(
function success(result) {
var notiID = "balancemsg";
alert(result.hsbc_balance);
if(result.hsbc_balance <= 0 && transactionType == 768280000 )//for debit
{
formContext.getControl("hsbc_type").setNotification("You cannot create Debit transaction as the Balnacce is ZERO",notiID);
}
else
{
formContext.getControl("hsbc_type").clearNotification(notiID);
}
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
}
Hope this helps.