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?!
Margo
(Margo)
January 20, 2022, 7:11am
2
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?
Margo
(Margo)
January 21, 2022, 7:34am
4
@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;
})
}
});
Margo
(Margo)
January 24, 2022, 7:04am
6
@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.
Margo
(Margo)
January 25, 2022, 7:18am
8
@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:
Dear @himansh ,
The easiest option to debug code is to run it directly from browser's console. First, you need to add variables that you want to work with to the global window, with the following lines in JS editor:
window.fd = fd;
window.$ = $;
window.pnp = pnp;
Then, run any code pieces in browser's console without any problems. The only issue is that you won't be able to run them inside the events, the form will already be loaded. Most of the time, you can just wrap the code after testing …
1 Like
Hi,
Thanks for getting back to me. I'll try this and let you know the outcome.
1 Like