Friday, December 21, 2012

currency logic in ms crm 2011

just putting my observations here...
this is on basis of the currency filed type in MS CRM 2011
1. when we create one currency field, the currency lookup will we will get created and two fields get created one with extra as (base)
2. if you just add the currency fields (not the one with Base) and do preview page you will see the field as just a common text field (normally the currency symbol should come)
below image Inventories is the currency field

3. if you just put the Currency look up and select the currency the currency symbol will be populated in the currency filed.
see in the below image
4. if you create from CRM its fine and things will work proper if you assign any value to the currency field(not lookup) so currency symbol(ex $) value will come automatically .
5. how if you have some interface program which is creating the record?
    a. if you create the record and you push any value to the currency field(not look up) then its fine , things are ok
    b. but how if you did not put the value ,means is you have n number of currency fields but none of the fields has any value, then in this scenario, all the currency fields will be shown as textbox, so no currency symbol.
    Why this happened?
cuz, the currency look up field has not been assigned any value there so its showing as text field and if you add any value to the currency(not llok up) field you will get error, 1st assign the currency look up.
   how to solve?
so to hit this, you just get the currency value and assign in the lookup, so that the currency symbol will be shown in the currency fields and teh value can be added. below code is to fetch the currency guid from the system, that configured.

public static Guid GetDefaultCurrencyID(IOrganizationService service)
        {
            Guid CurrencyGuid = Guid.Empty;
 
            QueryExpression query = new QueryExpression(TransactionCurrency.EntityLogicalName);
            query.ColumnSet = new ColumnSet(new string[] { "transactioncurrencyid" });
            query.Orders.Add(new OrderExpression("createdon", OrderType.Ascending));
 
            try
            {
                EntityCollection oEntityCollection = service.RetrieveMultiple(query);
                if (oEntityCollection.Entities.Count > 0)
                {
                    CurrencyGuid = (Guid)oEntityCollection.Entities[0].Id;
                }
            }
            catch (Exception ex)
            {
                Util.ThrowException(ex);
            }
 
            return CurrencyGuid;
        }

hope this will help to anyone in any corner....

today is doomsday... if we pass this day , it will help to many more :P

Regards,
yes.sudhanshu

No comments:

Post a Comment