Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Set Default value in a lookup field using javascript

Posted on by 760

Hi,

I want to set the value of  the field "currency" to dollar using javascript but it's not working

var id =Xrm.Page.data.entity.getId();
Xrm.Page.getAttribute('currency').setValue("Dollar");
alert(id);

*This post is locked for comments

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Set Default value in a lookup field using javascript

    Hi Arci,

    Open the following URL in your browser.

    You will get the transactioncurrencyid of the currency name US dollar in that JSON response.

    Hope this helps.

  • CRMexplorer Profile Picture
    CRMexplorer 760 on at
    RE: Set Default value in a lookup field using javascript

    Hi,

    How will I know the GUID of dollar?

  • CRMexplorer Profile Picture
    CRMexplorer 760 on at
    RE: Set Default value in a lookup field using javascript

    Hi,

    How will I get the GUID of Dollar?

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Set Default value in a lookup field using javascript

    Hi Arci,

    You can also use below function

    function SetLookUp(fieldName, fieldType, fieldId, value) {

       try {

           var object = new Array();

           object[0] = new Object();

           object[0].id = fieldId;

           object[0].name = value;

           object[0].entityType = fieldType;

           Xrm.Page.getAttribute(fieldName).setValue(object);

           Xrm.Page.getAttribute(fieldName).setSubmitMode("always");

       }

       catch (e) {

           alert("Error in SetLookUp: fieldName = " + fieldName + " fieldType = " + fieldType + " fieldId = " + fieldId + " value = " + value + " error = " + e);

       }

    }

    Thanks

    Regards,

    AW

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Set Default value in a lookup field using javascript

    Hi Arci,

    In order to set the value of lookup field using javascript, use the code below.

    var object = new Array();
    object[0] = new Object();
    object[0].id = "GUID of the currency record Dollar";
    object[0].name = "Dollar";
    object[0].entityType = "transactioncurrency";
    Xrm.Page.getAttribute("currency").setValue(object);

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Set Default value in a lookup field using javascript

    Please note the SCurrencyId is GUID of Dollar

    and sCurrencyName should be Dollar

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Set Default value in a lookup field using javascript

    Hi Arci,

    Hope the following code would help.

    The currency is a lookup field and you can't set it like a normal attribute value.

    You should create a array in which you would create object and then set the parameters.

    var Currency = new Array();

    Currency[0] = new Object();

    Currency[0].id = sCurrencyId;

    Currency[0].entityType = "transactioncurrency";

    Currency[0].name = sCurrencyName;

    Xrm.Page.getAttribute("transactioncurrencyid").setValue(Currency);

    Thanks

    Phanidhar

  • Suggested answer
    Ivan Ficko Profile Picture
    Ivan Ficko 1,380 on at
    RE: Set Default value in a lookup field using javascript

    The problem is that in this case, you need to send GUID as a parameter to the setValue method, not a string value.

    You can get that GUID by opening currency lookup in the new window/tab and look for the GUID in URL

    Example:

    YOURORGANIZATION.crm4.dynamics.com/.../edit.aspx3F842377-5194-E611-80F5-5065F38A99D1%7d

    GUID is marked in bold in the URL.

  • Daniel Wikell Profile Picture
    Daniel Wikell 2,360 on at
    RE: Set Default value in a lookup field using javascript

    Hi

    The currency field is of type Lookup. You need to assign the actual EntityReference of the Currency called "Dollar" in order for it to work the way you want.

    Best way is to retrieve the currency instance using the webapi and then use the CurrencyId property for the lookup. You can read more about retrieving data through the webapi here: community.dynamics.com/.../ms-crm-2016-web-api-operations-retrieve-single-or-multiple-records

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