5
votes

I'm using the following gem to connect to Microsoft Dynamics CRM: https://github.com/TinderBox/dynamics_crm. I was able to connect and add contacts, leads and a few other things just fine. My problem is, I can't figure out how to add an order and order details. Here is the code I am using to create an order detail:

details = Hash.new
    details = {
        'quantity' => 1000.0,
        'productid' => product,
        'salesorderid' => DynamicsCRM::XML::EntityReference.new("salesorder", order.id),
        'uomid' => DynamicsCRM::XML::EntityReference.new("uom", 'F5AE673D-5D8E-E211-8AD0-78E3B5101E8F'),
        'createdon' => Time.now.getutc,
        'salesorderstatecode' => 1,
        'description' => 'This is just a test order',
    }
    orderDetail = client.create('salesorderdetail', details)

This runs fine, but when I check in the CRM backend, there is no record under Order Details. I also can't figure out how to send custom fields, I have tried 'new_shirtsize' => 'XL', but I just get an error that the field 'new_shirtsize' doesn't exist for the entity 'salesorderdetail'.

2
in JavaScript we do it like details.salesorderid = { Id: order.id, LogicalName: 'salesorder' };Yaqub Ahmad

2 Answers

2
votes

I can only guess, but I had a look in the specs of the gem you mentioned. Looks like the two parameters need to be written like so:

details = {}
details['salesorderid'] = {}
details['salesorderid']['Id'] = order.id
details['salesorderid']['LogicalName'] = 'salesorder'
client.create('orderdetail', details)

Btw, you can make this a little more compact:

client.create('orderdetail', salesorderid: 
  {'Id' => order.id, 'LogicalName' => 'salesorder'} )
1
votes

Two things to try:

  1. Remove this line:

details['salesorderid']['Id'] = order.id

Reason: When creating a new record via CRM API you don't have to provide the Id. CRM will generate this for you. This is the recommended approach when creating a new CRM record, rather than specifying the Id.

  1. Check that order.id is NOT NULL. I suspect the object is NULL.

Reason: When creating a new record via CRM API you can (if you really want to) provide the Id, but you have to check it has a valid GUID in the form of (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx), where x = is of HEX value.