locked
Setting the value of Money-types using OrganizationService RRS feed

  • Question

  • Hi all

    I am trying to create an account in CRM 2015 using the OrganizationService from BizTalk. It almost works - I have issues getting values into fields that are of the "Money"-type. In order to test, I have created a small .NET application to simulate creating the XML BizTalk will be creating and in this small .NET application I cannot get values into Money-fields either.

    My sample code is as this:

    void CreateAccount()
    {
        OrganizationServiceClient client = new OrganizationServiceClient();
        client.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
    
        Entity entity = new Entity() { LogicalName = "account" };
                
        AttributeCollection att = new AttributeCollection();
        att.Add(new KeyValuePair<string, object>("accountnumber", "98269828"));
        att.Add(new KeyValuePair<string, object>("creditlimit", 1000.00));
        att.Add(new KeyValuePair<string, object>("name", "Jan Eliasen Dummy Corp"));
                
        entity.Attributes = att;
     
        Guid g = client.Create(entity);
    }

    I do not get any exceptions, but the value for "creditlimit" is null in the AccountBase-table in the database, whereas other fields, such as name, telephone number and so on are added just fine. I have researched and found that I probably need to set a currency for the amount in order to let CRM know how to understand the amount. I have tried setting it in two ways:

    att.Add(new KeyValuePair<string, object>("transactioncurrencyidname", "DKK"));
    att.Add(new KeyValuePair<string, object>("transactioncurrencyid", new Guid("A9CFECC5-0970-E511-80CB-005056814687")));


    where the guid-value is the one I found for the DKK-currency in the "TransactionCurrency"-table in the CRM-database.

    Neither seem to work, as the value for TransactionCurrencyId stays null in the created row in the AccountBase-table.

    I cannot use the SDK-classes and the Money-class because this is not supported by BizTalk - I need to create the XML for the OrganizationService my self.

    As a test, I tried setting the creditlimit like this:

    att.Add(new KeyValuePair<string, object>("creditlimit", new Money() { Value = (Decimal)1000.00 }));

    but that gave me this exception:

    There was an error while trying to serialize parameter schemas.microsoft.com/.../Services:entity. The InnerException message was 'Type 'TestCRM.crmref.Money' with data contract name 'Money:schemas.microsoft.com/.../Contracts' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

    Does anyone know how I can specify the currency? Or get Money-fields to work in another way?

    Thanks!


    eliasen, representing himself and not the company he works for.
    Five times MVP and four times MCTS in BizTalk.
    Blog: http://blogs.eliasen.dk/technical/

    Wednesday, March 9, 2016 9:03 AM

All replies

  • You will need to pass the creditlimit as a Money type, and the transactioncurrencyid as an EntityReference.

    The errors regarding serialisation look like those that you get if creating a web method that uses these classes, which you resolve by adding XmlInclude attributes on the web method definition. However, I don't know how you would resolve similar in BizTalk.

    One other option would be to create a new field of type decimal on the account, and update that from BizTalk. Within CRM, have a workflow (or plugin) that then updates the credit limit from the value in this field


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Wednesday, March 9, 2016 10:47 AM
    Moderator
  • Thank you for your answer!

    You will need to pass the creditlimit as a Money type, and the transactioncurrencyid as an EntityReference.

    Right. So my code to set the creditlimit is actually OK, but I need the below mentioned XmlIncludes in order for it to work? About the EntityReference, I have searched, but haven't found examples of how to program that. Do you happen to have an example? And can I use the code of the currency (DKK) instead of the guid in any way? I would hate for BizTalk to have hard coded a bunch of CRM-internal-guids, you see :-)

    The errors regarding serialisation look like those that you get if creating a web method that uses these classes, which you resolve by adding XmlInclude attributes on the web method definition. However, I don't know how you would resolve similar in BizTalk.

    Thanks. I will look into that and see if I can find any information about that. Basically, BizTalk will have to generate thje XML manually, that my .NET program would actually be sending to the OrganizationService, using XSLT. So once I figure out how it is done in code, I will try to mimick it in XSLT.

    One other option would be to create a new field of type decimal on the account, and update that from BizTalk. Within CRM, have a workflow (or plugin) that then updates the credit limit from the value in this field

    Definately an option, yes... But I would rather that the customer wouldn't have to do that. Thanks again! :-) If you have any examples of coding the EntityReference or input on my question about using the currencycode in stead of a guid, that would be great :)

    eliasen, representing himself and not the company he works for.
    Five times MVP and four times MCTS in BizTalk.
    Blog: http://blogs.eliasen.dk/technical/

    Wednesday, March 9, 2016 1:08 PM
  • For the EntityReference, you'd want something like:

    att.Add(new KeyValuePair<string, object>("transactioncurrencyid", new EntityReference() { Id = new Guid("A9CFECC5-0970-E511-80CB-005056814687") , LogicalName = "transactioncurrency" }));
    
    You need to specify the currency with a Guid when modifying data. You could get this via a RetrieveMultiple to query the transactioncurrencyid based on the currencycode. However, I think the currency records are static data that's the same for each implementation, so I think the guid for DKK would always be the same 

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Wednesday, March 9, 2016 2:27 PM
    Moderator