Tuesday, 25 June 2013

CRM 2011 Custom Workflow sample

 public class TestCodeActivity {
 
#region Input Properties

[Input("Task Subject")]
[Default("Empty Subject")]
public InArgument<string> TaskSubject { getset; }

[Input("Contract Lookup")]
[ReferenceTarget("contract")]
public InArgument<EntityReference>startContract { get; set;}
[Input("Contract Service Type Lookup")]
[ReferenceTarget("new_contractservicetype")]
public InArgument<EntityReference>contractServiceType { get; set; }
[Input("Component Template Lookup")]
[ReferenceTarget("new_componenttemplate")]
public InArgument<EntityReference>componentTemplate { get; set; }
[Output("Target Component")]
[ReferenceTarget("new_component")]
public OutArgument<EntityReference>targetComponent { get; set;}
[Output("Is Component Found Flag")]
public OutArgument<Boolean>IsFoundFlag { get; set;}

[Input("Activity Type")]
[AttributeTarget("appointment", "new_type")]
public InArgument<OptionSetValue>ActvType { get; set;}

[Input("End Date Time")]
public InArgument<DateTime>endDate { get; set;}
#endregion
 protected override void Execute(CodeActivityContext context) 
 {
 
      //Create the IWorkflowContext and the
     //IOrganizationService for communication with CRM
    IWorkflowContext workflowContext =context.GetExtension<IWorkflowContext>();
    IOrganizationServiceFactory Factory =context.GetExtension<IOrganizationServiceFactory>();
    IOrganizationService service =Factory .CreateOrganizationService(workflowContext.UserId);
   //Retrieve data from InputParameter 
    string newSubject = TaskSubject.Get<string>(context);
 
   //Create the new task object (in memory)
   Entity newTask = new Entity("task");
   newTask["subject"] = newSubject;
   newTask["regardingobjectid"] =new EntityReference("account", workflowContext.PrimaryEntity   Id);
 
   //Create task in CRM
   Guid taskId = service.Create(newTask);
  }
}



No comments:

Post a Comment