Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Set currency when creating Records via SDK

Posted on by

I have an Entity which has one or more Money-Fields. Therefore, it also has a transactioncurrencyid-field, which specifies the currency for the record.

Now, when i create a record of that entity in the browser, the transactioncurrencyid-field gets pre-populated with the default currency of the system or the user.

However, when creating a record of that entity via SDK without specifying a value for any of the Money-fields, the transactioncurrencyid-field stays empty. After that, no user is able to edit any Money-field because the currency is not set:

A currency is required if a value exists in a money field. Select a currency and try again.

Strangely, this doesn't happen when i set a value for a Money-field (even though I don't tell it what currency to use), e.g.:

var entity = new Entity("new_myentity")
{
    Attributes = 
    {
        { "name" = "Name 1" },
        { "amount" = new Money(1000) }
    }
}

So my question is:
Is there a way to set the Currency of a record to the default value when creating it via SDK without any populated Money-Attributes?

EDIT:

The default currency has been set for both the user and the system and I want that value to be autopopulated to transactioncurrencyid just like CRM does in the UI.

*This post is locked for comments

  • Saddamk206 Profile Picture
    Saddamk206 777 on at
    RE: Set currency when creating Records via SDK

    Get and Set Currency(Money) using C#

    Get : -

    var moneyValue = ((Money)item.Attributes[attributeName]).Value;

    Post : -

    newSalesOrder[attributeName] = new Money((decimal)moneyValue);

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Set currency when creating Records via SDK

    Please refer this:

    social.microsoft.com/.../how-to-get-user-default-currency-idvalue-using-javascript-in-crm-2011

    You can also use wepabpi to get the currency of user

  • Paul Kunze Profile Picture
    Paul Kunze on at
    RE: Set currency when creating Records via SDK

    Hi Nithya, I also tried that and it didn't help. This is why I was asking because I would expect the SDK to set it to the Users Default currency (or the systems default currency, if no user currency is set). Why would I set a default currency in the first place if I was not expecting it to be set by default if no other has been specified, you know?

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Set currency when creating Records via SDK

    Hi Paul,

    Default currency can be set in the Set Personal Options window.

    default-currency.png

    Also, refer the link below.

    community.dynamics.com/.../215112

    Hope this helps.

  • Paul Kunze Profile Picture
    Paul Kunze on at
    RE: Set currency when creating Records via SDK

    Thanks, I tried this already, of course setting transactioncurrencyid on my own solves the issue, but this is not what I am actually asking for. I was wondering if there was an (automated) way, to make it set the default currency on its own like CRM does when creating a record in the browser. Btw.: setting a Monetary-Field on create automatically sets the transactioncurrencyid, I tried that (even in SDK).

    I just think that this difference in behaviour between SDK and GUI is kind of strange and even though I could resolve this manually everytime I create records with all empty monetary fields, there's still a certain chance for Devs to forget to do so.

  • Paul Kunze Profile Picture
    Paul Kunze on at
    RE: Set currency when creating Records via SDK

    Thanks! I know I can set it, I was just wondering if there was a way to make it behave just like the GUI (set it to the users/systems default currency on its own).

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Set currency when creating Records via SDK

    Hi Paul,

    You should set the currency field as shown in the code below.

    EntityReference currencyType = new EntityReference();
    currencyType.Id = “(The Guid Of The Currency Type Goes Here)”;
    currencyType.LogicalName = “transactioncurrency”;
    
    entity.Attributes.Add(“transactioncurrencyid”,currencyType);

    See: https://community.dynamics.com/crm/f/117/t/147795

    Hope this helps.

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Set currency when creating Records via SDK

    Hi

    You need to set the transactioncurrencyid as well, when you set value for currency field.

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Set currency when creating Records via SDK

    Yes, you can directly set the transactioncurrencyid field when you create the record via the SDK - it's just a normal CrmReference field

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans