cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Sahil
Helper I
Helper I

How to call Portal Web API (GET) within Page_Validator.evaluationfunction ?

Hi Folks,

 

I need to place some validations on the Submit button of a basic form based on a lookup field. I am trying to call Portal GET API within the Page_Validator_Evaluation_function on a basic form. The web API call is asynchronous so by the time it returns a value, the control on the Parent function i.e. newvalidator.evaluationfunction() is gone and the returned value can't be accepted by the parent function from the child function (Web API). 

 

Is there any way that the system/control can wait until the completion of child fucntion that is executing the web api and only proceeds further when a returned value is received from the child function?

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
eugenevanstaden
Resolver III
Resolver III

@Sahil 

The 2nd approach would work then, you basically replace the Submit button with a dummy from which you execute the API call. Once the request is resolved you trigger the submit button. 

View solution in original post

5 REPLIES 5
eugenevanstaden
Resolver III
Resolver III

@Sahil 

You can use this function

 

		function entityFormClientValidate() {
			// Custom client side validation. Method is called by the submit button's onclick event.
			// Must return true or false. Returning false will prevent the form from submitting.
			return true;
		}			
	

 

 You can override it in your form or you can change the Submit function from the form with Javascript, eg.

        let nextButton = $("#NextButton");
        if ($(nextButton).length) {
            $(nextButton).hide();
            $(nextButton).before("<button type='button' name='ApiSaveButton' value= 'Save and continue' id = 'ApiSaveButton' class='btn btn-default' nonactionlinkbutton='true'>Save and continue</button>");
        }



$("#ApiSaveButton").mousedown(function (e) {


	//Do your API calls here

	$("#NextButton").trigger("click");

});

 @eugenevanstaden Thanks for the response. I believe with the first approach, I might end up into the same problem wherein the Web API function would be waiting to execute and meanwhile, the control will exit from entityFormClientValidate.  Let me try both of the suggested approaches and get back with an update.

eugenevanstaden
Resolver III
Resolver III

@Sahil 

The 2nd approach would work then, you basically replace the Submit button with a dummy from which you execute the API call. Once the request is resolved you trigger the submit button. 

@eugenevanstaden 

 

I tried to use the second approach, there is 1 issue -  I have a global variable (as shown in the code below) that is either set to true or false based on the result returned by the web API. I have a page validator evaluation function that is executed based on the  submit event of original Submit button and that returns the value of this global variable. For some reason, the default value of the global variable, i.e. true, is always returned. Below is the complete code. How can I return the value of this variable that has been set during the click event of Save and Continue button ?

 

 

$(document).ready(function () {
   let isPNNotRequired = true;

	let nextButton = $("#InsertButton");
        if ($(nextButton).length) {
            $(nextButton).hide();
            $(nextButton).before("<button type='button' name='ApiSaveButton' value= 'Save and continue' id = 'ApiSaveButton' class='btn btn-default btn-primary submit-btn' nonactionlinkbutton='true'>Save and continue</button>");
        }
	setPageValidator();

    $("#ApiSaveButton").bind("click", function () {
		
		 webapi.safeAjax({
			type: "GET",
			url: OdataUrl,
			contentType: "application/json",
			async: false,
			success: function (data, textStatus, xhr) {
				console.log(data);
				if (data != null && data["new_location"] == 2) {
					
					if ($("#new_pn").val() == "") {
						isPNNotRequired = false;
					}
					else{
						isPNNotRequired = true;
					}
				}
				$("#InsertButton").trigger("click");
			},
			error: function (jqXHR, textStatus, errorThrown) {
				alert("error");
			}
    });
		});
    
    });
    function setPageValidator() {

        if (typeof (Page_Validators) == 'undefined') return;
    
        // Create new validator
        var newValidator = document.createElement('span');
        newValidator.style.display = "none";
        newValidator.id = "pnValidator";
        newValidator.controltovalidate = "new_pn";
        newValidator.errormessage = "<a href='#new_pn_label'>Please enter pn</a>";
        newValidator.validationGroup = ""; // Set this if you have set ValidationGroup on the form
        newValidator.initialvalue = "";
        
        newValidator.evaluationfunction = function () {
            return isPNNotRequired;
        };
        Page_Validators.push(newValidator);
        $("a[href='#']").on("click", function () { scrollToAndFocus('new_pn_label', 'new_pn'); });
                
    }

 

 

@eugenevanstaden  I found the mistake and removed the default value of variable. It seems to work fine now. Thanks much!

 let isPNNotRequired;

 

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

MPP IDEAS updated 768x460.png

Ideas

Discover ideas and concepts from users like you for how to use Power Pages and take your work to the next level.

Users online (4,214)