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
i have searching these for a long time.. thanks for them 🙂
LikeLike
thanx for this “cookbook” 🙂 it really helped me 🙂
LikeLike
how to get the value of actualclosedate and new_estimatedrevenue for closing opportunity on lost and dropped
LikeLike
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
LikeLike
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)
LikeLike
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);
LikeLike
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);
}
LikeLike
To close order:
Change : closeOrderFields.Add(“salesorderid”, ms_itemID);
To: closeOrderFields.Add(“salesorderid”,new EntityReference(“salesorder”, new Guid( ms_itemID)));
LikeLike