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)

Set Default value in a lookup field using javascript

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Daniel Wikell Profile Picture
    2,360 on at

    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

  • Suggested answer
    Ivan Ficko Profile Picture
    1,380 on at

    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.

  • Community Member Profile Picture
    on at

    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

  • Community Member Profile Picture
    on at

    Please note the SCurrencyId is GUID of Dollar

    and sCurrencyName should be Dollar

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    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.

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    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

  • CRMexplorer Profile Picture
    760 on at

    Hi,

    How will I get the GUID of Dollar?

  • CRMexplorer Profile Picture
    760 on at

    Hi,

    How will I know the GUID of dollar?

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    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.

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