Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Plugin which runs after record save

(0) ShareShare
ReportReport
Posted on by 85

Hi,

I am creating a plugin which runs after saving a record.

1. now how do I get a particular field of the record which is being saved.

2. how to get records of another entity based on some condition ?

public void Execute(IServiceProvider serviceProvider)
        {

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService orgService = factory.CreateOrganizationService(context.UserId);
            ITracingService traceService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = context.InputParameters["Target"] as Entity;
                
                // Verify that the target entity represents an account.
                // If not, this plug-in was not registered correctly.
                if (entity.LogicalName != "contact")
                    return;
                
                //logic here
                
            }
        }

  • RG Student Profile Picture
    RG Student 85 on at
    RE: Plugin which runs after record save

    Hi Omar,

    Thanks for your response.

  • Verified answer
    OmarZaarour Profile Picture
    OmarZaarour 1,705 on at
    RE: Plugin which runs after record save

    Hi,

    If my understanding is correct, your plugin runs on the Create message, right? Or also on the update message?

    If it is on the Create Message, then your "entity" will have all the attributes you need. You can get the attribute like this:

    var someAttValue = entity.GetAttributeValue<Type>("logicalNameOfYourAttribute");

    as an example of this, if you want to get the value of an attribute named "abc_name", and it is of type string, you would write:

    var name=entity.GetAttributeValue<string>("abc_name");

    Now, if your plugin is registered on the Update message as well, the "entity" object will only contain the the attributes that got modified only. This means, you would want to issue another call to get the values that are not included in the execution context.

    To get a record of another entity, see the example here docs.microsoft.com/.../use-queryexpression-class

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans