Friday, October 2, 2009

Update / Set CRM Opportunity State/ Status

public bool UpdateOpportunityState(Guid activityid, string astatuscode, string astatecode)
{

CrmService Crm4_service = connect();
try
{
if (astatecode == "Open")
{
SetStateOpportunityRequest state = new SetStateOpportunityRequest();

if (astatuscode == "1")
{
return true;
}
else if (astatuscode == "2")
{
state.OpportunityState = OpportunityState.Open;
state.OpportunityStatus = 2;/// Convert.ToInt32();
}

state.EntityId = activityid;
SetStateOpportunityResponse stateSet = (SetStateOpportunityResponse)Crm4_service.Execute(state);

return true;
}



opportunity src_opp = (opportunity)Crm4_service.Retrieve(EntityName.opportunity.ToString(), activityid, new AllColumns()); // = OpportunityState.Lost;


opportunityclose opp_close = new opportunityclose();
opp_close.actualrevenue = src_opp.actualvalue;
opp_close.opportunityid = new Lookup();
opp_close.opportunityid.Value = src_opp.opportunityid.Value;
opp_close.actualend = src_opp.actualclosedate;
opp_close.statuscode = src_opp.statuscode;
if (astatuscode == "3")
{
// this Opportunity was won
WinOpportunityRequest opp_won = new WinOpportunityRequest();
opp_won.OpportunityClose = opp_close;
opp_won.Status = 3;// src_opp.statuscode.Value;
Crm4_service.Execute(opp_won);
return true;
}
else
{
// this Opportunity was lost
LoseOpportunityRequest opp_lost = new LoseOpportunityRequest();
opp_lost.OpportunityClose = opp_close;
if (astatuscode == "4")
{
opp_lost.Status = 4;// src_opp.statuscode.Value;
}
else
{
opp_lost.Status = 5;// src_opp.statuscode.Value;

}
Crm4_service.Execute(opp_lost);
return true;
}

}
catch (System.Web.Services.Protocols.SoapException ex)
{
return false;
}

return true;


}

No comments: