How To: Button used global variable or function

I have a variable counter (question) that I tried to make global. I have button (Button0) that I want to increment the counter and then call a function. I get a message that the “question” variable is not defined. I defined the counter and function both inside and outside of fd.spRendered but neither worked.

How can I have a button use a variable or call a function?

fd.spRendered(function() {
var question = 0;

function populateQuestion(qst) {
    //do something
}

fd.control("Button0").onclick = "question += 1; populateQuestion(question);";

});

Dear Marion,

You can define the variable in the window scope like this: “window.question”.

2 Likes

Hi @AlexZver
I am trying to implement a similar scenario. I want the button to execute a function in Javascript but the console states that the function was not defined.
Could you please guide me how I need to define the function so it is accessable to the button?
Below is my shortened code.

fd.spRendered(function() {
window.fd = fd;
});

function myFunction() {
console.log('myFunction triggerred');
}

And my button settings:
image

Hello @Andre,

To make a function global, you need to add the function to the "window" object like below.

window.myFunction = function() {
console.log('myFunction triggerred');
}

fd.spRendered(function() {

});

Thank you @mnikitina
This was exactly what I needed.

1 Like

I haven't quite followed this.

I want to define a global variable.

I need to use

var window.myVariable = 'some text'

correct?

Do I put it before or after this:

fd.rendered(function(){

What else is needed?

thanks

Hello @mrpowergage,

You need to define a variable before the rendered event like this:

window.myVariable = 'some text';

fd.rendered(function(){

});
1 Like

Thanks,

I need the variable to remain the same after the form is cleared (I want to preserve the email for the next entry on the form). Is this the right method to do this? I can't get it to work.

Hello @mrpowergage,

Could you please describe the use case so I would suggest the best option.

Hi,

Is it possible to run a function from a button to check if a name exists in a list from value entered in a field?

if a user types in a UPN in the field below, I want them to click a button which will check if the name exists in a list:

//Function will send Pnp request to search for value in list and let user know if name exists in list
window.val = function validateCC(value){
    pnp.sp.web.lists.getByTitle("POWERBiV2").items.filter("User_Principle_Name eq '" + value + "'").get().then(function(items){
           if (fd.field('User_Principle_Name').value.items.length > 0){
           alert("This user already exists, please edit this record below.");
           $($(fd.field('User_Principle_Name').$el).find('input')[0]).attr('style', 'border-color: #6CEE5A;');
           fd.field('SUPN').value = fd.field('User_Principle_Name').value;
           $('.EditAccess').show();
        
           }
    });
}

The function isn't doing anything. No errors in console on load, though it comes with length is not defined.

I am using SharePoint 2019

Hello @DryChips,

How do you call the function?

Also, I don't understand your condition:
if (fd.field('User_Principle_Name').value.items.length > 0)
Shouldn't you check if any items were returned on PnP request like so:
if(items.length > 0)

I want to call the function in a button. So I have added: validateCC(); in the button property but nothing happens.

Yep, the condition if (fd.field('User_Principle_Name').value.items.length > 0) was added in the JS editor to test and see if it works! But turns out it doesn't.

@DryChips,

Your global function name is val. You need to use this code: val() to call your global function.

Hi,

I added the code below outside of fd.spRendered(function(){ and added val() in the button click property and nothing happens. No errors appear in console either.

Where am I going wrong?

//Function will send Pnp request to search for value in list and let user know if name exists in list
window.val = function validateCC(value){
    pnp.sp.web.lists.getByTitle("POWERBiV2").items.filter("User_Principle_Name eq '" + value + "'").get().then(function(items){
           if (items.length > 0){
           alert("This user already exists, please edit this record below.");
           $($(fd.field('User_Principle_Name').$el).find('input')[0]).attr('style', 'border-color: #6CEE5A;');
           fd.field('SUPN').value = fd.field('User_Principle_Name').value;
           $('.EditAccess').show();
        
           }
    });
}

@DryChips,

You have a filter in PnP function. But as you are calling the function without the parameter, the PnP function returns nothing and you see no alerts.

Please test this code to make sure your global function works:

window.val = function validateCC(value){
    alert(value);
}

And separately test the PnP function. Define the pnp function globally to test the code from the browser console:

window.pnp = pnp;

Hi,

I'm not sure what you mean by this statement:

You have a filter in PnP function. But as you are calling the function without the parameter, the PnP function returns nothing and you see no alerts.

I have added this code in the JS Editor and called it using val() when placed in button property and it worked!

window.val = function validateCC(value){
    alert(value);
}

I mean that when you call the function without the value parameter like so: val(); the filter in PnP function looks is invalid and the function doesn't return any value:
pnp.sp.web.lists.getByTitle("POWERBiV2").items.filter("User_Principle_Name eq 'undefined'").get()

You must call this function with parameter like so: val('Jane Doe')

I see, I added the value parameter in val(value) but no luck! I get an error in console:

Here is the code:

window.val = function validateCC(value){
   pnp.sp.web.lists.getByTitle("POWERBiV2").items.filter("User_Principle_Name eq 'value'").get().then(function(items){
        if (items.length > 0){
        alert("This user already exists, please edit this record below.");
           $($(fd.field('User_Principle_Name').$el).find('input')[0]).attr('style', 'border-color: #6CEE5A;');
        }
   })
}

@DryChips,

How do you need to filter the POWERBiV2 list? The User_Principle_Name should be equal to what value?

The user principle name is manually typed into a field and then saved to a SharePoint list. I am doing a lookup to the same SharePoint list to check if the user principle name is present. If it present, I will alert the user so that they don't create the same user principle name but rather a different one.

I have already enabled "enforce unique" values to my list but I want to create a separate function to do another task for me.