Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Getting Deleted Record Id With a Custom Workflow

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi,

I have a custom workflow to update a custom entity' two fields when you delete a row from the sub grid(another custom entity). The problem I am struggling is I can not get the deleted record id neither with 

Entity entity = (Entity)context.InputParameters["Target"];  
string id = entity.Id.ToString();

nor 

IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
string id =  context.PrimaryEntityId.ToString();

context returns null all the time. Can someone please give me any suggestion about it.
Many thanks.

*This post is locked for comments

  • Inogic Profile Picture
    Inogic 438 on at
    RE: Getting Deleted Record Id With a Custom Workflow

    Hi Ezgi,

    Storing deleted record ID is something different requirement. If you want to do so for any record maintenance, It would be good if you write a preplugin on entity.

    You will find the record Id within plugin and can save it to your custom entity.

    Hope this helps for you.

    Thanks!
    Sam

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Deleted Record Id With a Custom Workflow

    Thanks a lot Yadnyesh, I totally missed that part. Now it works well.

  • Verified answer
    Yadnyesh Kuvalekar Profile Picture
    Yadnyesh Kuvalekar 4,102 on at
    RE: Getting Deleted Record Id With a Custom Workflow

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

    Here you should check for EntityReference.

    Below is corrected code:

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)

  • Suggested answer
    Yadnyesh Kuvalekar Profile Picture
    Yadnyesh Kuvalekar 4,102 on at
    RE: Getting Deleted Record Id With a Custom Workflow

    FYI, roll up fields get updated every 24 hours. So if you have tried Roll up fields for your requirement and if you tried to check the amount immediately, then you might have not seen updated value. If this is the case, you can refresh the value manually by opening the record and clicking refresh button right besides the field, or you can just write plugin to forcefully update the field (msdn.microsoft.com/.../dn817863.aspx).

    But I would still recommend to use roll up fields to calculate amounts on parent as it is fool proof solution.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Deleted Record Id With a Custom Workflow

    Here is the code ;

    using Microsoft.Xrm.Sdk;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Plugins
    {
        public class PrimDetayDelete : IPlugin
        {
            SqlConnection con = Baglanti.CrmDB();
    
            public void Execute(IServiceProvider serviceProvider)
            {
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
               
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    EntityReference entity = (EntityReference)context.InputParameters["Target"];
                    
                    try
                    {
                        string bonusDetailId = RetrieveBonusDetailIdByBonusPaymentId(entity.Id.ToString());                    
                        UpdateBonusDetailByBonusDetailId(bonusDetailId, entity.Id.ToString());          
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidPluginExecutionException(ex.Message);
                    }
                }
            }
    
            private string RetrieveBonusDetailIdByBonusPaymentId(string bonusPaymentId)
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter("select  new_primdetay as primDetayId from new_primodemeleri where new_primodemeleriId = @id", con);
                dataAdapter.SelectCommand.CommandType = CommandType.Text;
                dataAdapter.SelectCommand.Parameters.AddWithValue("@id", bonusPaymentId);
                DataTable dataTable = new DataTable();
                dataAdapter.Fill(dataTable);
    
                return dataTable.Rows[0]["primDetayId"].ToString();
            }
    
            private void UpdateBonusDetailByBonusDetailId(string bonusDetailId, string bonusPaymentId)
            {
                SqlCommand cmd = new SqlCommand(" UPDATE new_primdetay SET new_odenentutar = (select  SUM(new_odenentutar) from new_primodemeleri where new_primdetay = @id and new_primodemeleriId != @primodId), new_primdetay.new_kalantutar = (new_personelprimi - (select  SUM(new_odenentutar) from new_primodemeleri where new_primdetay = @id and new_primodemeleriId != @primodId))   WHERE new_primdetayId = @id", con);
                cmd.Parameters.AddWithValue("@id", bonusDetailId);
                cmd.Parameters.AddWithValue("@primodId", bonusPaymentId);
                con.Open();
                cmd.CommandTimeout = 1000;
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }

    here is the screenshots;

    6406.scrn2.PNG

    6406.scrn2.PNG

    Thanks

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Getting Deleted Record Id With a Custom Workflow

    Ezgi,

    I believe when child record is created/updated/deleted you have to update parent record in some way. I always use Plugins for that purpose and it worked fine for me all the time. Can you please provide code of your plugin and screenshot of your step from Plugin registration Tool?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Deleted Record Id With a Custom Workflow

    Hi Tim,

    I tried to do it with pre-operation plugin like you said but for some reason it is not triggered.

    Many Thanks

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Deleted Record Id With a Custom Workflow

    Hi Yadnyesh,

    Also tried that way but didn't work.

    Thanks

  • Suggested answer
    Yadnyesh Kuvalekar Profile Picture
    Yadnyesh Kuvalekar 4,102 on at
    RE: Getting Deleted Record Id With a Custom Workflow

    Hi,

    I believe, there is parent child relationship between BonusDetail and BonusPayment entities. If so and if your CRM version is above CRM 2015 Update 1, then you should use Rollup Fields and calculated fields concept for such requirements. It does not require any plugins and is foolproof solution.

    Below are some reference links:

    technet.microsoft.com/.../dn832162.aspx

    msdn.microsoft.com/.../dn817863.aspx

    Kindly tick verify answer if this solves your purpose.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting Deleted Record Id With a Custom Workflow

    Hi Andrii,

    I have a BonusDetail entity and a BonusPayment entity. These are both custom entities. When you add a new payment or delete a payment from BonusPayment subgrid, I need to update payment amount fields on BonusDetail entity(calculate payments and update payment amount on BonusDetail). When you add new payment the plugin I wrote works well to update BonusDetail entity but for some reason when you delete a row from BonusPayment subgrid it doesn't update the BonusDetail entity. By the way I have two plugins one for adding new payment second for deleting payment(also tried workflow but it also didnt work).

    Thanks for your time and help

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans