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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Hi, community i have to write a plugin "Restrict the deletion of an Opportunity if there exists a Quote associated with it in a 'Draft' state."

(0) ShareShare
ReportReport
Posted on by 154

Hi, please let me know how we can resolve this issue with this plugin.

I have the same question (0)
  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    Hi,

    You can do it with a pre-validation plugin on delete message. Throw an InvalidPluginExecutionException exception if the opportunity has quotes in draft state. See: www.youtube.com/watch

    Or you can create a real time workflow that trigger on pre-delete. See Goutam Das:'s answer:  community.dynamics.com/.../prevent-delete-of-record

  • Tushar Jadhav Profile Picture
    154 on at

    Thankyou for replay. but first we have to retrieve the quote that will related to particular opportunity right?? like this

    @"<fetch>" +

                             "<entity name='quote'>" +

                            "<attribute name='opportunityid' />" +

                            "<attribute name='statecode' />" +

                            "<filter>" +

                            "<condition attribute='opportunityid' operator='eq' value='" + target + "' />" +

                            "<condition attribute='statecode' operator='eq' value='0' />" +

                            "</filter>" +

                            "</entity>" +

                            "</fetch>";

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    // Get the opportunity from the input parameter "Target"
    var opportunityRef = execContext.InputParameters["Target"] as EntityReference;
    
    var hasDraftQuotesQuery = new FetchExpression($@"
        
            
                
                
                    
                    
                
            
        
    ");
    
    var result = orgService.RetrieveMultiple(hasDraftQuotesQuery);
    
    if (result.Entities.Count > 0)
        throw new InvalidPluginExecutionException("Delete an opportunity with draft quotes is not allowed.");

  • Tushar Jadhav Profile Picture
    154 on at

    Thankyou for replay delete operation run on stage 40??

    post operation or else pre validation.??

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    You can run it on pre-validation (Stage 10) to trigger the error as soon as possible in the pipeline. Also, you don't need to be in the transaction.

    To learn more: community.dynamics.com/.../when-to-use-the-pre-validation-stage-in-the-plugin-pipeline

  • Tushar Jadhav Profile Picture
    154 on at

    Still facing error please help me for this.

    Error:- System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: An exception System.FormatException was thrown while trying to convert input value '[REDACTED]' to attribute 'quote.opportunityid'. Expected type of attribute value: System.Guid. Exception raised: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). (Fault Detail is equal to Exception details:
    ErrorCode: 0x80040224
    Message: An exception System.FormatException was thrown while trying to convert input value '[REDACTED]' to attribute 'quote.opportunityid'. Expected type of attribute value: System.Guid. Exception raised: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
    TimeStamp: 2023-05-18T13:03:11.3085608Z
    OriginalException: PluginExecution
    ExceptionSource: PluginExecution
    --
    ).

    Code:-

    case "Delete_20":

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

                           {

                               tracer.Trace("hi1");

                               target = context.InputParameters["Target"] as Entity;

                               tracer.Trace("hi2");

                               EntityReference opportunityReference = context.InputParameters["Target"] as EntityReference;

                               GetDraftQuote(opportunityReference, service);

                               tracer.Trace("hi3");          

                           }

                               break;

                       default:

                           break;

      private void GetDraftQuote(EntityReference opportunityReference, IOrganizationService service)

           {

               string Query = @"<fetch>" +

                             "<entity name='quote'>" +

                            "<attribute name='opportunityid' />" +

                            "<attribute name='statecode' />" +

                            "<filter type='and'>" +

                            "<condition attribute='opportunityid' operator='eq'  value='" + opportunityReference + "' />" +

                             "<condition attribute='statecode' operator='eq' value='0' />" +

                            "</filter>" +

                            "</entity>" +

                           "</fetch>";

               EntityCollection result = service.RetrieveMultiple(new FetchExpression(Query));

               if (result.Entities.Count > 0)

               {

                   throw new InvalidPluginExecutionException("Delete an opportunity with draft quotes is not allowed.");

               }

           }

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    EntityReference is a structure with an Id property of type Guid.

    You need to use it in the fetch:

    ""

  • the12stringer Profile Picture
    5 on at

    Can't you just use plugin registration in XRM toolbox. Upon deletion of a specific type of record then throw a webhook. Use this webhook in power automate to create a flow and funnel all the specifics of a deletion or not. Even perhaps emailing the user of why something wasn't deleted?

    I guess it just depends on the speed you need, where this record is being deleted from, etc.

  • Tushar Jadhav Profile Picture
    154 on at

    For the Delete Operation:-

    Why we not using "pre operation" instead of  "Pre Validation".

    that means "Delete_20" instead of "Delete_10". 

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 164 Super User 2026 Season 1

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 51 Most Valuable Professional

#3
NeerajPawar Profile Picture

NeerajPawar 42

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans