Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Setting field Value using JavaScript

(0) ShareShare
ReportReport
Posted on by 270

I have 4 fields that are currency, I am using a business rule to calculate the total which is working. I am trying to set the values of the 4 fields onLoad to $1.00

Is this how I can acchive that? Thanks!

function onLoad() {
    Test("new_field1");
    Test2("new_field2");
    Test3("new_field3");
}

function new_field1OnChange() {
    Test("new_field1");
}

function new_field1OnChange() {
    Test2("new_field2");
}

function new_field1OnChange() {
    Test3("new_field3");
}

function Test(fieldName) {
Xrm.Page.getAttribute(arg).setValue("1.00);
} 

*This post is locked for comments

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Setting field Value using JavaScript

    Not really what I am trying to do is to trigger other fileds based on Filed3 value if Field3 is greater than 1, I would display field 4, and if field 4 is greater than 1, I will display field 5 and so on

    function budgetBreakdown(fieldName, forceReset) {

       var field = Xrm.Page.getAttribute(fieldName);

       var value = field.getValue();

       if (!value || forceReset === true)

           field.setValue(1.00);

       if (field == "new_field3" && value != 1.00) {

           Xrm.Page.getControl("new_field3Comment").setVisible(true);

           Xrm.Page.getAttribute("new_field3Comment").setRequiredLevel("required");

           Xrm.Page.getControl("new_field4").setVisible(true);

       }

       if (field == "new_field4" && value != 1.00) {

           Xrm.Page.getControl("new_field4Comment").setVisible(true);

           Xrm.Page.getAttribute("new_field4Comment").setRequiredLevel("required");

           Xrm.Page.getControl("new_field5").setVisible(true);

       }

    }

  • RE: Setting field Value using JavaScript

    what do you want to do by using && !==0 ??

    do you want to check if the value is 0?

    it will never because the line above you set the value to 1.00.

    and you would have to check like

    if(fieldName == "new_field3" && field.getValue()!==0)

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Setting field Value using JavaScript

    Hey guys, can I toggle between the different fields like this? Thanks

    if (fieldName == "new_field3"  && !==0)

          Xrm.Page.getControl("new_fied4").setVisible(true);

          Xrm.Page.getControl("new_field4").setVisible(false);

          Xrm.Page.getControl("new_field5").setVisible(false);

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Setting field Value using JavaScript

    Hi Tobias,

    I changed if(!value || forceReset===true) to if(!value || forceReset===FALSE), that seems to fix.

    I need to make an update in the code, adding more field which will be displayed if a user put a value for field 3. OnLoad the fields are hidden. The part am struggling with is displaying the other fields based on the condition, which is if field3 has a value other than 1, field 4 will be displayed, if field 4 has a value other than 1, field 5 will be displayed...

    I modified the code as follow:

    function Test(fieldName, forceReset) {

    var field = Xrm.Page.getAttribute(fieldName);

    var value = field.getValue();

    if(!value || forceReset===true)

    field.setValue(1.00);

    if (fieldName == "new_field3"  && !==0)

           Xrm.Page.getControl("new_fied4").setVisible(true);

           Xrm.Page.getControl("new_field4").setVisible(false);

           Xrm.Page.getControl("new_field5").setVisible(false);

    }

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Setting field Value using JavaScript

    Thank you Tobias.  It's working but it overight the value onLoad. What I meant by that is, I entered values and the total calculated onSave.  When I refresh the page the fields are setback to 1. Since I saved the value I was expecting to see the fields value. The only value I see is the total

  • Verified answer
    RE: Setting field Value using JavaScript

    ok please try this code.

    this should work.

    function onLoad() {
        
    	setEvents();
    	
    	Test("new_field1",true);
        Test("new_field2",true);
        Test("new_field3",true);	
    }
    
    function setEvents()
    {
    	setEventToField("new_field1", new_field1OnChange);
    	setEventToField("new_field2", new_field2OnChange);
    	setEventToField("new_field3",new_field3OnChange);
    }
    
    function setEventToField(fieldName,callback){
    	var field= Xrm.Page.getAttribute(fieldName);
    	if(!field){
    		alert("Field ["+fieldName+"] is not on the form. Can't set onChange-handler");
    		return;
    	}
    	
    	field.addOnChange(callback);
    }
    
    function new_field1OnChange() {
        Test("new_field1");
    }
    
    function new_field2OnChange() {
        Test("new_field2");
    }
    
    function new_field3OnChange() {
        Test("new_field3");
    }
    
    function Test(fieldName, forceReset) {
    	var field = Xrm.Page.getAttribute(fieldName);
    	var value = field.getValue();
    	
    	if(!value || forceReset===true)
    		field.setValue(1.00);
    } 
  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Setting field Value using JavaScript

    Yes, I want to reset the value to 1 onLoad and if the value is deleted (null). I tried the code it only works for the first field and I am getting ReferenceError: new_field2 is not defined at eval (eval at RunHandlerInternal ) for the remaining fields.

  • RE: Setting field Value using JavaScript

    do you always want to reset the value in onload (no matter what value it is?)

    this would only set the field to 1.00 if the field is empty.

    function Test(fieldName) {
    	var field = Xrm.Page.getAttribute(fieldName);
    	var value = field.getValue();
    	
    	if(!value)
    		field.setValue(1.00);
    } 
  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Setting field Value using JavaScript

    Hey Tobias,

    This code is also changing the value of the field before it gets saved, if I change the value from 1 to 10 it will automatically change it back to 1. The only time I want the field to be 1 is onLoad and if the user delete the value.

    Thanks

  • RE: Setting field Value using JavaScript

    based on your first sample-code this would look like:

    function onLoad() {
        
        setEvents();
    	
        Test("new_field1");
        Test("new_field2");
        Test("new_field3");	
    }
    
    function setEvents()
    {
    	Xrm.Page.getAttribute("new_field1").addOnChange(new_field1OnChange)
    	Xrm.Page.getAttribute("new_field2").addOnChange(new_field2OnChange)
    	Xrm.Page.getAttribute("new_field3").addOnChange(new_field3OnChange)
    }
    
    function new_field1OnChange() {
        Test("new_field1");
    }
    
    function new_field2OnChange() {
        Test("new_field2");
    }
    
    function new_field3OnChange() {
        Test("new_field3");
    }
    
    function Test(fieldName) {
    	Xrm.Page.getAttribute(fieldName).setValue(1.00);
    } 

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans