Conditional Logic Error

Hi,

I'm struggling to get this If statement working.

//If statement when user answers No && No in prelim Qs
fd.spRendered(function() {

function StopSubmission() {
    if (fd.field('Is_x0020_the_x0020_employee_x0020').value == 'No' && fd.field('Is_x0020_the_x0020_Employee_x002').value == 'No') {
        // Returns Error, telling user you are submitting wrong form
        error: 'You are completing wrong form!';
        return false;
    }
}

// Calling StopSubmission on form loading
StopSubmission();

});

Please can you help?!

Hello @DryChips,

The condition itself is correct, but the rest of the code is invalid.

How exactly do you want the form to behave when the condition is met? To disable all fields, show the error message, redirect to another page, etc.?

Hi,

Can you provide snippets to all three examples?

I think it would be nice if an error message appears in a dialog box, explaining why the user can't submit the form as well as disabling all fields in case they ignore the message.

If that too much, please can you provide snippets to the examples you have suggested?

@DryChips,

You can display the alert message and disable all fields on the form using the code:


fd.spRendered(function() {
    if(fd.field('Is_x0020_the_x0020_employee_x0020').value == 'No' 
&& fd.field('Is_x0020_the_x0020_Employee_x002').value == 'No'){
        
        //display the alert box
        alert("You can't edit this form");
        
       //disable all fields on the form
        fd.fields().forEach(function(i) {
            console.log(i.title)
            fd.field(i.title).disabled = true;
        })
    }
});

@DryChips,

I'm sorry, the fd.fields() is not available in SharePoint 2019 version yet. You need to disable fields one by one:

fd.spRendered(function() {
    if(fd.field('Is_x0020_the_x0020_employee_x0020').value == 'No' 
&& fd.field('Is_x0020_the_x0020_Employee_x002').value == 'No'){
        
        //display the alert box
        alert("You can't edit this form");
        
       //disable fields on the form
            fd.field(Field1).disabled = true;
            fd.field(Field2).disabled = true;
            fd.field(Field3).disabled = true;

    }
});

Or you can redirect a user to the display form:

fd.spRendered(function() {
    if(fd.field('Is_x0020_the_x0020_employee_x0020').value == 'No' 
&& fd.field('Is_x0020_the_x0020_Employee_x002').value == 'No'){
        
            //display the alert box
            alert("You can't edit this form");
            var listId = fd.spFormCtx.ListAttributes.Id;
            var itemId = fd.itemId;

            //redirect to Display form
            window.location.href = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/listform.aspx?PageType=4&ListId=" + listId + "&ID=" + itemId;


    }
});
1 Like

Hello,

Thanks for the code! They're still not working for some weird reason.

@DryChips,

There are no syntax errors in the code, that's all I can say.

Try commenting out all code except this one:

fd.spRendered(function() {
    if(fd.field('Is_x0020_the_x0020_employee_x0020').value == 'No' 
&& fd.field('Is_x0020_the_x0020_Employee_x002').value == 'No'){
        
            //display the alert box
            alert("You can't edit this form");
            var listId = fd.spFormCtx.ListAttributes.Id;
            var itemId = fd.itemId;

            //redirect to Display form
            window.location.href = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/listform.aspx?PageType=4&ListId=" + listId + "&ID=" + itemId;

    }
});

You can also debug the code, find the instructions here:

1 Like

Hi,

Thanks for getting back to me. I'll try this and let you know the outcome.

1 Like