Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Can't get to work Javascript web resource to Handle form verifications

Posted on by 10

Hello,

          I have been trying to validate a form Lookup field but it just doesnt seem to work, I have tried many ways in the Javascript and nothing, it just doesn't seem to work, Basically

I need to verify one field on the form of case, if that field has a Determined text another field gets blocked if not the other field is enabled, this seems pretty basic but everytime I try to do it nothing happens, I have even tried with a normal text on an Alertin the browser and it doesn't work either. I'm just starting with Dynamics 365 and I'm not really sure if I'm doing something wrong when loading the JS, The event handler is created in the field itself (the one I need to capture the text ) and is an onChange type, I'm not really sure if I need to add other parameters but so far it hasn't worked.

this is the code of the Script:

function serviceType(executionContext)
{
    debugger;
    var formContext =executionContext.getFormContext();
    var serviceType = formContext.getAttribute("c9_caseservicetype");
     
    if (serviceType != null) {
        
        var text = serviceType[0].name;

       if(text == "TM"){
        formContext.getControl("c9_contract").seDisabled(true);
           
       }
    }
}

As you can see the functionality I'm looking for is quite simple but I can't get it to work, maybe I can accomplish the same with business rules but I just want to learn how to apply scripts for harder modifications that might be not possible with business rules. Any help will be appreciated.

Thanks in advance.

  • Suggested answer
    JorgeDiaz Profile Picture
    JorgeDiaz 10 on at
    RE: Can't get to work Javascript web resource to Handle form verifications

    That did it Amit, thank you so much for your help, basically I've never used Lookup before so that's why the logic of the code was totally wrong. This is for learning.

  • Verified answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,525 Super User 2024 Season 1 on at
    RE: Can't get to work Javascript web resource to Handle form verifications

    Hello Jorge,

    I have checked your code and found that you have not placed any null checks in the given code for lookup.

    Please refer below code where I am checking if lookup field is null or not. If lookup is null/blank then I will make setDisable(false)

    else I will get the lookup value and then check the name of the lookup field.

    if it will match with the respective name then I will make setDisable(true) else I will make as make setDisable(false).

    Also your code is not correct you have used seDisable(false) rather than setDisable(false).

    function serviceType(executionContext)
    {
        debugger;
        var formContext =executionContext.getFormContext();
         
        if (formContext.getAttribute("ownerid").getValue() != null) //Check if lookup is null or not
        {
           var serviceType = formContext.getAttribute("ownerid").getValue()[0].name; //if it is not null then Get Lookup value name
           if(serviceType == "Amit Katariya") // COmpare it with specific name
           {
                formContext.getControl("cr791_cartofirstid").setDisabled(true);      //execute if condition is satisfied  
           }
           else
           {
                formContext.getControl("cr791_cartofirstid").setDisabled(false);     //execute if condition is not satisfied      
           }
        }
        else
        {
            formContext.getControl("cr791_cartofirstid").setDisabled(false);        //execute if lookup is null
        }
    }

    You can see below screenshots for your reference.

    1. pastedimage1652367638670v1.png

    2.pastedimage1652367667746v2.png

    3.pastedimage1652367697131v3.png

    Thank you,

    Amit Katariya

  • JorgeDiaz Profile Picture
    JorgeDiaz 10 on at
    RE: Can't get to work Javascript web resource to Handle form verifications
    [quote user="Vinoth8549"]

    Hello Jorge,

    Try this 

     var serviceType = formContext.getAttribute("c9_caseservicetype").getValue();

     Have you checked the context parameter on the form events ? So it will trigger this function 

    Thanks

    [/quote]

    I changed and the same, this is what I have on the lookup event, this is on the lookup field not on the entire form.

    pastedimage1652332956054v1.png

  • Suggested answer
    Vinoth8549 Profile Picture
    Vinoth8549 5 on at
    RE: Can't get to work Javascript web resource to Handle form verifications

    Hello Jorge,

    Try this 

     var serviceType = formContext.getAttribute("c9_caseservicetype").getValue();

     Have you checked the context parameter on the form events ? So it will trigger this function 

    Thanks

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans