Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Strange Javascript Field Hiding Behaviour

Posted on by 112

Hello Community,

I am encountering some unusual behaviour using Java to show and hide fields based on optionset values (inserted what I have so far):

function showRiskOnChange(executionContext)
{
   const formContext = executionContext.getFormContext();
   
   const notetype = formContext.getAttribute("rst_note_type").getValue();
   
	if (notetype != null)
	{
			// Risk Note Type
		if (notetype === 866570003)
		{
			formContext.getControl("rst_risk_policy_number").setVisible(true);
			formContext.getControl("rst_risk_insurer").setVisible(true);
			formContext.getControl("rst_risk_product").setVisible(true);
			formContext.getControl("rst_risk_date_issue").setVisible(true);
			formContext.getControl("rst_risk_super_self_owned").setVisible(true);
			formContext.getControl("rst_risk_policy_owner").setVisible(true);
			formContext.getControl("rst_risk_date_end").setVisible(true);
			formContext.getControl("rst_risk_life_insured").setVisible(true);
			formContext.getControl("rst_risk_type").setVisible(true);
			formContext.getControl("rst_risk_amount").setVisible(true);
			formContext.getControl("rst_risk_premium").setVisible(true);
			formContext.getControl("rst_risk_premium_type").setVisible(true);
			formContext.getControl("rst_risk_waiting_benefit_period").setVisible(true);
			formContext.getControl("rst_risk_loading_exclusions").setVisible(true);
		}
		else 
		{
			formContext.getControl("rst_risk_policy_number").setVisible(false);
			formContext.getControl("rst_risk_insurer").setVisible(false);
			formContext.getControl("rst_risk_product").setVisible(false);
			formContext.getControl("rst_risk_date_issue").setVisible(false);
			formContext.getControl("rst_risk_super_self_owned").setVisible(false);
			formContext.getControl("rst_risk_policy_owner").setVisible(false);
			formContext.getControl("rst_risk_date_end").setVisible(false);
			formContext.getControl("rst_risk_life_insured").setVisible(false);
			formContext.getControl("rst_risk_type").setVisible(false);
			formContext.getControl("rst_risk_amount").setVisible(false);
			formContext.getControl("rst_risk_premium").setVisible(false);
			formContext.getControl("rst_risk_premium_type").setVisible(false);
			formContext.getControl("rst_risk_waiting_benefit_period").setVisible(false);
			formContext.getControl("rst_risk_loading_exclusions").setVisible(false);
		}
			// A&L Note Type
		if (notetype === 866700002)
		{
			formContext.getControl("rst_asset_description").setVisible(true);
			formContext.getControl("rst_asset_doa").setVisible(true);
			formContext.getControl("rst_asset_doa_col").setVisible(true);
			formContext.getControl("rst_asset_cost").setVisible(true);
			formContext.getControl("rst_asset_owner").setVisible(true);
			formContext.getControl("rst_asset_value").setVisible(true);
			formContext.getControl("rst_asset_dov").setVisible(true);
			formContext.getControl("rst_asset_dov_col").setVisible(true);
			formContext.getControl("rst_asset_bsb").setVisible(true);
			formContext.getControl("rst_asset_account").setVisible(true);
			formContext.getControl("rst_liability_description").setVisible(true);
			formContext.getControl("rst_liability_dd").setVisible(true);
			formContext.getControl("rst_liability_dd_col").setVisible(true);
			formContext.getControl("rst_liability_repayment").setVisible(true);
			formContext.getControl("rst_liability_repayment_rate").setVisible(true);
			formContext.getControl("rst_liability_value").setVisible(true);
			formContext.getControl("rst_liability_dov").setVisible(true);
			formContext.getControl("rst_liability_dov_col").setVisible(true);
			formContext.getControl("rst_liability_bsb").setVisible(true);
			formContext.getControl("rst_liability_account").setVisible(true);
		}
		else
		{
			formContext.getControl("rst_asset_description").setVisible(false);
			formContext.getControl("rst_asset_doa").setVisible(false);
			formContext.getControl("rst_asset_doa_col").setVisible(false);
			formContext.getControl("rst_asset_cost").setVisible(false);
			formContext.getControl("rst_asset_owner").setVisible(false);
			formContext.getControl("rst_asset_value").setVisible(false);
			formContext.getControl("rst_asset_dov").setVisible(false);
			formContext.getControl("rst_asset_dov_col").setVisible(false);
			formContext.getControl("rst_asset_bsb").setVisible(false);
			formContext.getControl("rst_asset_account").setVisible(false);
			formContext.getControl("rst_liability_description").setVisible(false);
			formContext.getControl("rst_liability_dd").setVisible(false);
			formContext.getControl("rst_liability_dd_col").setVisible(false);
			formContext.getControl("rst_liability_repayment").setVisible(false);
			formContext.getControl("rst_liability_repayment_rate").setVisible(false);
			formContext.getControl("rst_liability_value").setVisible(false);
			formContext.getControl("rst_liability_dov").setVisible(false);
			formContext.getControl("rst_liability_dov_col").setVisible(false);
			formContext.getControl("rst_liability_bsb").setVisible(false);
			formContext.getControl("rst_liability_account").setVisible(false);
		}
	}
}

pastedimage1659949366248v1.png

The Show/Hide logic should kick in on the choice of either A&L or Risk (per code above):

pastedimage1659950094313v3.png

First entering the form, behaviour is normal and as expected:

pastedimage1659950123555v4.png

Entering the data, behaviour continues to be normal:

pastedimage1659950343904v5.png

When we save and close, the information is correctly stored against the note:

pastedimage1659950421754v6.png

My issues are when I attempt to view the newly created record, if needing to update/edit the records:

pastedimage1659950533178v7.png

I have very unsuccessfully searched for a solution for this issue, and as I have noticed it appears to be consistent across the Show/Hide scripts I have created across a number of forms.

Is there anything obvious that I am missing, or any suggestions?

Thanks in advance,

Jayden.

  • JaydenPerez17 Profile Picture
    JaydenPerez17 112 on at
    RE: Strange Javascript Field Hiding Behaviour

    Thank you Bipin,

    I have now discovered the onLoad event in the form properties.

    It wasnt initially clear to me the requirement, but I have tested it successfully.

    Thanks again.

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,962 Moderator on at
    RE: Strange Javascript Field Hiding Behaviour

    Hi,

    You need to add your function on onload event also to make it work.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans