CRM 2016 not liking Javascript using if-else - javascript

Can anyone tell me what CRM would hate about this onload function I've created?
It's telling me Form_OnLoad is not defined. Looks defined to me. It's enabled in my form onload, published, etc.
Thank you.
function Form_OnLoad() {
//Calculates total commission for AE1
// Products + Services
if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
var comm1 = (Xrm.Page.getAttribute("new_commissionproductae1").getValue() + Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(comm1);
} else if {
// Products only
(Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionproductae1").getValue());
} else if {
// Services only
(Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
} else {
// Net Sales
(Xrm.Page.getAttribute("new_commissionnetae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
}
}

I believe that's because your JavaScript is not correct. Try to use following code instead of yours:
function Form_OnLoad() { //Calculates total commission for AE1
// Products + Services
if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
var comm1 = Xrm.Page.getAttribute("new_commissionproductae1").getValue() + Xrm.Page.getAttribute("new_commissionserviceae1").getValue();
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(comm1);
} else if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionproductae1").getValue());
} else if (Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
} else if (Xrm.Page.getAttribute("new_commissionnetae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
}
}

Related

Firebase Data validation not matching with data

I have a click button which is validating data from firebase firestore, but it is not giving the correct output as in the checks applied are getting failed.
createAptBtn.onclick = () => {
if (category.value === "" || email.value === "" || day.value === "" || time.value === "") {
promptContent.innerText = "All fields are required."
}
else if (category.value === "New") {
db.collection("recordsDb").onSnapshot((querySnapshot) => {
querySnapshot.forEach((doc) => {
//Condtion 1
if (email.value === doc.data().email && day.value === doc.data().day && time.value === doc.data().time) {
promptContent.innerText = "Email already exists."
}
//Condition 2
if (email.value !== doc.data().email && day.value === doc.data().day && time.value === doc.data().aptTimeSlot) {
promptContent.innerText = "Slot already filled."
}
//Condition 3
if (email.value === doc.data().email || mobile.value === doc.data().mobile) {
promptContent.innerText = "User already exits."
}
// Condition 4
if(email.value !== doc.data().email && day.value !== doc.data().day && time.value !== doc.data().time) {
promptContent.innerText = "Account created."
}
})
})
}
}
Also even when one of the condition is true it still outputs the else statement which should work when all ifs are getting failed.
However, condition 4 is an else statement but since its not working I'm using it as an if statement by adding some checks in it which though fails.
How do I fix this?

Javascript filter between two string dates in Vue component

I'm working with Vue in a Laravel app. Everything below works except the last one. I can't seem to find the right search terms to fit this situation. Sorry if it's a duplicate.
Here is my current code:
return [...this.tableData].filter((salesorders) => {
if (this.selectOption == '6') {
return salesorders.order_status.match(this.status);
}
if (this.selectOption == '1') {
return salesorders.number.includes(this.searchInput);
}
if (this.selectOption == '2' && this.choice == 'is') {
var ship_date = moment(String(this.first_date)).format('MM-DD-YYYY');
return salesorders.requested_ship_date.match(ship_date);
}
if (this.selectOption == '2' && this.choice == 'is not') {
var ship_date = moment(String(this.first_date)).format('MM-DD-YYYY');
return !salesorders.requested_ship_date.match(ship_date);
}
if (this.selectOption == '2' && this.choice == 'is between') {
var ship_date1 = moment(String(this.first_date)).format('MM-DD-YYYY');
var ship_date2 = moment(String(this.end_date)).format('MM-DD-YYYY');
return salesorders.requested_ship_date >= ship_date1 && salesorders.requested_ship_date >= ship_date2;
}
});
I just figured it out:
return salesorders.requested_ship_date >= ship_date1 && salesorders.requested_ship_date <= ship_date2;

Adding conditions to filter() dynamically in Javascript

I want to add conditions in JavaScript filter() method dynamically.
I have the code below:
let condition = '';
let a = ['empEmail', 'employeeAction', 'transactionNo', 'deviceListName', 'projectName'];
if (this.selectedEmployeeAlias != undefined) {
condition += '&& a => a.empEmail === this.selectedEmployeeAlias';
}
if (this.employeeStatusList != undefined) {
condition += '&& a.employeeAction === this.employeeStatusList'
}
if (this.selectedTransactionNo != undefined) {
condition += '&& a.transactionNo === this.selectedTransactionNo';
}
if (this.selectedDeviceList != undefined) {
condition += ' && a.deviceListName == this.selectedDeviceList';
}
if (this.selectedProjectName != undefined) {
condition += '&& a.projectName == this.selectedProjectName';
}
var finalCondition = condition.substring(2, condition.length);
var fArray = arrayDetails.filter(finalCondition);
The code is returning an error as:
finalCondition is not a function.
Could you please let me know how can I add conditions to filter() dynamically.
You could take an array of functions with conditions. Then iterate with every.
var conditions = [];
if (this.selectedEmployeeAlias !== undefined) {
conditions.push(a => a.empEmail === this.selectedEmployeeAlias);
}
if (this.employeeStatusList !== undefined) {
conditions.push(a => a.employeeAction === this.employeeStatusList);
}
if (this.selectedTransactionNo !== undefined) {
conditions.push(a => a.transactionNo === this.selectedTransactionNo);
}
if (this.selectedDeviceList !== undefined) {
conditions.push(a => a.deviceListName == this.selectedDeviceList);
}
if (this.selectedProjectName !== undefined) {
conditions.push(a => a.projectName == this.selectedProjectName);
}
var fArray = arrayDetails.filter(o => conditions.every(c => c(o)));
As you got the nakes of the keys, just loop over them and check for undefineds:
const keys = ['empEmail', 'employeeAction', 'transactionNo', 'deviceListName', 'projectName'];
const result = arrayDetails.filter(el => {
for(const key of keys) {
if(this[key] === undefined) continue;
if(this[key] !== el[key]) return false;
}
return true;
});
eval to the Rescue!
While it's generally advised against, eval does exactly what you want here.
Just pass your condition variable to eval inside the .filter method and voila!
let condition='';
let a = ['empEmail', 'employeeAction', 'transactionNo', 'deviceListName', 'projectName'];
if (this.selectedEmployeeAlias != undefined) {
condition += '&& a => a.empEmail === this.selectedEmployeeAlias';
}
if (this.employeeStatusList != undefined) {
condition += '&& a.employeeAction === this.employeeStatusList'
}
if (this.selectedTransactionNo != undefined) {
condition += '&& a.transactionNo === this.selectedTransactionNo';
}
if (this.selectedDeviceList != undefined) {
condition += ' && a.deviceListName == this.selectedDeviceList';
}
if (this.selectedProjectName != undefined) {
condition += '&& a.projectName == this.selectedProjectName';
}
var finalCondition=condition.substring(2, condition.length);
var fArray=arrayDetails.filter(stuff => eval(finalCondition));

nested if else function in javascript

I am trying to add a barcode verify function to an item picking webapp page and i have the javascript as this:
function barcodeSubmit () {
if (barcode1 != barcode || barcode2 != barcode) {
if (PUT_LPN == "") {
barcodeF.focus();
return false;
}
} else {
if (barcode1 != barcode || barcode2 != barcode) {
if (PUT_LPN != "") {
barcodeF.focus();
return false;
}
}
}
} else {
if (barcode1 == barcode || barcode2 == barcode) {
if (PUT_LPN == "") {
PUT_LPN.focus();
return false;
}
}
}
else {
if (barcode1 == barcode || barcode2 == barcode) {
if (PUT_LPN != "") {
return true;
}
}
}
}
When I submit this form it does not submit and I can not get it to do the function its connected to an onclick input field . It does call when I make it simpler it will execute what I want. I also tried it without the else statements and came out with slightly different result but still not triggering event properly
Use switch instead:
switch(true) {
case ((barcode1 != barcode || barcode2 != barcode) && PUT_LPN == ""):
barcodeF.focus();
return false;
case ((barcode1 != barcode || barcode2 != barcode) && PUT_LPN != ""):
barcodeF.focus();
return false;
case ((barcode1 == barcode || barcode2 == barcode) && PUT_LPN == ""):
PUT_LPN.focus();
return false;
case ((barcode1 == barcode || barcode2 == barcode) && PUT_LPN != ""):
return true;
default:
return true;
}
You can remove default case if not needed
Your code having lot of problems you are using one if and 4 else and some unused conditions also try following:
function barcodeSubmit() {
if (barcode1 != barcode || barcode2 != barcode) {
if (PUT_LPN == "") {
barcodeF.focus();
return false;
} else if (PUT_LPN != "") {
barcodeF.focus();
return false;
}
}
else if (barcode1 == barcode || barcode2 == barcode) {
if (PUT_LPN == "") {
PUT_LPN.focus();
return false;
} else if (PUT_LPN != "") {
return true;
}
}
}
Please try something like following
function barcodeSubmit () {
if (barcode1 != barcode || barcode2 != barcode) {
if (PUT_LPN == "") {
barcodeF.focus();
return false;
}
} else if (barcode1 != barcode || barcode2 != barcode) {
if (PUT_LPN != "") {
barcodeF.focus();
return false;
}
} else if (barcode1 == barcode || barcode2 == barcode) {
if (PUT_LPN == "") {
PUT_LPN.focus();
return false;
}
}else {
if (barcode1 == barcode || barcode2 == barcode) {
if (PUT_LPN != "") {
return true;
}
}
}
}
Problem seems to me is PUT_LPN.focus() and PUT_LPN == "". I assume its and input field and in order to compare value of it you need to use PUT_LPN.value. Again the syntax of if...else is also not valid.
if(condition) {
//
} else if(condition) {
//
} else {
//
}
Also there are lot many repetitive conditions which you need to take care of.
function barcodeSubmit() {
if (barcode1 === barcode || barcode2 === barcode) {
var flag = ('' === PUT_LPN.value);
if (flag)
PUT_LPN.focus();
return !flag;
}
barcodeF.focus();
return false;
}

how to change the default working of SetFocusOnError property of required field validator

the default behavior of setFocusOnError= true set the focus on error field. But issue is that there is a banner on top of the window having fixed position. due to which banner hides the error field. How do I override the default working of setFocusOnError so that there is a margin between banner and the error field?
<script type="text/jscript">
$(function () {
// on first time page load
if (typeof (Page_ClientValidate) != "undefined") {
ValidatorSetFocus = CustomSetFocus;
}
});
function CustomSetFocus(val, event) {
var ctrl;
if (typeof (val.controlhookup) == "string") {
var eventCtrl;
if ((typeof (event) != "undefined") && (event != null)) {
if ((typeof (event.srcElement) != "undefined") && (event.srcElement != null)) {
eventCtrl = event.srcElement;
}
else {
eventCtrl = event.target;
}
}
if ((typeof (eventCtrl) != "undefined") && (eventCtrl != null) &&
(typeof (eventCtrl.id) == "string") &&
(eventCtrl.id == val.controlhookup)) {
ctrl = eventCtrl;
}
}
if ((typeof (ctrl) == "undefined") || (ctrl == null)) {
ctrl = document.getElementById(val.controltovalidate);
}
if ((typeof (ctrl) != "undefined") && (ctrl != null) &&
(ctrl.tagName.toLowerCase() != "table" || (typeof (event) == "undefined") || (event == null)) &&
((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
(typeof (ctrl.disabled) == "undefined" || ctrl.disabled == null || ctrl.disabled == false) &&
(typeof (ctrl.visible) == "undefined" || ctrl.visible == null || ctrl.visible != false) &&
(IsInVisibleContainer(ctrl))) {
if ((ctrl.tagName.toLowerCase() == "table" && (typeof (__nonMSDOMBrowser) == "undefined" || __nonMSDOMBrowser)) ||
(ctrl.tagName.toLowerCase() == "span")) {
var inputElements = ctrl.getElementsByTagName("input");
var lastInputElement = inputElements[inputElements.length - 1];
if (lastInputElement != null) {
ctrl = lastInputElement;
}
}
if (typeof (ctrl.focus) != "undefined" && ctrl.focus != null) {
ctrl.focus();
Page_InvalidControlToBeFocused = ctrl;
var temp1=$(window).scrollTop();
temp1 = temp1 - 150;
$(window).scrollTop(temp1);
}
}
}
</script>

Categories