CRM 2011 – Verkaufschance abschließen – „gewonnen“/“verloren“

Nachfolgend findet Ihr zwei nützliche Funktionen, mit welchen Ihr programmatisch eine Verkaufschance abschließen (als ‚gewonnen‘ schließen bzw. als ‚verloren‘ schließen) könnt:

[sourcecode language=“csharp“]

/// <summary>
/// Marks as lost.
/// </summary>
/// <param name="organization">The organization.</param>
/// <param name="entityId">The entity id.</param>
/// <returns></returns>
public Result MarkAsLost(IOrganization organization, string entityId)
{
try
{
LoseOpportunityRequest req = new LoseOpportunityRequest();
Entity opportunityClose = new Entity("opportunityclose");
opportunityClose.Attributes.Add("opportunityid", new EntityReference("opportunity", new Guid(entityId)));
opportunityClose.Attributes.Add("subject", "opportunity lost");
req.OpportunityClose = opportunityClose;

OptionSetValue o = new OptionSetValue();
o.Value = 4;
req.Status = o;
LoseOpportunityResponse resp = (LoseOpportunityResponse) Helper.OrgService(organization).Execute(req);

return Helper.CreateResult("ok", Result.StatusCodes.Ok, null);
}
catch (Exception ex)
{
return Helper.CreateResult(ex.Message, Result.StatusCodes.Error, null);
}
}

/// <summary>
/// Marks as won.
/// </summary>
/// <param name="organization">The organization.</param>
/// <param name="entityId">The entity id.</param>
/// <returns></returns>
public Result MarkAsWon(IOrganization organization, string entityId)
{
try
{
WinOpportunityRequest req = new WinOpportunityRequest();
Entity opportunityClose = new Entity("opportunityclose");
opportunityClose.Attributes.Add("opportunityid", new EntityReference("opportunity", new Guid(entityId)));
opportunityClose.Attributes.Add("subject", "opportunity won");
req.OpportunityClose = opportunityClose;

//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.
req.Status = new OptionSetValue(-1);

WinOpportunityResponse res = (WinOpportunityResponse)Helper.OrgService(organization).Execute(req);
return Helper.CreateResult("ok", Result.StatusCodes.Ok, null);
}
catch (Exception ex)
{
return Helper.CreateResult(ex.Message, Result.StatusCodes.Error, null);
}
}

[/sourcecode]

Happy coding!

Leave a reply