Answered by:
Exception when trying to create Invoice record from Opportunity entity using SDK.

Question
-
Hi, When I am trying to create Invoice using CRM SDK with the following code I am getting the exception. The following is the code and exception.DynamicEntity New_Invoice = new DynamicEntity();New_Invoice.Name = EntityName.invoice.ToString();//New_Invoice.Name = "SDK Name";
PicklistProperty pp = new PicklistProperty();pp.Name = "customerid";pp.Value = new Picklist();pp.Value.Value = 0;New_Invoice.Properties.Add(pp);
//Customer customer = new Customer();//customer = (Customer)opportunity.Properties["customerid"];//CustomerProperty cp = new CustomerProperty();//cp.Name = "customerid";//cp.Value = customer;//New_Invoice.Properties.Add(cp);
PicklistProperty pp1 = new PicklistProperty();pp1.Name = "pricelevelid";pp1.Value = new Picklist();pp1.Value.Value = 0;New_Invoice.Properties.Add(pp1);
PicklistProperty pp2 = new PicklistProperty();pp2.Name = "transactioncurrencyid";pp2.Value = new Picklist();pp2.Value.Value = 0;New_Invoice.Properties.Add(pp2);
TargetCreateDynamic tcd = new TargetCreateDynamic();tcd.Entity = New_Invoice;
CreateRequest cr = new CreateRequest();cr.Target = tcd;
ICrmService _service = (ICrmService)context.CreateCrmService(true);
CreateResponse cresponse = (CreateResponse)_service.Execute(cr);
The above highlighter text is throwing the exception "Server was unable to process request".When I look into the details I see"de>0x80040216</code>\n <description>An unexpected error occurred.</description>\n <type>Platform</type>\n</error>"" the exception.
Can any one help me in this?
Thanks,Vikram.- Edited by Vikram96 Thursday, February 19, 2015 8:11 PM
Thursday, February 17, 2011 3:05 AM
Answers
-
The error is due to the 'opportunity' object's Properties collection not containing a "customerid" property.
If this is a Custom Workflow Activity, this will be due to the triggering event not providing the "customerid" attribute in the primary entity object.
If this is a Plugin, this will be due to the message being processed not providing the "customerid" attribute in the Target entity object.
If for instance, you are processing an 'Update' and the "customerid" was not actually updated, the "customerid" attribute is not available.
In any event you can obtain any/all attributes required via a Retrieve:
The above retrieves All attributes (or the "customerid" attribute using the commented out line) for the primary entity instance in a Custom Workflow Activity.private DynamicEntity GetTargetEntity(IWorkflowContext context, ICrmService crmService) { TargetRetrieveDynamic target = new TargetRetrieveDynamic(); target.EntityId = context.PrimaryEntityId; target.EntityName = context.PrimaryEntityName; RetrieveRequest req = new RetrieveRequest(); req.Target = target; req.ReturnDynamicEntities = true; req.ColumnSet = new AllColumns(); //req.ColumnSet = new ColumnSet(new string[] { "customerid" }); RetrieveResponse resp = (RetrieveResponse)crmService.Execute(req); return resp.BusinessEntity as DynamicEntity; }
--pogo (pat)- Marked as answer by Vikram96 Friday, February 18, 2011 3:16 AM
Thursday, February 17, 2011 11:52 PM
All replies
-
You're attempting to assign Picklist Properties to what should be one Customer and two Lookup Properties. Also, you'll need to supply at least a name Property. Should look something more like:
DynamicEntity New_Invoice = new DynamicEntity(EntityName.invoice.ToString()); New_Invoice.Properties.Add(new StringProperty("name", "Invoice Name - This is a short, human readable identifier for the Invoice")); New_Invoice.Properties.Add(new CustomerProperty("customerid", (Customer)opportunity.Properties["customerid"])); //New_Invoice.Properties.Add(new CustomerProperty("customerid", new Customer(EntityName.account.ToString(), new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")))); New_Invoice.Properties.Add(new LookupProperty("pricelevelid", new Lookup("pricelevel", new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")))); New_Invoice.Properties.Add(new LookupProperty("transactioncurrencyid", new Lookup("transactioncurrency", new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"))));
The former Customer property assignment can be used if you already have access to your Opportunity entity instance (the code looks like a Custom Workflow Activity). The latter can be used if you know the Customer entity type and GUID.
You will have to find the GUID identifier values of the relevant Price List (pricelevel) and Currency (transactioncurrency) entity instances.
In order to get more indepth information about the Exception raised, either:
- Debug your code, capture the Exception and examine the Detail property OR
- Enable tracing on the CRM Server and interrogate the resultant trace file logs
--pogo (pat)- Proposed as answer by Andrii ButenkoMVP, Moderator Thursday, February 17, 2011 6:57 AM
Thursday, February 17, 2011 3:57 AM -
Hi Pogo,
Thanks to the response. I have made code changes as suggested by you. But I still get the error at the line
New_Invoice.Properties.Add(
new CustomerProperty("customerid", (Customer)opportunity.Properties["customerid"]));
The error message is "The given key was not present in the dictonary".
Can you please suggest over this.
Thursday, February 17, 2011 6:00 PM -
The error is due to the 'opportunity' object's Properties collection not containing a "customerid" property.
If this is a Custom Workflow Activity, this will be due to the triggering event not providing the "customerid" attribute in the primary entity object.
If this is a Plugin, this will be due to the message being processed not providing the "customerid" attribute in the Target entity object.
If for instance, you are processing an 'Update' and the "customerid" was not actually updated, the "customerid" attribute is not available.
In any event you can obtain any/all attributes required via a Retrieve:
The above retrieves All attributes (or the "customerid" attribute using the commented out line) for the primary entity instance in a Custom Workflow Activity.private DynamicEntity GetTargetEntity(IWorkflowContext context, ICrmService crmService) { TargetRetrieveDynamic target = new TargetRetrieveDynamic(); target.EntityId = context.PrimaryEntityId; target.EntityName = context.PrimaryEntityName; RetrieveRequest req = new RetrieveRequest(); req.Target = target; req.ReturnDynamicEntities = true; req.ColumnSet = new AllColumns(); //req.ColumnSet = new ColumnSet(new string[] { "customerid" }); RetrieveResponse resp = (RetrieveResponse)crmService.Execute(req); return resp.BusinessEntity as DynamicEntity; }
--pogo (pat)- Marked as answer by Vikram96 Friday, February 18, 2011 3:16 AM
Thursday, February 17, 2011 11:52 PM -
Hi Pogo,
Your solutions has resolved my problem. Thanks alot.
Vikram
- Edited by Vikram96 Thursday, February 19, 2015 8:11 PM
Friday, February 18, 2011 3:16 AM