Wednesday, August 02, 2006

MSCRM 3.0 SDK: Close Opportunity

To close a Won Opportunity:

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

WhoAmIRequest req = new WhoAmIRequest();
WhoAmIResponse res = (WhoAmIResponse) service.Execute(req);

Guid OpportunityID = new Guid("{e0fbb411-cb35-db11-b3d2-0003ffc41bcc}");

// Close Opportunity - Won
WinOpportunityRequest wReq = new WinOpportunityRequest();
opportunityclose won = new opportunityclose();

// Oppoturnity ID
Guid id = new Guid(OpportunityID);
Lookup oLookup = new Lookup();
oLookup.Value = id;
won.opportunityid = oLookup;

// Status Reason
Status oStatus = new Status();
oStatus.Value = 3; // Won
won.statuscode = oStatus;

// Execute Request
wReq.OpportunityClose = won;
wReq.Status = 3; // Won
WinOpportunityResponse wRes = (WinOpportunityResponse) service.Execute(wReq);


Lost Opportunity:

// Opportunity - Lost
LoseOpportunityRequest lReq = new LoseOpportunityRequest();
opportunityclose lose = new opportunityclose();

// Oppoturnity ID
Guid id = new Guid(strOpportunityID);
Lookup oLookup = new Lookup();
oLookup.Value = id;
lose.opportunityid = oLookup;

// Status Reason
Status oStatus = new Status();
oStatus.Value = 5; // Out-Sold
lose.statuscode = oStatus;

// Execute Request
lReq.OpportunityClose = lose;
lReq.Status = 5; // Out-Sold
LoseOpportunityResponse lRes = (LoseOpportunityResponse) service.Execute(lReq);