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.