Skip to main content

Notifications

Announcements

No record found.

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."

Posted on by 154

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

  • Tushar Jadhav Profile Picture
    Tushar Jadhav 154 on at
    RE: 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."

    For the Delete Operation:-

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

    that means "Delete_20" instead of "Delete_10". 

  • the12stringer Profile Picture
    the12stringer 5 on at
    RE: 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."

    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.

  • Suggested answer
    XM-22040801-0 Profile Picture
    XM-22040801-0 11 on at
    RE: 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."

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

    You need to use it in the fetch:

    ""

  • Tushar Jadhav Profile Picture
    Tushar Jadhav 154 on at
    RE: 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."

    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
    XM-22040801-0 11 on at
    RE: 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."

    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
    Tushar Jadhav 154 on at
    RE: 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."

    Thankyou for replay delete operation run on stage 40??

    post operation or else pre validation.??

  • Suggested answer
    XM-22040801-0 Profile Picture
    XM-22040801-0 11 on at
    RE: 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."

    // 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
    Tushar Jadhav 154 on at
    RE: 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."

    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
    XM-22040801-0 11 on at
    RE: 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."

    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

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans