How to – Close an opportunity programmatically CRM


We can make use of following for closing the opportunity programmatically in CRM.

WinOpportunityRequest for closing an opportunity as won

//We create an instance of WinOpportunityRequest class as salesstage is Won.
WinOpportunityRequest woReq=new WinOpportunityRequest();
//WinOpportunityRequest takes 2 values: Opportunityclose and Status.
opportunityclose oc=new opportunityclose();
//Since Opportunityid is of type Lookup, we create an instance of the Lookup class. Lookup class has 2 attributes: Lookup.Value and Lookup.Type
Lookup lookup=new Lookup();
//We pass the GUID value of the opportunity to an instance of the Lookup class.
lookup.Value=entityContext.InstanceId;                                                                                          //We specify the type of the entity being passed.
lookup.type=EntityName.opportunity.ToString();
oc.opportunityid=lookup;
oc.actualend=actualclosedate(CrmDateTime variable);
oc.actualrevenue=new_estimatedrevenue(CrmMoney variable);
woReq.OpportunityClose=oc;

//The Status parameter corresponds to Status Reason in the Microsoft CRM application. If you pass -1 for this parameter, the platform sets the status to the appropriate value for the Microsoft CRM application.
woReq.Status=-1;
//The WinOpportunityRequest is sent to the platform using the Execute method. The platform will run the request and send back an instance of the Response class message.
WinOpportunityResponse woRes=(WinOpportunityResponse)service.Execute(woReq);


LoseOpportunityRequest for closing opportunity on lost and dropped
LoseOpportunityRequest loReq=new LoseOpportunityRequest();
//LoseOpportunityRequest takes 2 values: Opportunityclose and Status.
opportunityclose oc=new opportunityclose();
//Since Opportunityid is of type Lookup, we create an instance of the Lookup class. Lookup class has 2 attributes: Lookup.Value and Lookup.Type
Lookup lookup=new Lookup();
//We pass the GUID value of the opportunity to an instance of the Lookup class.
lookup.Value=entityContext.InstanceId;
//We specify the type of the entity being passed.
lookup.type=EntityName.opportunity.ToString();
oc.opportunityid=lookup;
oc.actualend=actualclosedate;
oc.actualrevenue=new_estimatedrevenue;
loReq.OpportunityClose=oc;
//The Status parameter corresponds to Status Reason in the Microsoft CRM application. If you pass -1 for this parameter, the platform sets the status to the appropriate value for the Microsoft CRM application.
loReq.Status=-1;
//The LoseOpportunityRequest is sent to the platform using the Execute method. The platform will run the request and send back an instance of the Response class message.
LoseOpportunityResponse loRes=(LoseOpportunityResponse)service.Execute(loReq);

Bye

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

8 thoughts on “How to – Close an opportunity programmatically CRM”

  1. Hi Nishant,

    I need one help.

    I want to call customer address lookup from my custom code, which can show contact and/or company addresses. We have our own lookup control which in turn will use CRM’s lookup. we are passing class parameter as null, so it is showing all the address which is the problem because when we add any address in the contact it will create 2 addresses address 1 and address2 where address2 will be blank. So in the lookup it is also showing the blank records.
    I want to filter out the lookup so that it will show the proper data.

    Is there any way to do that?

    Thanks,
    Krutika

    Like

  2. Here is a sample code that worked for me in CRM 2011 to close an Opportunity as Won.

    OpportunityClose oppclose = new OpportunityClose();
    oppclose.OpportunityId = new EntityReference();
    oppclose.OpportunityId.Id = opportunity.Id; // Guid of the opportunity
    oppclose.OpportunityId.LogicalName = Opportunity.EntityLogicalName;
    oppclose.Description = description; // optional
    oppclose.StatusCode = new OptionSetValue(StatusCode); // StatusCode for Status Won
    oppclose.ActualEnd = DateTime.Now;

    WinOpportunityRequest winReq = new WinOpportunityRequest();
    winReq.OpportunityClose = oppclose;
    winReq.Status = new OptionSetValue(StatusCode);

    CrmService.Execute(winReq)

    Like

  3. for Closing Opportunity as Lost

    OpportunityClose oppclose = new OpportunityClose();
    oppclose.OpportunityId = new EntityReference();
    oppclose.OpportunityId.Id = opportunity.Id; //Opportunity Guid
    oppclose.OpportunityId.LogicalName = Opportunity.EntityLogicalName;
    oppclose.Description = description; //optional
    oppclose.StatusCode = new OptionSetValue(StatusCode); //Status code for Lost
    oppclose.ActualEnd = DateTime.Now;

    LoseOpportunityRequest loseReq = new LoseOpportunityRequest();
    loseReq.OpportunityClose = oppclose;
    loseReq.Status = new OptionSetValue(StatusCode); //Status code for Lost

    CrmService.Execute(loseReq);

    Like

  4. Somebody know how to close an order? I am getting FaultException: ‘The entity id is missing.’ For oCloseEntity.id I have tried new Guid,guid of salesorder but both dont work..

    try
    {
    if (string.IsNullOrEmpty(ms_ownerID) || ms_ownerID == null)
    {
    ms_ownerID = ms_userid;
    ms_ownerType = “systemuser”;
    }
    AttributeCollection closeOrderFields = new AttributeCollection();
    closeOrderFields.Add(“salesorderid”, ms_itemID);
    closeOrderFields.Add(“ownerid”,new EntityReference(“systemuser”, new Guid(ms_ownerID)));
    closeOrderFields.Add(“statuscode”, -1);
    Entity oCloseEntity = new Entity();
    oCloseEntity.LogicalName = “orderclose”;
    oCloseEntity.Id = Guid.NewGuid();// ms_itemID;// new Guid();
    oCloseEntity.Attributes = closeOrderFields;

    if (ms_statecode.ToUpper() == “FULFILLED” || ms_statecode == “3”)
    {
    FulfillSalesOrderRequest oFulfill = new FulfillSalesOrderRequest();
    oFulfill.OrderClose = oCloseEntity;
    oFulfill.Status = new OptionSetValue((int)mi_statuscode);
    _orgService.Execute(oFulfill);
    }

    Like

    1. To close order:
      Change : closeOrderFields.Add(“salesorderid”, ms_itemID);
      To: closeOrderFields.Add(“salesorderid”,new EntityReference(“salesorder”, new Guid( ms_itemID)));

      Like

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Welcome to the Blog & Website of Paul Andrew

Technical Leadership Centred Around the Microsoft Data Platform

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

dreaming to be a clean coder and TDD minded programmer.

%d bloggers like this: