Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to close opportunity using Organisation services.

Posted on by 4,601

Hi,

I have requirement to close opportunity using asp.net application. Please share if any example.

Thanks,

Sandeep

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to close opportunity using Organisation services.

    LOSE. LoseOpportunityRequest

  • Suggested answer
    Alagunellaikumar Profile Picture
    Alagunellaikumar 6,210 on at
    RE: How to close opportunity using Organisation services.

    Hi

    Did you use correct CRM version SDK? Please refer below URL

    msdn.microsoft.com/.../microsoft.crm.sdk.messages.winopportunityrequest.aspx

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to close opportunity using Organisation services.

    Never mind, I found it in Microsoft.Crm.Sdk.Messages;

    What namespace is needed for WinOpportunityRequest and LostOpportunityRequest?  These don't resolve in my code.  I've got these using statements:

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk.Metadata;

    using Microsoft.Xrm.Sdk.Messages;

    using Microsoft.Xrm.Sdk.WebServiceClient;

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: How to close opportunity using Organisation services.

    Hi Sandeep,

    You can use following code to Close Opportunity as Won or Lost.

    public void Execute(IServiceProvider serviceProvider)

           {

               //Extract the tracing service for use in debugging sandboxed plug-ins.

               ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               // Obtain the execution context from the service provider.

               IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

               // The InputParameters collection contains all the data passed in the message request.

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

               {

                   if (context.Depth > 1) return;

                   // Obtain the target entity from the input parameters.

                   Entity entity = (Entity)context.InputParameters["Target"];

                   // For Close Opportunity as Won

                   var winOppRequest = new WinOpportunityRequest

                   {

                       OpportunityClose = new OpportunityClose

                       {

                           OpportunityId = new EntityReference

                               (Opportunity.EntityLogicalName, entity.Id)

                       },

                       Status = new OptionSetValue(3)

                   };

                   service.Execute(winOppRequest);

                   // For Close Opportunity as Lost

                   var LostOppRequest = new LoseOpportunityRequest

                   {

                       OpportunityClose = new OpportunityClose

                       {

                           OpportunityId = new EntityReference(Opportunity.EntityLogicalName, entity.Id)

                       },

                       Status = new OptionSetValue(4)

                   };

                   service.Execute(LostOppRequest);

               }

           }

    Hope it will helps you,

    Thanks.

  • Inogic Profile Picture
    Inogic 24,094 on at
    RE: How to close opportunity using Organisation services.

    Hi,

    You can refer below link to connect with CRM which will help you to retrieve proxy.

    https://msdn.microsoft.com/en-us/library/gg309393.aspx 

    After that you can get the service object using proxy. Then you can refer below link to close opportunity.

    https://nishantrana.me/2008/07/11/closing-an-opportunity-programmatically-crm/  

    Hope this helps for you.

    Thanks!

    Sam

  • Suggested answer
    Tushar2016CRM Profile Picture
    Tushar2016CRM 1,130 on at
    RE: How to close opportunity using Organisation services.

    HI ,

    Please follow the below links if they might help :

    nishantrana.me/.../closing-an-opportunity-programmatically-crm

    social.microsoft.com/.../crm-2011-plugin-for-opportunity-close

    Thanks !

  • Rajeev Kumar Profile Picture
    Rajeev Kumar 10 on at
    RE: How to close opportunity using Organisation services.

    Here the Code to Close the Oppty. Hope it will help you.

    private void WinOpportunity(IOrganizationService organizationService, Opportunity opportunity)
    {

    OpportunityClose oppClose = new OpportunityClose
    {
    OpportunityId = opportunity.ToEntityReference(),
    Subject = "Won!",
    ActualEnd = DateTime.Now,
    Description = "Won!",
    };

    var req = new WinOpportunityRequest
    {
    OpportunityClose = oppClose,
    RequestName = "WinOpportunity",
    Status = new OptionSetValue(3)
    };

    var response = (WinOpportunityResponse)organizationService.Execute(req);

    var results = response.Results;
    }

    Thanks,

    Rajeev

  • Suggested answer
    Shantnu Sharma Profile Picture
    Shantnu Sharma 655 on at
    RE: How to close opportunity using Organisation services.

    1. Initialize the organizationserviceproxy in your application.

    2. Use WinOpportunityRequest or LostOpportunityRequest and execute this organization request in your application.

    Please let me know if you need more 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

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans