web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Setting field Value using JavaScript

(0) ShareShare
ReportReport
Posted on by 274

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

I have the same question (0)
  • Suggested answer
    Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi Mitu,

    Can u please clarify your question ??

    Set value of field onload of page,we can do this using business rules instead of going javascript. So no need to write js for this.For your concern in js code is : Xrm.Page.getAttribute("field name").setValue("1.00");

    Where ever you want to set field value you can use this & in crm Onload event call the method.

    Hope you got it...

    Thanks

  • Tobias Koller Profile Picture
    on at

    I'm sure you just typed in this JavaScript and never used it, right?

    because your three OnChange-Functions are named all the same: new_field1OnChange...

    and your Test-Function is using "args" as the fieldName and not the variable "fieldName".

    Like Jharana wrote: just use Xrm.Page.getAttribute("fieldname").setValue("1.00");

  • Suggested answer
    Rahul G J Profile Picture
    605 on at

    U can use the above mentioned JS Code, but as your value is an integer/number:

    use this, with no commas inside setvalue

    Xrm.Page.getAttribute("field name").setValue(1.00);

  • MituCRMing Profile Picture
    274 on at

    Thank you guys, I used the below code , it works onLoad, but if I delete the value it doesn't set back to 1.00 instead it will be null. Thanks!

    function onLoad() {

       Test("new_field1");

    }

    function new_field1OnChange() {

       Test("new_field1");

    }

    function Test(fieldName) {

       if (Xrm.Page.getAttribute(fieldName).getValue() == null) {

           Xrm.Page.getAttribute("new_field1").setValue("1.00");

       }

       else {

           Xrm.Page.getAttribute("new_field1").setValue("");

       }

    }

  • Suggested answer
    Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi Mitu,

    Just write code OnChange  of that particular field for which when you will delete or change the value it should store the value with "1.00".

    Hope you will get !!

    Thanks,

  • Tobias Koller Profile Picture
    on at

    I'm always a fan to set the Event directly in the script.

    This way you can see always what Events are set.

    just set the event in your onload-function:

    Xrm.Page.getAttribute("new_field1").addOnChange(new_field1OnChange)

  • MituCRMing Profile Picture
    274 on at

    Thank you guys, but it's still not setting the value back to 1. I have 3 fields, onLoad 3 of the fields gets the value 1, after I change the values and onSave the total gets calculated.  If I change any of the value after save it's set back to null and my total also set too null. This is the code

    function Test1(fieldName) {

       if (Xrm.Page.getAttribute(fieldName).getValue() == null) {

           Xrm.Page.getAttribute("new_field1").setValue("1.00");

       }

      else {

               Xrm.Page.getAttribute("new_field1").addOnChange(new_field1OnChange)

              }

    }

  • Tobias Koller Profile Picture
    on at

    can you share the complete code? I'm sure your problem is very easy to resolve

  • Tobias Koller Profile Picture
    on at

    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);
    } 
  • MituCRMing Profile Picture
    274 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans