locked
CRM 2011 Custom Workflow Code activities RRS feed

  • Question

  • I am trying to perform some custom operation on the email, while it is received, i have module which scan it for the data i need, but i cannot understand few things in crm 2011 workflow.

    1. how do i access context entity data, ie, email of sender, subject, direction, description, i dont see any classes.

    2. how do i deal with party list in this.

    3. i need some code snippet in terms of syntax because it was pretty staright forward in crm 4.

    4. how to acces context.inputparameters and output parameteres and propertybags in custom workflow activities.

    please help.


    Thanks, Bharatkumar. Save Nature For Future.
    Wednesday, September 28, 2011 11:51 AM

Answers

  • two things you can use Entity which is equavalent to dynamicentity in crm4.0

    you can do all those things using dynamicentity/Entity.

    secondly, you can use crmsvcutil to create strongly types classes for early binding.

    Check the sdk on how to add this generated classes to plugin or workflow

    This will sort the problem out.

    I hope this helps.


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    Wednesday, September 28, 2011 1:05 PM

All replies

  • get the id from the context and you can retrive the rest of field in your code

    IWorkflowContext.PrimaryEntityId property

    I beleieve you can get the entity from the context as we do in plugins

    I find this example

    http://mohanswargam.wordpress.com/2011/06/10/step-by-step-my-first-workflow-example-for-crm-2011/

     

    I hope this helps.


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    • Proposed as answer by Amreek Singh Wednesday, September 28, 2011 12:14 PM
    • Edited by Amreek Singh Saturday, January 26, 2013 1:04 AM
    Wednesday, September 28, 2011 12:13 PM
  • Thanks, amreek,

     

    also if you can guide me on how to deal with activity Party for email entities, like how to get them and how to set them,

    i want from email address and to email address, the problem is my solution is behaving horrible, i am working on late binding classes.

     


    Thanks, Bharatkumar. Save Nature For Future.
    Wednesday, September 28, 2011 12:19 PM
  • Moreover when i debug i am unable to find the target in input parameter, see below.

     

    That is why i have raised the question and feels like i am doing something seriously wrong.


    Thanks, Bharatkumar. Save Nature For Future.
    Wednesday, September 28, 2011 12:38 PM
  • It works exactly the same way as in plugins, they both use the IExecutionContext interface to get all the context information such as the images, input parameters, etc. In the preentityimages collection you will be able to access the primary entity. If you need to access another entity you'd have to pass it as a parameter to your custom workflow activity.

    Now to work with activity parties, you should check some of the samples in the SDK documentation that explain how these work. Here is a sample form the SDK:

     

    ActivityParty toParty = new ActivityParty
                        {
                            PartyId = new EntityReference(Contact.EntityLogicalName, _contactId)
                        };
    
                        Console.WriteLine("Created activity parties.");
                
                        // Create an e-mail message.
                        Email email = new Email
                        {
                            To = new ActivityParty[] { toParty },
                            From = new ActivityParty[] { fromParty },
                            Subject = "SDK Sample e-mail",
                            Description = "SDK Sample for SendEmailFromTemplate Message.",
                            DirectionCode = true
                        };
    

     


    Gonzalo | gonzaloruizcrm.blogspot.com

    Wednesday, September 28, 2011 12:38 PM
    Moderator
  • I think it would be the same as we get the value in plugins

    once you have the entity

    something like this but I did not test it

    if (entity.Attributes.Contains("fieldname")

    {

    ActivityParty[] parties= entity.Attributes["fieldname"]

    }

    Then go through the collection to get individual partylist

    Check the sdk sample for better understanding. sample has some code on how to work with partylist fields

    sdk\samplecode\cs\businessdatamodel\activities

    I hope this helps.

     

     

     

    var = de.


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    • Proposed as answer by Amreek Singh Wednesday, September 28, 2011 12:47 PM
    Wednesday, September 28, 2011 12:47 PM
  • Agree!!!

     but at the first point, i am not been able to use Typed classes, in that case how you deal with it.


    Thanks, Bharatkumar. Save Nature For Future.
    Wednesday, September 28, 2011 12:50 PM
  • I never really tried it. It should work .but worse comes to worse you can use the entityid to retrieve the entity and get all the values.

    I hope this helps.


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    • Edited by Amreek Singh Wednesday, September 28, 2011 1:06 PM
    Wednesday, September 28, 2011 12:53 PM
  • Thanks, amreek

    share if you have any other pointers, may be i get something from there, i am looking towards plugin like operation on executing entity in Custom workflow activity.

    Thanks for all you help.

     

     


    Thanks, Bharatkumar. Save Nature For Future.
    Wednesday, September 28, 2011 12:55 PM
  • Any reason why you want to use late-bound entities? It makes everything easier if you use the early-boud. You just need to use crmsvcutil to generate the code.

    Gonzalo | gonzaloruizcrm.blogspot.com

    Wednesday, September 28, 2011 1:02 PM
    Moderator
  • two things you can use Entity which is equavalent to dynamicentity in crm4.0

    you can do all those things using dynamicentity/Entity.

    secondly, you can use crmsvcutil to create strongly types classes for early binding.

    Check the sdk on how to add this generated classes to plugin or workflow

    This will sort the problem out.

    I hope this helps.


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    Wednesday, September 28, 2011 1:05 PM
  • Thats valuable but, i am sort of in a stage where i can not use strongly typed class otherwise :).


    Thanks, Bharatkumar. Save Nature For Future.
    Wednesday, September 28, 2011 1:09 PM
  • Hi

    I'm have a similar problem.  I've created a workflow that fires when a attribute has a specific value.  The aim of the workflow is to send an email to a specific team.  I have a custom assembly that returns the members of the team in the format: [prefix]guid id;guid id;.  Here is an example:

    [systemuser]793d2fe0-53dd-e011-9566-005056ae0006;ba28ff45-54dd-e011-9566-005056ae0006;
    

     

    My workflow then creates a new record of type email and adds the above string to a custom attribute: receipients.  I have a plugin registerd on the Create of the email entity that should locate the above string, parse it and insert the users into the To field of the email.  The problem that I having is that nothing is occuring with the To, CC or BCC fields.  I am able to modify the contents of the Subject and Description and others if necessary.  The prefix is in place to allow the plugin to be generic and reused for customers if necessary.  Here is the code that I'm using in the plug in.  I'm using early bound classes:

     

            public void Execute(IServiceProvider serviceProvider)
            {
                // Get the IPlugin Context
                var context = (IPluginExecutionContext) serviceProvider.GetService(typeof (IPluginExecutionContext));
    
                var serviceFactory =
                    (IOrganizationServiceFactory) serviceProvider.GetService(typeof (IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                var tracingService = (ITracingService) serviceProvider.GetService(typeof (ITracingService));
    
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    var entity = (Entity) context.InputParameters["Target"];
    
                    var oEmail = entity.ToEntity<Lema.Email>();
    
    
                    if (entity.LogicalName == "email")
                    {
                        string entityName = oEmail.epa_Receipient.Substring(1,
                                                                            oEmail.epa_Receipient.
                                                                                IndexOf(']') - 1);
                        oEmail.Subject = "Extracting entity prefix.";
                        string target =
                            oEmail.epa_Receipient.Substring(oEmail.epa_Receipient.IndexOf(']') + 1);
                        oEmail.Subject = "Extracting target list.";
    
                        //ensure that the last character isn't a ";".
                        if (target.Substring(target.Length - 1) == ";")
                        {
                            target = target.Substring(0, target.Length - 1);
                        }
    
                        //breakdown the list into an array.
                        string[] targetList = target.Split(';');
                        oEmail.Subject = "Substring" + targetList.Length.ToString();
    
                        try
                        {
                            //Add each person to the To list.
                            IList<ActivityParty> receipients = new List<ActivityParty>();
    
                            foreach (string systemUserId in targetList)
                            {
                                var user = new ActivityParty { PartyId = new EntityReference(SystemUser.EntityLogicalName, new Guid(systemUserId)) };
                                receipients.Add(user);
                            }
    
                            oEmail.Subject = "Preparing attributes...";
                            oEmail.To = receipients;
                            oEmail.Category = "Test";
                            oEmail.Description = "updating something else...";
                        }
                        catch (Exception e)
                        {
                            tracingService.Trace("error message: " + e.Message);
                            throw;
                        }
                    }
                }
            }
    


    Appreciate any points you can give.

     

    Michael.

    Thursday, November 3, 2011 2:38 PM
  • EntityCollection

    Recipients = email.GetAttributeValue<EntityCollection>("from");

               

    EntityReferenceparty = (Recipients.Entities.Count > 0) ? Recipients.Entities.FirstOrDefault<Entity>().GetAttributeValue<EntityReference>("partyid") : null;

              

    Friday, January 25, 2013 4:03 PM