I will made it using custom workflow action. So the code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.Activities;
using Microsoft.Crm.Workflow;
using System.Workflow.ComponentModel;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
namespace OpportunityProductPlugin
{
[CrmWorkflowActivity("Get opportunity close record", "Opportunity utiles")]
public class OpportunityCloseGetter : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
if (Opportunity != null)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext workflowContext = contextService.Context;
ICrmService crmservice = workflowContext.CreateCrmService();
//Building the query to get opportunity close record
QueryByAttribute query = new QueryByAttribute();
query.Attributes = new string[] { "opportunityid" };
query.ColumnSet = new AllColumns();
query.EntityName = EntityName.opportunityclose.ToString();
query.Values = new object[] { Opportunity.Value };
//Retrieving the data
List<BusinessEntity> entities = crmservice.RetrieveMultiple(query).BusinessEntities;
//Check that opportunity close record exists
if (entities.Count == 1)
{
//Filling output lookup
OpportunityClose = new Lookup(EntityName.opportunityclose.ToString(),((opportunityclose)entities[0]).activityid.Value);
}
}
return ActivityExecutionStatus.Closed;
}
public static DependencyProperty OpportunityProperty = DependencyProperty.Register("Opportunity", typeof(Lookup), typeof(OpportunityCloseGetter));
[CrmInput("Opportunity")]
[CrmReferenceTarget("opportunity")]
public Lookup Opportunity
{
get
{
return (Lookup)base.GetValue(OpportunityProperty);
}
set
{
base.SetValue(OpportunityProperty, value);
}
}
public static DependencyProperty OpportunityCloseProperty = DependencyProperty.Register("OpportunityClose", typeof(Lookup), typeof(OpportunityCloseGetter));
[CrmOutput("OpportunityClose")]
[CrmReferenceTarget("opportunityclose")]
public Lookup OpportunityClose
{
get
{
return (Lookup)base.GetValue(OpportunityCloseProperty);
}
set
{
base.SetValue(OpportunityCloseProperty, value);
}
}
}
}
How to use it - I build simple on-demand workflow:
![](http://1.bp.blogspot.com/_73OmG38HHME/S3PC9ftmETI/AAAAAAAAAg8/9R8p6Tj1144/s400/1.jpg)
![](http://3.bp.blogspot.com/_73OmG38HHME/S3PC9exMoCI/AAAAAAAAAg0/3WyLcipJEgQ/s400/2.jpg)
![](http://1.bp.blogspot.com/_73OmG38HHME/S3PC9GMzadI/AAAAAAAAAgs/kVoI5sm2qBI/s400/3.jpg)
![](http://3.bp.blogspot.com/_73OmG38HHME/S3PCrJgVJZI/AAAAAAAAAgk/jZkdACrYq4o/s400/4.jpg)
![](http://4.bp.blogspot.com/_73OmG38HHME/S3PCq9eP11I/AAAAAAAAAgc/2xoEWC4CnXk/s400/5.jpg)
![](http://2.bp.blogspot.com/_73OmG38HHME/S3PCqjTmekI/AAAAAAAAAgU/hko726P1roE/s400/6.jpg)
![](http://2.bp.blogspot.com/_73OmG38HHME/S3PCqdMPyaI/AAAAAAAAAgM/LcG6CgEIxWY/s400/7.jpg)
![](http://4.bp.blogspot.com/_73OmG38HHME/S3PCqMqW4rI/AAAAAAAAAgE/zPEgcolQy_4/s400/8.jpg)
Source code and ready-to-deploy assembly can be downloaded here.
No comments:
Post a Comment