Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

JS - WebApi with Promise function for Work order validation

(0) ShareShare
ReportReport
Posted on by 873

Hello everyone,

I have the below code that has different rules to prevent saving the WO if the user did not completed all his tasks:

var isValidationNeeded = true;
var preventEventSave = true;
var SaveMode = {
    Save: 1,
    SaveAndClose: 2,
    SaveAndNew: 59,
    Autosave: 70
};

function preventSave(executionContext) {

    debugger;

    //getting save mode from event
    var saveMode = executionContext.getEventArgs().getSaveMode();
    var formContext = executionContext.getFormContext();
    var formType = formContext.ui.getFormType();  
    console.log(formType); 

if(formType != 1){


    //if savemode is not one of listed - just quit the execution and let the record to be saved
    if (saveMode !== SaveMode.Save &&
        saveMode !== SaveMode.SaveAndClose &&
        saveMode !== SaveMode.SaveAndNew &&
        saveMode !== SaveMode.Autosave) {
        return;
    }
    if (!isValidationNeeded) {
        isValidationNeeded = true;
        return;
    }

    var ruleThree = false;

    rule3(executionContext).then(function(data) {
        ruleThree = data;
        if (!ruleThree) {
            if (saveMode === SaveMode.Save || saveMode === SaveMode.SaveAndClose) {
                Xrm.Navigation.openAlertDialog({
                    text: "Current work order can't be closed because the incident type is visit and contains products!"
                });
                return;

            }
        } else {
            var boolRule1 = false;
            var formContext = executionContext.getFormContext();
            var status = formContext.getAttribute("msdyn_systemstatus").getValue(); // getting the status the value

            console.log(status);
            // Completed Status
            // status is not close posted
            if (status === 690970004 || status === 690970003) {

                var ruleOne = false;
                var ruleTwo = false;

                if (status === 690970004) {
                    rule1(executionContext).then(function(data) {

                        ruleOne = data;

                        if (!ruleOne) {

                            if (saveMode === SaveMode.Save || saveMode === SaveAndClose) {
                                Xrm.Navigation.openAlertDialog({
                                    text: "Current work order can't be saved because it's linked to canceled agreement!"
                                });
                                return;

                            }

                        } else {
                            isValidationNeeded = false;
                            formContext.data.save().then(function() {
                            }, function(error) {
                                var errorMessage = error.message;
                                console.log(errorMessage);
                            });

                        }
                    }, function(errorMessage) {
                        Xrm.Navigation.openAlertDialog({
                            text: errorMessage
                        });
                    });
                }


                if (status === 690970003) {

                    rule2(executionContext).then(function(data) {

                        ruleTwo = data;
                        if (!ruleTwo) {
                            Xrm.Navigation.openAlertDialog({
                                text: "Current work order can't be closed because not all the service tasks are done!"
                            });
                            return;
                        } else {
                            isValidationNeeded = false;
                            formContext.data.save().then(function() {
                            }, function(error) {
                                var errorMessage = error.message;
                                console.log(errorMessage);
                            });
                        }
                    }, function(errorMessage) {
                        Xrm.Navigation.openAlertDialog({
                            text: errorMessage
                        });
                    });
                }

            } else {
                isValidationNeeded = false;
                console.log(preventEventSave);
                if (preventEventSave) {
                    formContext.data.save().then(function() {
                    }, function(error) {
                        var errorMessage = error.message;
                        console.log(errorMessage);
                    })
                }
            }
        }
    }, function(errorMessage) {
        Xrm.Navigation.openAlertDialog({
            text: errorMessage
        });
    });
}
}
    // if rule1 is passed then check rule3


function rule1(executionContext) { // 1. Prevent the user to close posted the work order if the linked agreement is canceled.
    var formContext = executionContext.getFormContext();
    var agreement = formContext.getAttribute("msdyn_agreement").getValue();
    executionContext.getEventArgs().preventDefault();
    var promiseRule1 = new Promise(function(resolve, reject) {
        var boolRule1 = true;
        if (agreement != null && agreement.length > 0) {
            var id = agreement[0].id;
            var result = Xrm.WebApi.retrieveRecord("msdyn_agreement", id, "?$select=msdyn_systemstatus").then(
                function success(result) {
                    if (result["msdyn_systemstatus"] === 690970003) {
                        //Xrm.Navigation.openAlertDialog({text:"Current work order can't be saved because of Cancelled agreement"});
                        boolRule1 = false;
                    }
                    resolve(boolRule1);
                },
                function(error) {
                    //Xrm.Navigation.openAlertDialog({ text: error.message });
                    reject(error.message);
                }
            );
        } else {
            resolve(boolRule1);
        }
    })
    return promiseRule1;
}

    function rule2(executionContext) { //2.Prevent the user from completing the work order if not all the service tasks are done.
        executionContext.getEventArgs().preventDefault();
        var promiseRule2 = new Promise(function(resolve, reject) {
    
            var boolRule2 = true;
            var serviceTaskValidation = true;
            var formContext = executionContext.getFormContext();
            //get current WO id
            var workOrderId = formContext.data.entity.getId().replace("{", "").replace("}", "");
            //var valInc = formContext.getAttribute("msdyn_primaryincidenttype").getValue();
            var woStatus = formContext.getAttribute("msdyn_systemstatus").getValue();
            var woType = formContext.getAttribute("msdyn_workordertype").getValue();
            //close the WO or not
            //console.log(valInc);
            if (woType != null && woType.length > 0) {
                var woTypeName = woType[0].name;
            }
            if (woStatus != null && woStatus === 690970003 && woTypeName === "Installation" ) { // If the System status is equal to Completed

                //get active related service task
                Xrm.WebApi.retrieveMultipleRecords("msdyn_workorderservicetask", "?$select=msdyn_name,msdyn_percentcomplete,statecode,haceb_responsevalueyesno&$filter=statecode eq 0 and msdyn_workorder/msdyn_workorderid eq "   workOrderId).then(
                    function success(result) {
                        for (var i = 0; i < result.entities.length; i  ) {
    
                            console.log(result.entities[i].msdyn_name);
                            console.log(result.entities[i].msdyn_percentcomplete);
                            console.log(result.entities[i].haceb_responsevalueyesno);
    
                            if (result.entities[i].haceb_responsevalueyesno == false) {
                                serviceTaskValidation = false;
                                break;
                            }
                        }
                        if (!serviceTaskValidation) {
                            boolRule2 = false;
                        }
                        resolve(boolRule2);
                    },
                    function(error) {
                        reject(error.message);
                    }
                )
            }
    
        });
        return promiseRule2;
    }

    function rule3(executionContext) { // Prevent Closing WO if primary incident is visit and WO contains products
        var promiseRule3 = new Promise(function(resolve, reject) {
            var boolRule3 = true;
            var woProdValidation = true;
            var formContext = executionContext.getFormContext();
            var woID = formContext.data.entity.getId().replace("{", "").replace("}", "");
            var valInc = formContext.getAttribute("msdyn_primaryincidenttype").getValue();
    
            if (valInc != null && valInc.length > 0) {
                var name = valInc[0].name;
                if (name == "Visit") {
                    executionContext.getEventArgs().preventDefault();
                    Xrm.WebApi.retrieveMultipleRecords("msdyn_workorderproduct", "?$select=msdyn_name&$filter=msdyn_workorder/msdyn_workorderid eq "   woID   " and statecode eq 0").then(
                        function success(result) {
                            console.log(result);
                            // perform operations on on retrieved records
                            if (result.entities != null && result.entities.length > 0) {
                                woProdValidation = false;
                            } else {
                            }
                            if (!woProdValidation) {
                                boolRule3 = false;
                            }
                            resolve(boolRule3);
                        },
                        function(error) {
                            reject(error.message);
                            // handle error conditions
                        }
                    )
                } else {
                    preventEventSave = false;
                    resolve(boolRule3);
                }
            } else {
                preventEventSave = false;
                resolve(boolRule3);
            }
        });
        return promiseRule3;
    }
  

The issue is only with function Rule2 (Line 167) in the above code which is about (Prevent saving the WO if the Work order type is equal to "Installation" and Wo Status equal to "Completed" and if any of the service task in the same WO is/are not completed using a custom field eq false)


While testing & debugging rule2 for a WO that don't match the above condition the form is saving due to the promise is always returning status "Pending" after the API call and the Web API call is not returning any data.

CodeDebug.PNGProof.gif

I tested the api call in a new tab by using the below query same as the code and it is working fine:

APIResult.PNG

The expected result for Rule2 and for the scenario I tested is to throw the below error message and avoid saving the form:
AsAresult.PNG

The other rules are working perfectly with no issue. It is really strange why Rule2 is not working.

What I am doing wrong? Can please someone check the code and provide the new updated version?

Kindly note that   supported me previously on the same topic but I made a change only for rule2 by adding a condition to check for WOType is eq to installation.

community.dynamics.com/.../plugin---work-order-onupdate-validation

Any help is highly appreciated.

Thank you!

  • EBMRay Profile Picture
    EBMRay 873 on at
    RE: JS - WebApi with Promise function for Work order validation

    Hello ,

    Thank you so much for your assistance.

    I didn't noticed that and now it is working as expected.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JS - WebApi with Promise function for Work order validation

    Hi EBMRay,

    No, this isn't due to a conflict between those two rules. This is because you add another condition(woTypeName === "Installation"). However, when your woTypeName  isn't equal "Installation", the rule2 won't return true. So add the line 212~214 is ok. 

    var isValidationNeeded = true;
    var preventEventSave = true;
    var SaveMode = {
        Save: 1,
        SaveAndClose: 2,
        SaveAndNew: 59,
        Autosave: 70
    };
    
    function preventSave(executionContext) {
    
        debugger;
    	
    
        //getting save mode from event
        var saveMode = executionContext.getEventArgs().getSaveMode();
        var formContext = executionContext.getFormContext();
        var formType = formContext.ui.getFormType();  
        console.log(formType); 
    
    if(formType != 1){
    
    
        //if savemode is not one of listed - just quit the execution and let the record to be saved
        if (saveMode !== SaveMode.Save &&
            saveMode !== SaveMode.SaveAndClose &&
            saveMode !== SaveMode.SaveAndNew &&
            saveMode !== SaveMode.Autosave) {
            return;
        }
        
        if (!isValidationNeeded) {
            isValidationNeeded = true;
            return;
        }
    	executionContext.getEventArgs().preventDefault();
    
        var ruleThree = false;
    
        rule3(executionContext).then(function(data) {
            ruleThree = data;
            if (!ruleThree) {
                if (saveMode === SaveMode.Save || saveMode === SaveMode.SaveAndClose) {
                    Xrm.Navigation.openAlertDialog({
                        text: "Current work order can't be closed because the incident type is visit and contains products!"
                    });
                    return;
    
                }
            } else {
                var boolRule1 = false;
                var formContext = executionContext.getFormContext();
                var status = formContext.getAttribute("msdyn_systemstatus").getValue(); // getting the status the value
    
                console.log(status);
                // Completed Status
                // status is not close posted
                if (status === 690970004 || status === 690970003) {
    
                    var ruleOne = false;
                    var ruleTwo = false;
    
                    if (status === 690970004) {
                        rule1(executionContext).then(function(data) {
    
                            ruleOne = data;
    
                            if (!ruleOne) {
    
                                if (saveMode === SaveMode.Save || saveMode === SaveAndClose) {
                                    Xrm.Navigation.openAlertDialog({
                                        text: "Current work order can't be saved because it's linked to canceled agreement!"
                                    });
                                    return;
    
                                }
    
                            } else {
                                isValidationNeeded = false;
                                formContext.data.save().then(function() {
                                }, function(error) {
                                    var errorMessage = error.message;
                                    console.log(errorMessage);
                                });
    
                            }
                        }, function(errorMessage) {
                            Xrm.Navigation.openAlertDialog({
                                text: errorMessage
                            });
                        });
                    }
    
    
                    if (status === 690970003) {
    
                        rule2(executionContext).then(function(data) {
    
                            ruleTwo = data;
                            if (!ruleTwo) {
                                Xrm.Navigation.openAlertDialog({
                                    text: "Current work order can't be closed because not all the service tasks are done!"
                                });
                                return;
                            } else {
                                isValidationNeeded = false;
                                formContext.data.save().then(function() {
                                }, function(error) {
                                    var errorMessage = error.message;
                                    console.log(errorMessage);
                                });
                            }
                        }, function(errorMessage) {
                            Xrm.Navigation.openAlertDialog({
                                text: errorMessage
                            });
                        });
                    }
    
                } else {
                    isValidationNeeded = false;
                    console.log(preventEventSave);
                    if (preventEventSave) {
                        formContext.data.save().then(function() {
                        }, function(error) {
                            var errorMessage = error.message;
                            console.log(errorMessage);
                        })
                    }
                }
            }
        }, function(errorMessage) {
            Xrm.Navigation.openAlertDialog({
                text: errorMessage
            });
        });
    }
    }
        // if rule1 is passed then check rule3
    
    
    function rule1(executionContext) { // 1. Prevent the user to close posted the work order if the linked agreement is canceled.
        var formContext = executionContext.getFormContext();
        var agreement = formContext.getAttribute("msdyn_agreement").getValue();
        
        var promiseRule1 = new Promise(function(resolve, reject) {
            var boolRule1 = true;
            if (agreement != null && agreement.length > 0) {
                var id = agreement[0].id;
                var result = Xrm.WebApi.retrieveRecord("msdyn_agreement", id, "?$select=msdyn_systemstatus").then(
                    function success(result) {
                        if (result["msdyn_systemstatus"] === 690970003) {
                            //Xrm.Navigation.openAlertDialog({text:"Current work order can't be saved because of Cancelled agreement"});
                            boolRule1 = false;
                        }
                        resolve(boolRule1);
                    },
                    function(error) {
                        //Xrm.Navigation.openAlertDialog({ text: error.message });
                        reject(error.message);
                    }
                );
            } else {
                resolve(boolRule1);
            }
        })
        return promiseRule1;
    }
    
        function rule2(executionContext) { //2.Prevent the user from completing the work order if not all the service tasks are done.
            
            var promiseRule2 = new Promise(function(resolve, reject) {
        
                var boolRule2 = true;
                var serviceTaskValidation = true;
                var formContext = executionContext.getFormContext();
                //get current WO id
                var workOrderId = formContext.data.entity.getId().replace("{", "").replace("}", "");
                //var valInc = formContext.getAttribute("msdyn_primaryincidenttype").getValue();
                var woStatus = formContext.getAttribute("msdyn_systemstatus").getValue();
                var woType = formContext.getAttribute("msdyn_workordertype").getValue();
                //close the WO or not
                //console.log(valInc);
                if (woType != null && woType.length > 0) {
                    var woTypeName = woType[0].name;
                }
                if (woStatus != null && woStatus === 690970003 && woTypeName === "Installation" ) { // If the System status is equal to Completed
    
                    //get active related service task
                    Xrm.WebApi.retrieveMultipleRecords("msdyn_workorderservicetask", "?$select=msdyn_name,msdyn_percentcomplete,statecode,haceb_responsevalueyesno&$filter=statecode eq 0 and msdyn_workorder/msdyn_workorderid eq "   workOrderId).then(
                        function success(result) {
                            for (var i = 0; i < result.entities.length; i  ) {
        
                                console.log(result.entities[i].msdyn_name);
                                console.log(result.entities[i].msdyn_percentcomplete);
                                console.log(result.entities[i].haceb_responsevalueyesno);
        
                                if (result.entities[i].haceb_responsevalueyesno == false) {
                                    serviceTaskValidation = false;
                                    break;
                                }
                            }
                            if (!serviceTaskValidation) {
                                boolRule2 = false;
                            }
                            resolve(boolRule2);
                        },
                        function(error) {
                            reject(error.message);
                        }
                    )
                }else{
    				resolve(boolRule2);
    			}
        
            });
            return promiseRule2;
        }
    
        function rule3(executionContext) { // Prevent Closing WO if primary incident is visit and WO contains products
            var promiseRule3 = new Promise(function(resolve, reject) {
                var boolRule3 = true;
                var woProdValidation = true;
                var formContext = executionContext.getFormContext();
                var woID = formContext.data.entity.getId().replace("{", "").replace("}", "");
                var valInc = formContext.getAttribute("msdyn_primaryincidenttype").getValue();
        
                if (valInc != null && valInc.length > 0) {
                    var name = valInc[0].name;
                    if (name == "Visit") {
                        
                        Xrm.WebApi.retrieveMultipleRecords("msdyn_workorderproduct", "?$select=msdyn_name&$filter=msdyn_workorder/msdyn_workorderid eq "   woID   " and statecode eq 0").then(
                            function success(result) {
                                console.log(result);
                                // perform operations on on retrieved records
                                if (result.entities != null && result.entities.length > 0) {
                                    woProdValidation = false;
                                } else {
                                }
                                if (!woProdValidation) {
                                    boolRule3 = false;
                                }
                                resolve(boolRule3);
                            },
                            function(error) {
                                reject(error.message);
                                // handle error conditions
                            }
                        )
                    } else {
                        preventEventSave = false;
                        resolve(boolRule3);
                    }
                } else {
                    preventEventSave = false;
                    resolve(boolRule3);
                }
            });
            return promiseRule3;
        }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JS - WebApi with Promise function for Work order validation

    Hi EBMRay,

    I would test and update it later.

  • EBMRay Profile Picture
    EBMRay 873 on at
    RE: JS - WebApi with Promise function for Work order validation

    Hi  ,

    While testing the code today, I noticed that the form is preventing saving if the Primary incident type is visit and the WO status is completed due to preventDefault knowing that it should save with no issue since the data is already validated.
    I think that the executionContext.getEventArgs().preventDefault(); at line 37 is stopping the form from being saved.

    Since the primary incident type is Visit we already have a rule3 that should be checked and since we have the work order status equal to completed, rule2 will be called and checked.

    So maybe there is a conflict between those two rules after calling their web apis?

    I didn't make any changes to the code, please try to replicate the same scenario that I did:

    Select the primary incident as Visit, and the System status as Completed, make any other changes to the form and try to save. (The form will stay unsaved).

    Below is proof while debugging:
    2605.Proof.gif

    Please let me know if you need any further information.

    I look forward to your response on what should be changed in the code.

    Thank you!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JS - WebApi with Promise function for Work order validation

    Hi EBMRay,

    If it is very relevant to the previous topic like this topic, you could just post there.

    Otherwise, raise a new topic would be a better way.

  • EBMRay Profile Picture
    EBMRay 873 on at
    RE: JS - WebApi with Promise function for Work order validation

    Hi ,

    As usual, your assistance is highly appreciated.

    I noticed that the Auto-save on the production environment was on so I turn it off.

    In addition, even if I keep the autosave on the script is working but due to autosave behavior, the script keeps on calling over and over again so that's why I disable it.

    I just have a concern, If I have a query that is similar to a previous topic can I use the topic? or I should always raise a new topic?

    Thank you!

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JS - WebApi with Promise function for Work order validation

    Hi EBMRay,

    It seems that executionContext.getEventArgs().preventDefault(); won't prevent the form autosave. So I put this line at the beginning and it can work for me now. You could have a try:

    var isValidationNeeded = true;
    var preventEventSave = true;
    var SaveMode = {
        Save: 1,
        SaveAndClose: 2,
        SaveAndNew: 59,
        Autosave: 70
    };
    
    function preventSave(executionContext) {
    
        debugger;
    	
    
        //getting save mode from event
        var saveMode = executionContext.getEventArgs().getSaveMode();
        var formContext = executionContext.getFormContext();
        var formType = formContext.ui.getFormType();  
        console.log(formType); 
    
    if(formType != 1){
    
    
        //if savemode is not one of listed - just quit the execution and let the record to be saved
        if (saveMode !== SaveMode.Save &&
            saveMode !== SaveMode.SaveAndClose &&
            saveMode !== SaveMode.SaveAndNew &&
            saveMode !== SaveMode.Autosave) {
            return;
        }
        
        if (!isValidationNeeded) {
            isValidationNeeded = true;
            return;
        }
    	executionContext.getEventArgs().preventDefault();
    
        var ruleThree = false;
    
        rule3(executionContext).then(function(data) {
            ruleThree = data;
            if (!ruleThree) {
                if (saveMode === SaveMode.Save || saveMode === SaveMode.SaveAndClose) {
                    Xrm.Navigation.openAlertDialog({
                        text: "Current work order can't be closed because the incident type is visit and contains products!"
                    });
                    return;
    
                }
            } else {
                var boolRule1 = false;
                var formContext = executionContext.getFormContext();
                var status = formContext.getAttribute("msdyn_systemstatus").getValue(); // getting the status the value
    
                console.log(status);
                // Completed Status
                // status is not close posted
                if (status === 690970004 || status === 690970003) {
    
                    var ruleOne = false;
                    var ruleTwo = false;
    
                    if (status === 690970004) {
                        rule1(executionContext).then(function(data) {
    
                            ruleOne = data;
    
                            if (!ruleOne) {
    
                                if (saveMode === SaveMode.Save || saveMode === SaveAndClose) {
                                    Xrm.Navigation.openAlertDialog({
                                        text: "Current work order can't be saved because it's linked to canceled agreement!"
                                    });
                                    return;
    
                                }
    
                            } else {
                                isValidationNeeded = false;
                                formContext.data.save().then(function() {
                                }, function(error) {
                                    var errorMessage = error.message;
                                    console.log(errorMessage);
                                });
    
                            }
                        }, function(errorMessage) {
                            Xrm.Navigation.openAlertDialog({
                                text: errorMessage
                            });
                        });
                    }
    
    
                    if (status === 690970003) {
    
                        rule2(executionContext).then(function(data) {
    
                            ruleTwo = data;
                            if (!ruleTwo) {
                                Xrm.Navigation.openAlertDialog({
                                    text: "Current work order can't be closed because not all the service tasks are done!"
                                });
                                return;
                            } else {
                                isValidationNeeded = false;
                                formContext.data.save().then(function() {
                                }, function(error) {
                                    var errorMessage = error.message;
                                    console.log(errorMessage);
                                });
                            }
                        }, function(errorMessage) {
                            Xrm.Navigation.openAlertDialog({
                                text: errorMessage
                            });
                        });
                    }
    
                } else {
                    isValidationNeeded = false;
                    console.log(preventEventSave);
                    if (preventEventSave) {
                        formContext.data.save().then(function() {
                        }, function(error) {
                            var errorMessage = error.message;
                            console.log(errorMessage);
                        })
                    }
                }
            }
        }, function(errorMessage) {
            Xrm.Navigation.openAlertDialog({
                text: errorMessage
            });
        });
    }
    }
        // if rule1 is passed then check rule3
    
    
    function rule1(executionContext) { // 1. Prevent the user to close posted the work order if the linked agreement is canceled.
        var formContext = executionContext.getFormContext();
        var agreement = formContext.getAttribute("msdyn_agreement").getValue();
        
        var promiseRule1 = new Promise(function(resolve, reject) {
            var boolRule1 = true;
            if (agreement != null && agreement.length > 0) {
                var id = agreement[0].id;
                var result = Xrm.WebApi.retrieveRecord("msdyn_agreement", id, "?$select=msdyn_systemstatus").then(
                    function success(result) {
                        if (result["msdyn_systemstatus"] === 690970003) {
                            //Xrm.Navigation.openAlertDialog({text:"Current work order can't be saved because of Cancelled agreement"});
                            boolRule1 = false;
                        }
                        resolve(boolRule1);
                    },
                    function(error) {
                        //Xrm.Navigation.openAlertDialog({ text: error.message });
                        reject(error.message);
                    }
                );
            } else {
                resolve(boolRule1);
            }
        })
        return promiseRule1;
    }
    
        function rule2(executionContext) { //2.Prevent the user from completing the work order if not all the service tasks are done.
            
            var promiseRule2 = new Promise(function(resolve, reject) {
        
                var boolRule2 = true;
                var serviceTaskValidation = true;
                var formContext = executionContext.getFormContext();
                //get current WO id
                var workOrderId = formContext.data.entity.getId().replace("{", "").replace("}", "");
                //var valInc = formContext.getAttribute("msdyn_primaryincidenttype").getValue();
                var woStatus = formContext.getAttribute("msdyn_systemstatus").getValue();
                var woType = formContext.getAttribute("msdyn_workordertype").getValue();
                //close the WO or not
                //console.log(valInc);
                if (woType != null && woType.length > 0) {
                    var woTypeName = woType[0].name;
                }
                if (woStatus != null && woStatus === 690970003 && woTypeName === "Installation" ) { // If the System status is equal to Completed
    
                    //get active related service task
                    Xrm.WebApi.retrieveMultipleRecords("msdyn_workorderservicetask", "?$select=msdyn_name,msdyn_percentcomplete,statecode,haceb_responsevalueyesno&$filter=statecode eq 0 and msdyn_workorder/msdyn_workorderid eq "   workOrderId).then(
                        function success(result) {
                            for (var i = 0; i < result.entities.length; i  ) {
        
                                console.log(result.entities[i].msdyn_name);
                                console.log(result.entities[i].msdyn_percentcomplete);
                                console.log(result.entities[i].haceb_responsevalueyesno);
        
                                if (result.entities[i].haceb_responsevalueyesno == false) {
                                    serviceTaskValidation = false;
                                    break;
                                }
                            }
                            if (!serviceTaskValidation) {
                                boolRule2 = false;
                            }
                            resolve(boolRule2);
                        },
                        function(error) {
                            reject(error.message);
                        }
                    )
                }
        
            });
            return promiseRule2;
        }
    
        function rule3(executionContext) { // Prevent Closing WO if primary incident is visit and WO contains products
            var promiseRule3 = new Promise(function(resolve, reject) {
                var boolRule3 = true;
                var woProdValidation = true;
                var formContext = executionContext.getFormContext();
                var woID = formContext.data.entity.getId().replace("{", "").replace("}", "");
                var valInc = formContext.getAttribute("msdyn_primaryincidenttype").getValue();
        
                if (valInc != null && valInc.length > 0) {
                    var name = valInc[0].name;
                    if (name == "Visit") {
                        
                        Xrm.WebApi.retrieveMultipleRecords("msdyn_workorderproduct", "?$select=msdyn_name&$filter=msdyn_workorder/msdyn_workorderid eq "   woID   " and statecode eq 0").then(
                            function success(result) {
                                console.log(result);
                                // perform operations on on retrieved records
                                if (result.entities != null && result.entities.length > 0) {
                                    woProdValidation = false;
                                } else {
                                }
                                if (!woProdValidation) {
                                    boolRule3 = false;
                                }
                                resolve(boolRule3);
                            },
                            function(error) {
                                reject(error.message);
                                // handle error conditions
                            }
                        )
                    } else {
                        preventEventSave = false;
                        resolve(boolRule3);
                    }
                } else {
                    preventEventSave = false;
                    resolve(boolRule3);
                }
            });
            return promiseRule3;
        }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JS - WebApi with Promise function for Work order validation

    Hi EBMRay,

    I'm testing it and would update it later.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans