Reputation: 7169
Along the lines of this question.
I'm trying to mark an existing Invoice as Paid in Dynamics CRM 2011 using Silverlight.
According to the documentation, all I need to do is set the Status Code = 100001 and the State Code = 2.
When I do this I get a "NotFound" exception.
Guid invoiceID = new Guid("Existing Invoice Guid");
IOrganizationService orgService = OrgServiceFactory.GetInstance();
orgService.BeginRetrieve("invoice", invoiceID, new ColumnSet(new string[] { "invoiceid", "statecode", "statuscode" }), (result) =>
{
var fetchResp = orgService.EndRetrieve(result);
var statecodeAttrib = fetchResp.Attributes.Single(a => a.Key == "statecode");
OptionSetValue statecode = (OptionSetValue)statecodeAttrib.Value;
statecode.Value = 2;
var statuscodeAttrib = fetchResp.Attributes.Single(a => a.Key == "statuscode");
OptionSetValue statuscode = (OptionSetValue)statuscodeAttrib.Value;
statuscode.Value = 100001;
orgService.BeginUpdate(fetchResp, (updateResult) =>
{
/* Web Exception thrown here */
orgService.EndUpdate(updateResult);
Console.Write("");
}, orgService);
}, orgService);
If I remove the "statecode" bit, and just try and set the statuscode to 2 - (Partially Shipped) or 4 - (Billed) it works as expected.
It is only when I try and set both it fails. It also fails if I just try and set statuscode = 100001, 100002, 100003 (Complete, Partial, Canceled)
Is there another way to mark an invoice as paid?
Upvotes: 3
Views: 3419
Reputation: 3210
Executing a standard SetState request will also work, negating the need to establish a SOAP connection.
SetStateRequest request = new SetStateRequest();
request.EntityMoniker = new EntityReference(Invoice.EntityLogicalName, invoice.Id);
request.State = new OptionSetValue ((int)InvoiceState.Paid);
request.Status = new OptionSetValue (100001); // Complete
SetStateResponse response = (SetStateResponse)_service.Execute(request);
Upvotes: 4
Reputation: 4686
To change a record's State, you always need to execute a separate SetState request as opposed to just a simple update to the state and statuscode. In your case, you can do a SetStateDynamicEntity or SetStateInvoice request.
Unfortunately, those messages are not available from the OData service in CRM 2011. What you'll need to do is use the SOAP web service through Silverlight. The SDK has a walkthrough, and if you want a head start, the SilverCRMSoap library is a good quick implementation of that walkthrough.
Upvotes: 5