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)

Calculation using exchange rate and opportunity field using a Custom Activity Workflow

(1) ShareShare
ReportReport
Posted on by 1,257

Hi Knowledgeable team :)

I'm trying to do a calculation on opportunities, my requeriment is the following:

Use the Transaction currency entity, retrieve the one that is in euro and doesn't matter the currency of the opportunity calculate the base using the euro, the easiest way of course will be setting up the system currency base to euro, but the organization needs both, so the way is Custom development... So i have a field called First Year Value and that one is the field that i need to convert to euro... i'm trying the following, but it doesnt do anything, just execute, but doesn't update the field that i need to set a "euro base"

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CA.Opportunity.CalculateEuroFYCV
{
    public class EuroBaseFYCV : CodeActivity
    {
        [Input("Opportunity")]
        [ReferenceTarget("opportunity")]
        [RequiredArgument]
        public InArgument<EntityReference> InOpportunity { get; set; }
        public InArgument<EntityReference> InTransactionCurrency { get; set; }

        protected override void Execute(CodeActivityContext executionContext)
        {
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory factory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            this.Run(service, InOpportunity.Get(executionContext), InTransactionCurrency.Get(executionContext));
        }

        public void Run(IOrganizationService service, EntityReference OpportunityRef, EntityReference TransactionCurrencyref)
        {

            Entity eTransactionCurrency = service.Retrieve(TransactionCurrencyref.LogicalName, TransactionCurrencyref.Id, new ColumnSet("currencyname","transactioncurrencyid","exchangerate"));
            Entity eOpportunity = service.Retrieve(OpportunityRef.LogicalName, OpportunityRef.Id, new ColumnSet("opportunityid","",""));


            QueryExpression queryOpp = new QueryExpression(OpportunityRef.LogicalName);
            queryOpp.ColumnSet = new ColumnSet("new_firstyearvalue_base", "new_firstyearvalue", "opportunityid");
            queryOpp.Criteria.AddCondition("opportunityid", ConditionOperator.Equal, OpportunityRef.Id);
            queryOpp.LinkEntities.Add(new LinkEntity(OpportunityRef.LogicalName, "transactioncurrency", "opportunityid", "transactioncurrencyid", JoinOperator.Inner));
            queryOpp.LinkEntities[0].Columns = new ColumnSet("exchangerate","currencyname","transactioncurrencyid");

            EntityCollection ecOppData = service.RetrieveMultiple(queryOpp);

            if (ecOppData != null)
            {
                if (ecOppData.Entities.Count > 0)
                {
                    Entity eOppData = ecOppData.Entities[0];
                    decimal exchangerate = (decimal)((AliasedValue)eOppData["transactioncurrency.exchangerate"]).Value;

                    Entity eOpp = new Entity(OpportunityRef.LogicalName);
                    eOpp.Id = OpportunityRef.Id;
                    eOpp["new_eurofyv_base"] = exchangerate * ((Money)eOppData["new_firstyearvalue_base"]).Value;
                    
                    service.Update(eOpp);
                }
            }
        }
    }
}

So many thanks in advance for any help/clue you can give me :)

Best regards.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    I haven't tried it "yet" but it looks like its a CRM limitation to show all money fields  in a single currency i.e. if you have selected AUD on the record, then all the fields will be displayed  in AUD. Take a look at the discussion on this thread -community.dynamics.com/.../179158

    Now you have mentioned that the field is not updating at all which makes me think that a problem in your query results i.e. your query is not returning the ecOppData hence the check  if (ecOppData != null) and also                 if (ecOppData.Entities.Count > 0) doesn't return true hence no update is performed. But like I said, even if you update it may not work as crm displays the same currency for all currency field in the form/record.

    Also, a quick tip is that if you need to work on a workflow which requires parameter then always build your logic outside of workflow with hardcoding the parameters because sometimes the if you make any changes to parameters it doesn't reflect and you may think your logic is not working whereas crm will be executing the old cached version of your assembly. So I would suggest you to take out your core logic, put it in a console application and test it.

    Hope this helps.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Another thought is that if you make your euro field as decimal (i.e. not money) then you may be able to update the field.

  • Verified answer
    Vile Andreas Rantala Profile Picture
    1,257 on at

    the only way i found to achieve this is:

    1. adding a new currency field which will be populated always onload setting the currency that i need for calculations which is euro.
    2. in the link entity use this new field to retrieve the exchange rate on the plugin and do te calculation.

    3. works like a charm.

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