web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Conditional warning using Jscript

(0) ShareShare
ReportReport
Posted on by 750

Hello,

I am by no means an expert at this but have been trying for hours and cant get this to work. I have it to trigger onload on the form and below is my jscript. I kept getting the unidentified error yet the field value is correct. This is a two option field. with Yes value = 1 and No = 0. 

function lockwarning(executionContext){
var warningvar = "warning";
var formContext = executionContext.getFormContext(); 
{
var lockphase = formContext.getAttribute("new_lockphase").getValue(); 
if (lockphase === 1){
Xrm.Page.ui.setFormNotification ("Phase locked Contacted Management","WARNING",warningvar);
}
if (lockphase === 0){
formContext.ui.clearFormNotification(warninvar);
}}
}
  

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Conditional warning using Jscript

    Hello,

    Check this code (you had extra curly brackets):

    function lockwarning(executionContext) {
    	var warningvar = "warning";
    	var formContext = executionContext.getFormContext();
    	var lockphase = formContext.getAttribute("new_lockphase").getValue();
    	if (!!lockphase) {
    		formContext.ui.setFormNotification("Phase locked Contacted Management", "WARNING", warningvar);
    	} else {
    		formContext.ui.clearFormNotification(warninvar);
    	}
    }

    If it's not working - can you please provide the screenshot on how you registered the onload event handler?

  • fireinc Profile Picture
    750 on at
    RE: Conditional warning using Jscript

    I gave it a go at first gave it gave error "“Cannot read property ‘getFormContext’ of undefined”" So I selected pass execution content first here is the onload event. I do not get any errors but it does not seem to fire. 

    1884.onload.jpg

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Conditional warning using Jscript

    Hello,

    Based on the code that you have if the checkbox is checked - then it will show the warning during onload. If it's not checked - it will do nothing.

    If you want the code to be executed on the change of the field as well - add one more handler to onchange of your new_lockphase field and don't forget to check "Pass execution context as a first parameter" (you can use the same function).

    Also I tweaked the code a bit - check and use the latest version from my previous reply.

  • fireinc Profile Picture
    750 on at
    RE: Conditional warning using Jscript

    Marked your first answer as correct the new code worked. Thanks man. I was working on that for a long time. Jscript is not my strong point.

  • fireinc Profile Picture
    750 on at
    RE: Conditional warning using Jscript

    I spoke to soon the code works correct for if the box is checked if the box is not checked it comes back with the undefined error.

  • fireinc Profile Picture
    750 on at
    RE: Conditional warning using Jscript

    I found the issue you carried over my typo. I actually used chatgpt to help me find the error and it worked pretty good. The last clear was missing the g in warning

    function lockwarning(executionContext) {
        var warningvar = "warning";
        var formContext = executionContext.getFormContext();
        var lockphase = formContext.getAttribute("new_lockphase").getValue();
        if (!!lockphase) {
            formContext.ui.setFormNotification("Phase locked Contacted Management", "WARNING", warningvar);
        } else {
            formContext.ui.clearFormNotification(warningvar);
        }
    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 125

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 98 Super User 2025 Season 2

#3
Erin Lubben Profile Picture

Erin Lubben 73

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans