Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Need help to write custom workflow activity

(0) ShareShare
ReportReport
Posted on by 255

I have Case entity: want to trigger custome workflow on change of owner field which sends one email to updated owner.

flow of entities are as follows :

Entity A(have subgrid of entity B)->Entity B(Have subgrid of related activities)-> Related activities(Appontments)

1) Entity A
1) having owner field: on change of this need to send email
2)has subgrid of entity B records

2)Entity B
->StepFiled(lookup)
->Status Field(Lookup)

email should get trigger based on following condition:

-->select last modified record Entity B subgrid where we have step == "value 1" and Status="value 1" 0r Status="value 2" or Status="value 3" for Entity A record

-->for this record we have related appointments -> we need to select last modified appointment --> then from this appointment we need to display some fields infromation in mail.

  • Mona Chavan Profile Picture
    Mona Chavan 255 on at
    RE: Need help to write custom workflow activity

    thanks Radu Chiribelea,

    will this work for CRM 2011.

    the code you shared is plugin code  right not a code for custom workflow activity?

  • Suggested answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Need help to write custom workflow activity

    Hi Mona,

    Based on your description i think this would be doable via MS Flow - please see [View:https://flow.microsoft.com/en-us/connectors/shared_dynamicscrmonline/dynamics-365/:750:50. I am not referring to the old UI Workflow engine. However MS Flow is available only for CRM Online - not sure what you are using.

    With regards to the code logic for the plug-in (you can use the same logic for either plugin or wf this could look something like below

        public class DemoPlugin : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider.  
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                // Get a reference to the Organization service.
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    try
                    {
                        Entity entityA = context.InputParameters["Target"] as Entity;
    
                        //Get child EntityB
                        QueryExpression entityBQuery = new QueryExpression("EntityB");
                        //Get only child records
                        entityBQuery.Criteria.AddCondition("entityALookup", ConditionOperator.Equal, entityA.Id);
    
                        //Apply filter criteria 
                        entityBQuery.Criteria.AddCondition("step", ConditionOperator.Equal, "abc");
                        entityBQuery.Criteria.AddCondition("status", ConditionOperator.Equal, "pqr");
    
                        //To get the last modified
                        entityBQuery.AddOrder("modifiedon", OrderType.Descending);
                        entityBQuery.TopCount = 1;
    
                        Entity entityB = service.RetrieveMultiple(entityBQuery).Entities.FirstOrDefault();
    
                        //proceed only if entity B exists
                        if (entityB != null)
                        {
                            QueryExpression activitiesQuery = new QueryExpression("activitypointer");
                            activitiesQuery.ColumnSet = new ColumnSet(true);
    
                            //Fetch only appointments
                            LinkEntity appointmentLink = activitiesQuery.AddLink("appointment", "activityid", "activityid");
                            appointmentLink.Columns = new ColumnSet(true);
    
                            //To get the last modified
                            activitiesQuery.AddOrder("modifiedon", OrderType.Descending);
                            activitiesQuery.TopCount = 1;
    
                            Entity appointment = service.RetrieveMultiple(activitiesQuery).Entities.FirstOrDefault();
    
                            //Only if child activity exists
                            if (appointment != null)
                            {
    
    
                                //Set sender and recipient (instantiate EntityReference accordingly)
                                Entity from = new Entity("activityparty");
                                from["partyid"] = new EntityReference();
    
                                Entity to = new Entity("activityparty");
                                to["partyid"] = new EntityReference();
    
    
                                Entity email = new Entity("email");
    
                                //copy here attributes from appointment entity
    
                                // set actitivy party
                                email["from"] = new Entity[] { from };
                                email["to"] = new Entity[] { to };
    
                                //create
                                Guid emailId = service.Create(email);
    
                                //send the email
                                SendEmailRequest sendEmailreq = new SendEmailRequest
                                {
                                    EmailId = emailId,
                                    IssueSend = true
                                };
    
                                service.Execute(sendEmailreq);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidPluginExecutionException($"Error during plugin execution: {ex.Message}");
                    }
                }
            }
        }

    This however needs to be adapted to the entities that are being used in your CRM Organization.

    Please keep in mind that the code as is above is only an example and needs refactoring before being shipped into production (handle cases where child records don't exist, extract methods, etc.) It's written purely as an example to be able to view the flow.

    However if you are uncomfortable with writing code, then i suggest reaching out to an MS Partner for assistance.

    Hope this helps you,

    Radu

  • Mona Chavan Profile Picture
    Mona Chavan 255 on at
    RE: Need help to write custom workflow activity

    Hi Radu Chiribelea,

    sorry but i am not able to achieve this using workflow because i am not getting third level entity in workflow selection

    here is the clear scenario i am posting using image can you help me in some way .

    8422.ex1.png

    let me know if your not getting the scenario.

  • Suggested answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Need help to write custom workflow activity

    Hi Mona,

    I think the requirements would benefit from some more details - maybe a diagram that explains how your relationships are set up and what you would like to obtain.

    In addition to the above, have you considered MS Flow? You might achieve this with no code at all.

    Regards,

    Radu

  • Mona Chavan Profile Picture
    Mona Chavan 255 on at
    RE: Need help to write custom workflow activity

    can anybody please help me on above workflow activity?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans