Created
February 13, 2018 13:25
-
-
Save crmaddicted/350a7deae522b1a88e4f72d6e0db60dd to your computer and use it in GitHub Desktop.
Custom Workflow activity to Associate records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xrm.Sdk; | |
using Microsoft.Xrm.Sdk.Workflow; | |
using System; | |
using System.Activities; | |
using Microsoft.Xrm.Sdk.Query; | |
namespace CRMWorkflowProject1 | |
{ | |
public class AssociateOpportunityMembers : CodeActivity | |
{ | |
// Input parameter für Requests an datahub definieren | |
[RequiredArgument] | |
[Input("Hauptbetreuer")] | |
[ReferenceTarget("tt_mitarbeiter")] | |
public InArgument<EntityReference> Hauptbetreuer { get; set; } | |
protected override void Execute(CodeActivityContext executionContext) | |
{ | |
ITracingService tracer = executionContext.GetExtension<ITracingService>(); | |
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); | |
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); | |
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); | |
try | |
{ | |
// Create a new entity reference for the main account manager including GUID and name | |
EntityReference Employee = Hauptbetreuer.Get<EntityReference>(executionContext); | |
Entity EmployeeName = service.Retrieve("tt_mitarbeiter", Employee.Id, new ColumnSet("tt_name")); | |
Employee.Name = EmployeeName.GetAttributeValue<string>("tt_name"); | |
// Check if account manager has already been associated | |
ConditionExpression condition1 = new ConditionExpression(); | |
condition1.AttributeName = "opportunityid"; | |
condition1.Operator = ConditionOperator.Equal; | |
condition1.Values.Add(context.PrimaryEntityId); | |
ConditionExpression condition2 = new ConditionExpression(); | |
condition2.AttributeName = "tt_mitarbeiterid"; | |
condition2.Operator = ConditionOperator.Equal; | |
condition2.Values.Add(Employee.Id); | |
FilterExpression filter1 = new FilterExpression(); | |
filter1.Conditions.Add(condition1); | |
filter1.Conditions.Add(condition2); | |
QueryExpression query = new QueryExpression("pb_opportunity_tt_mitarbeiter"); | |
query.ColumnSet.AddColumns("tt_mitarbeiterid"); | |
query.Criteria.AddFilter(filter1); | |
EntityCollection associates = service.RetrieveMultiple(query); | |
// Associate Account Manager to opportunity in case he/she hasn't been added so far | |
if (associates.Entities.Count == 0) | |
{ | |
EntityReferenceCollection relatedEntities = new EntityReferenceCollection(); | |
relatedEntities.Add(Employee); | |
Relationship relationship = new Relationship("pb_opportunity_tt_mitarbeiter"); | |
service.Associate(context.PrimaryEntityName, context.PrimaryEntityId, relationship, relatedEntities); | |
} | |
} | |
catch (Exception e) | |
{ | |
throw new InvalidPluginExecutionException(e.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment