web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Can You Update Entity Attribute In PostOperation?

(0) ShareShare
ReportReport
Posted on by

I have a plugin that is registered like this:

[CrmPluginRegistration(MessageNameEnum.Create,
    "contractdetail",
    StageEnum.PostOperation,
    ExecutionModeEnum.Asynchronous,
    "",
    "Post-Invoice Contract",
    1,
    IsolationModeEnum.Sandbox)]


I am trying to update an attribute within the target entity like this:

                    //Update Contract Line StartDate
                    Entity updatedContractLine = new Entity(entity.LogicalName)
                    {
                        Id = entity.Id
                    };
                    updatedContractLine["activeon"] = startDate;
                    service.Update(updatedContractLine);


I know that a PostOperation means that the created entity has already been committed to the database and I am fine with that. I want the "activeon" attribute to be updated after it is created. For some odd reason, the attribute is not updating! Can someone explain why?!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Ben Thompson Profile Picture
    6,350 on at

    I think we will need to see the rest of your plugin especially that are retrieving the entity details from the context. You are looking at the output parameters as this is running asynchronously after the record was created?

  • Verified answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at

    Hmm. This should work. Did you debug or can you add trace and see?

    Do you have any depth check or any asynchronous update plugin - check for any other exception which may rollback.

  • Community Member Profile Picture
    on at

    Here is the rest of the plugin:

    public class UnitPluginPostInvoice : IPlugin
        {
    
            public void Execute(IServiceProvider serviceProvider)
            {
                // Extract the tracing service for use in debugging sandboxed plug-ins.
                // Will be registering this plugin, thus will need to add tracing service related code.
    
                ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                //obtain execution context from service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                // InputParameters collection contains all the data passed in the message request. 
    
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
    
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    if (entity.LogicalName != "contractdetail")
                        return;
    
                    //obtain the organization service for web service calls.
                    IOrganizationServiceFactory serviceFactory =
                        (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
    
                    //Core Plugin code in Try Block
                    try
                    {
                        
    
                        //get contractid lookup field value
                        var contractLookup = entity.GetAttributeValue<EntityReference>("contractid");
    
                        //Retrieve associated contract
                        var contract = service.Retrieve(contractLookup.LogicalName, contractLookup.Id, 
                            new ColumnSet(true));
    
                        //if associated contract is not invoiced, return
                        if (contract.GetAttributeValue<OptionSetValue>("statecode").Value != 1)
                            return;
    
                        //Get Contract line start date
                        var startDate = DateTime.Today;
    
                        //Update Contract Line StartDate
                        Entity updatedContractLine = new Entity(entity.LogicalName)
                        {
                            Id = entity.Id
                        };
                        updatedContractLine["activeon"] = startDate;
                        service.Update(updatedContractLine);
    
    
                        //Get Contract Line End Date
                        DateTime endDate = (DateTime)entity["expireson"];
    
                        //Get Contract range into weekdays list
                        Eachday range = new Eachday();
                        var weekdays = range.WeekDay(startDate, endDate);
    
                        //Get Unit Order Lookup Id
                        EntityReference unitOrderLookup = (EntityReference)contract.Attributes["new_unitorderid"];
                        var unitOrderLookUpId = unitOrderLookup.Id;
    
                        //Get the contract Id to string for FetchXML
                        var contractId = contract.Id.ToString();
    
                        
                        //Retrieve Unit Order
                        var unitOrder = service.Retrieve(unitOrderLookup.LogicalName, unitOrderLookUpId,
                            new ColumnSet(true));
    
                        var unitOrderId = unitOrder.Id;
                        var uiName = unitOrder.GetAttributeValue<string>("new_name");


  • Community Member Profile Picture
    on at

    I will debug and report back to you!

  • Ben Thompson Profile Picture
    6,350 on at

    As you are post operation you should be using context.OutputParameters.Contains("Target") instead of the context.InputParameters.Contains("Target") you are using above.

    Post operation the target entity is not part of the InputParameters and is part of the OutputParameters so your plugin is currently triggering, checking for a target entity and then skipping the rest of the code as the target entity doesn't exist.

    Also (as an aside) on create the target entity in the input context does not contain the Id of the record as the id of the record only exists after its been created (hence it only appears in the output parameters).

  • David Jennaway Profile Picture
    14,065 on at

    To expand on the previous post, on a Create message, the Target InputParameter won't have the .Id set (unless it is explicitly passed in the Create). Instead, use the Id OutputParameter to get the Id of the new record

  • Community Member Profile Picture
    on at

    Your answer is incorrect!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans