Hi everyone, I hope you are all well. Our Dynamics 365 CRM just updated to the new unified platform and now one of the scripts that used to run flawlessly for the last 5 years no longer works.
I have a simple cost calculator setup in our opportunity screens that calculates sell price or Gross profit margin based on a selection the user makes.
When you execute the script the error Value should be of type: boolean pops up as an error.
I didn't write the script, it was setup years ago and I have no idea what to do to fix this issue.
Below is a copy of the script that runs. Can anyone help? Much appreciated!
function CalculateRevenue(button,calculation,costprice,sellprice,gpmpercent) {
Xrm.Page.data.entity.save();
var recalculate = Xrm.Page.data.entity.attributes.get(button);
recalculate.setValue(0);
var cost = Xrm.Page.data.entity.attributes.get(costprice).getValue();
var calc = Xrm.Page.data.entity.attributes.get(calculation).getValue();
if (calc > 0) {
var revenue = Xrm.Page.data.entity.attributes.get(sellprice);
var gpm = Xrm.Page.data.entity.attributes.get(gpmpercent).getValue();
revenue.setValue(0);
if (gpm < 99.99) {
revenue.setValue(cost/(1-(gpm*.01)));
}
else {
alert ("*** to set GPM to 100% set COST to Kinetix to 0");
}
}
if (calc < 1) {
var gpm = Xrm.Page.data.entity.attributes.get(gpmpercent);
var revenue = Xrm.Page.data.entity.attributes.get(sellprice).getValue();
gpm.setValue(0);
if (cost > revenue) {
alert ("***ERROR COST to kinetix is greater than SELL PRICE");
}
else {
gpm.setValue(((revenue-cost)/revenue) *100);
}
}
}
As you wish Joe, but doing it now would mitigate a mid/long-term risk with the current script
Hi Henry, Thanks for the quick response! I haven't tried this out yet as the above answer from Leah fixed my problem. If I run into issues I will give your answer a try.
Thank you!
Joe Muscella
Leah,
Thanks so much , this worked perfectly! I really appreciate your quick response!
Joe Muscella
Hello Joe -
There are other issues in your script that will need refactoring, such as the use of Xrm.Page that is deprecated (or else this could break at a later point in time).
We have created an update of your code, can you try to use it instead?
function CalculateRevenue(executionContext, button, calculation, costprice, sellprice, gpmpercent) { var formContext = executionContext.getFormContext(); formContext.data.save().then( function () { formContext.getAttribute(button).setValue(false); var cost = formContext.getAttribute(costprice).getValue(); var calc = formContext.getAttribute(calculation).getValue(); if (calc > 0) { var revenueAttr = formContext.getAttribute(sellprice); var gpmAttr = formContext.getAttribute(gpmpercent).getValue(); revenueAttr.setValue(0); if (gpmAttr < 99.99) { revenueAttr.setValue(cost / (1 - (gpmAttr * .01))); } else { Xrm.Navigation.openAlertDialog({ title: "Revenue calculation", text: "*** to set GPM to 100% set COST to Kinetix to 0" }); } } else if (calc < 1) { var gpmAttr = formContext.getAttribute(gpmpercent); var revenue = formContext.getAttribute(sellprice).getValue(); gpmAttr.setValue(0); if (cost > revenue) { Xrm.Navigation.openAlertDialog({ title: "Revenue calculation", text: "***ERROR COST to kinetix is greater than SELL PRICE" }); } else { gpmAttr.setValue(((revenue - cost) / revenue) * 100); } } }, function (error) { Xrm.Navigation.openErrorDialog(error); } ); }
Also, please make sure that the context is passed to your form function, in the Event Handlers
Henry
Hi Joe,
The Boolean type error appears in the field that data type is ‘Twop options’,
The D365 legacy Xrm.Page field used to accept 0 for the false value,but UCI require false instead of 0 (zero).
So you need change js code:
recalculate.setValue(0); should changed to recalculate.setValue(false);
also, you can check all fields that exsit in js, if the data type is two options, you need use ture/false to set it’s value.
Regards,
Leah Ju
Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,228 Super User 2024 Season 2
Martin Dráb 230,056 Most Valuable Professional
nmaenpaa 101,156