Skip to main content

Notifications

Announcements

No record found.

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);
}}
}
  

  • 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);
        }
    }

  • 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

    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.

  • Suggested answer
    a33ik Profile Picture
    84,325 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

    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

  • Verified answer
    a33ik Profile Picture
    84,325 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?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,865 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans