I am hoping below is helpfull for you, Ex: Get quote close description:
namespace CRMGETQUOTE
{
using Entities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Linq;
using System.ServiceModel;
public sealed class GetQuoteClose : CodeActivity
{
/// <summary>
/// Executes the workflow activity.
/// </summary>
/// <param name="executionContext">The execution context.</param>
protected override void Execute(CodeActivityContext executionContext)
{
// Create the tracing service
ITracingService tracingService = executionContext.GetExtension<ITracingService>();
if (tracingService == null)
{
throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
}
tracingService.Trace("Entered GetQuoteClose.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
executionContext.ActivityInstanceId,
executionContext.WorkflowInstanceId);
// Create the context
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
if (context == null)
{
throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
}
tracingService.Trace("GetQuoteClose.Execute(), Correlation Id: {0}, Initiating User: {1}",
context.CorrelationId, context.InitiatingUserId);
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
Quote quote = (Quote)service.Retrieve(QuoteToFind.Get(executionContext).LogicalName, QuoteToFind.Get(executionContext).Id, new ColumnSet(true));
EntityCollection closes = GetQuoteCloses(service, quote.Id);
if (closes.Entities.Count == 0)
{
QuoteCloseDesc.Set(executionContext, string.Empty);
return;
}
bool firstOrLast = FirstOrLast.Get(executionContext);
if (firstOrLast)
{
QuoteClose close = (QuoteClose)closes.Entities.First();
QuoteCloseDesc.Set(executionContext, close.Description);
}
else
{
QuoteClose close = (QuoteClose)closes.Entities.Last();
QuoteCloseDesc.Set(executionContext, close.Description);
}
}
catch (FaultException<OrganizationServiceFault> e)
{
tracingService.Trace("Exception: {0}", e.ToString());
throw;
}
tracingService.Trace("Exiting GetQuoteClose.Execute(), Correlation Id: {0}", context.CorrelationId);
}
private EntityCollection GetQuoteCloses(IOrganizationService service, Guid id)
{
//Query for the quote closes
QueryExpression query = new QueryExpression()
{
EntityName = QuoteClose.EntityLogicalName,
ColumnSet = new ColumnSet(true),
LinkEntities =
{
new LinkEntity
{
LinkFromEntityName = QuoteClose.EntityLogicalName,
LinkFromAttributeName = "quoteid",
LinkToEntityName = Quote.EntityLogicalName,
LinkToAttributeName = "quoteid",
LinkCriteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "quoteid",
Operator = ConditionOperator.Equal,
Values = { id }
}
}
}
}
}
};
return service.RetrieveMultiple(query);
}
[RequiredArgument]
[Input("Quote With Close")]
[ReferenceTarget("quote")]
public InArgument<EntityReference> QuoteToFind { get; set; }
[RequiredArgument]
[Input("Retrieve First (True) Or Last (False) Close")]
public InArgument<bool> FirstOrLast { get; set; }
[OutputAttribute("Quote Close")]
public OutArgument<string> QuoteCloseDesc { get; set; }
}
}