Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

JScript - Clear Whole Tab Values on Field Change

Posted on by Microsoft Employee

Hi all! I am trying to write a script that will run OnChange for a field to clear all values in a specific tab. In this example, the Tab to be cleared is "Triage", and the field i'd like to have it execute on is "new_storereportedissue":

function clearTab() {

Xrm.Page.getAttribute("new_storereportedissue").addOnChange(clearTab)
var Triage = Xrm.Page.ui.tabs.get("Triage");
	   
    Triage.sections.forEach(function (section, sectionIndex) {
        section.controls.forEach(function (control, controlIndex) {

            switch (control.getAttribute().getAttributeType()) {
                case "boolean":
                    //for checkbox control, uncheck it
                    control.getAttribute().setValue(null);
                    break;
            }
        });
    });
}


I'd love any help. Thank you!

*This post is locked for comments

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JScript - Clear Whole Tab Values on Field Change

    Problem resolved with the following code:

    function clearTab() {  
             var sectionName = Xrm.Page.ui.tabs.get("Triage").sections.get();
             for (var i in sectionName) {
             var controls = sectionName[i].controls.get();
             var controlsL = controls.length;
             for (var i = 0; i < controlsL; i++) {
                 controls[i].getAttribute().setValue(null);
              }
            }
        }


    Thanks everyone!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JScript - Clear Whole Tab Values on Field Change

    Hi Ravi! Thanks for the reply. When I add Nicolas' version of the clearTab function to OnChange for the new_storereportedissue field, I get this error when it fires (had to edit out my companies instance name):

    clearTab/</<@example.crm.dynamics.com/.../new_CIClearTab
    forEach@example.crm.dynamics.com/.../global.ashx
    clearTab/<@example.crm.dynamics.com/.../new_CIClearTab
    forEach@example.crm.dynamics.com/.../global.ashx
    clearTab@example.crm.dynamics.com/.../new_CIClearTab
    @example.crm.dynamics.com/.../ClientApiWrapper.aspx line 157 > eval:1:1
    RunHandlerInternal@example.crm.dynamics.com/.../ClientApiWrapper.aspx
    RunHandlers@example.crm.dynamics.com/.../ClientApiWrapper.aspx
    ExecuteHandler@example.crm.dynamics.com/.../ClientApiWrapper.aspx
    $Ch_1@example.crm.dynamics.com/.../formcontrols.js
    executeHandler@example.crm.dynamics.com/.../formcontrols.js
    executeHandlerByDescriptor@example.crm.dynamics.com/.../formcontrols.js
    getHandler/<@example.crm.dynamics.com/.../formcontrols.js
    getHandler/<@example.crm.dynamics.com/.../global.ashx
    fireOnChange@example.crm.dynamics.com/.../formcontrols.js
    setValueInternal@example.crm.dynamics.com/.../formcontrols.js
    setValue@example.crm.dynamics.com/.../formcontrols.js
    updateDataAttributeValue@example.crm.dynamics.com/.../formcontrols.js
    $3e_2@example.crm.dynamics.com/.../formcontrols.js
    Function.createDelegate/<@example.crm.dynamics.com/.../MicrosoftAjax.js
    b@example.crm.dynamics.com/.../MicrosoftAjax.js


    Hope this helps!

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: JScript - Clear Whole Tab Values on Field Change

    Hi,

    The script provided by Nicolas above should work fine. What's the error you got with that script?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JScript - Clear Whole Tab Values on Field Change

    Bump - anyone have any ideas? I just want to get the Javascript to wipe a whole tab's info when a certain field is changed. Thanks!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JScript - Clear Whole Tab Values on Field Change

    [Reserved]

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JScript - Clear Whole Tab Values on Field Change

    [quote user="Nicolas.Plourde"]

    [stuff]

    [/quote]

    I tried throwing this into the TabStateChange Handler of the Triage tab with no success:

    function clearTab() {
    	Xrm.Page.getAttribute("new_storereportedissue").addOnChange(clearTab);
    }

    Additionally, I tried to execute your version of clearTab and it errored out. Is it correct to reference a variable as shown (triage.sections.forEach)?

    If it helps, the reason I would like to blanket clear the whole tab is because it contains over 150 fields that are visible based on the store reported issue. Hope this helps, and thanks for replying.

  • Nicolas.Plourde Profile Picture
    Nicolas.Plourde 240 on at
    RE: JScript - Clear Whole Tab Values on Field Change

    You can also make a business rule to do this.

    function onload() {
    	// You can either do this in the onload of the form or bind the event
    	// on the control (double-click on the control and look for the tab events)
    	Xrm.Page.getAttribute("new_storereportedissue").addOnChange(clearTab);
    }
    
    function clearTab() {
    
    	var triage = Xrm.Page.ui.tabs.get("Triage");
    	
    	// This will work but it's a bit too much. You should consider setting each attributes yourself here.
    	//  Of course with this method you wont have to update your code.
        triage.sections.forEach(function (section, sectionIndex) {
            section.controls.forEach(function (control, controlIndex) {
    
    			// In theory, you can set null every fields without checking types
    			control.getAttribute().setValue(null);
    			
    			// Making sure the control will be saved
    			control.setSubmitMode('dirty');
            });
        });
    }

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans