Beispiel #1
0
        /// <summary>
        /// This method first creates a new currency within the system, setting its
        /// exchange rate to a pre-defined value. It then issues a
        /// RetrieveExchangeRateRequest to get the exchange rate from the created
        /// currency to the organization's base currency. Finally, it retrieves the
        /// organization's base currency and displays the conversion rate.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>

        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                //<snippetTransactionCurrencyExchangeRate1>
                // Connect to the Organization service.
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
                    // using the service context makes retrieving entities easier
                    using (_context = new ServiceContext(_serviceProxy))
                    {
                        // This statement is required to enable early-bound type support.
                        _serviceProxy.EnableProxyTypes();

                        String currentOrganizatoinUniqueName = GetCurrentOrganizationName(serverConfig);

                        CreateRequiredRecords();

                        RetrieveExchangeRateRequest request = new RetrieveExchangeRateRequest()
                        {
                            TransactionCurrencyId = _currency.Id
                        };
                        RetrieveExchangeRateResponse response =
                            (RetrieveExchangeRateResponse)_serviceProxy.Execute(request);
                        Console.WriteLine("  Retrieved exchange rate for created currency");

                        // get the base currency for the current org
                        var baseCurrencyName =
                            (from currency in _context.TransactionCurrencySet
                             join org in _context.OrganizationSet
                             on currency.Id equals org.BaseCurrencyId.Id
                             where org.Name == currentOrganizatoinUniqueName
                             select currency.CurrencyName).FirstOrDefault();
                        Console.WriteLine("  This organization's base currency is {0}",
                                          baseCurrencyName);

                        Console.WriteLine(
                            "  The conversion from {0} -> {1} is {2}",
                            _currency.CurrencyName,
                            baseCurrencyName,
                            response.ExchangeRate);

                        DeleteRequiredRecords(promptforDelete);
                    }
                //</snippetTransactionCurrencyExchangeRate1>
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }
        internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef)
        {
            var request = MakeRequest <RetrieveExchangeRateRequest>(orgRequest);

            var row = db.GetEntityOrNull(new EntityReference("transactioncurrency", request.TransactionCurrencyId));

            var resp = new RetrieveExchangeRateResponse();

            resp.Results["ExchangeRate"] = row?.GetAttributeValue <decimal>("exchangerate") ?? throw new FaultException($"transactioncurrency With Id = {request.TransactionCurrencyId} Does Not Exist");
            return(resp);
        }
Beispiel #3
0
        /// <summary>
        /// Retrieve specified <c>Transaction Currency</c> exchange rate.
        /// <para>
        /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.retrieveexchangeraterequest(v=crm.8).aspx
        /// </para>
        /// </summary>
        /// <param name="transactioncurrencyId"></param>
        /// <returns>
        /// <see cref="decimal"/> Exchange Rate
        /// </returns>
        public decimal GetExchangeRate(Guid transactioncurrencyId)
        {
            ExceptionThrow.IfGuidEmpty(transactioncurrencyId, "transactioncurrencyId");

            RetrieveExchangeRateRequest request = new RetrieveExchangeRateRequest()
            {
                TransactionCurrencyId = transactioncurrencyId
            };

            RetrieveExchangeRateResponse serviceResponse = (RetrieveExchangeRateResponse)this.OrganizationService.Execute(request);

            return(serviceResponse.ExchangeRate);
        }
        [STAThread] // Added to support UX
        static void Main(string[] args)
        {
            CrmServiceClient service = null;

            try
            {
                service = SampleHelpers.Connect("Connect");
                if (service.IsReady)
                {
                    #region Sample Code
                    //////////////////////////////////////////////
                    #region Set up
                    SetUpSample(service);
                    #endregion Set up
                    #region Demonstrate
                    //String un = service.
                    String currentOrganizatoinUniqueName = service.ConnectedOrgFriendlyName;

                    RetrieveExchangeRateRequest request = new RetrieveExchangeRateRequest()
                    {
                        TransactionCurrencyId = _currency.Id
                    };
                    RetrieveExchangeRateResponse response =
                        (RetrieveExchangeRateResponse)service.Execute(request);
                    Console.WriteLine("  Retrieved exchange rate for created currency");

                    context = new ServiceContext(service);
                    // get the base currency for the current org
                    var baseCurrencyName =
                        (from currency in context.TransactionCurrencySet
                         join org in context.OrganizationSet
                         on currency.Id equals org.BaseCurrencyId.Id
                         where org.Name == currentOrganizatoinUniqueName
                         select currency.CurrencyName).FirstOrDefault();
                    Console.WriteLine("  This organization's base currency is {0}",
                                      baseCurrencyName);

                    Console.WriteLine(
                        "  The conversion from {0} -> {1} is {2}",
                        _currency.CurrencyName,
                        baseCurrencyName,
                        response.ExchangeRate);


                    #region Clean up
                    CleanUpSample(service);
                    #endregion Clean up
                }
                #endregion Demonstrate
                else
                {
                    const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Dynamics CRM";
                    if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR))
                    {
                        Console.WriteLine("Check the connection string values in cds/App.config.");
                        throw new Exception(service.LastCrmError);
                    }
                    else
                    {
                        throw service.LastCrmException;
                    }
                }
            }
            #endregion Sample Code
            catch (Exception ex)
            {
                SampleHelpers.HandleException(ex);
            }

            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }

                Console.WriteLine("Press <Enter> to exit.");
                Console.ReadLine();
            }
        }