[quote user="tekwise"]
Can someone please help me with this? I need help writing the syntax from scratch.
I've spent a lot of time trying to figure this out and I'm struggling...I keep getting this error. I can only use the productid, unless I'm wrong...I would greatly appreciate the help.

[/quote]
Here's the code - the SALESORDER works fine - just attaching the product to the SALESORDERDETAIL ENTITY
========================================================
using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
{
foreach (var r in orders)
{
_service = (IOrganizationService)_serviceProxy;
// Instaniate an salesorder object.
Entity salesorder = new Entity("salesorder");
// Set the required attributes. For salesorder, only the name is required.
salesorder["name"] = r.Name.ToString();
//update orders with the values
salesorder["promocode"] = "Promo";
//CREATE NEW SALES ORDER
//Entity Reference: set the attributes to be retrieved
var account = GetEntityCollection(_service, "account", "name", r.customerid, new ColumnSet("accountid", "name"));
Guid accountId = (Guid)account[0].Id;
salesorder["customerid"] = new EntityReference("account", new Guid(accountId.ToString()));
salesorder.Id = _orderId; // the order id is mapped to the specific order, can be used to map the product detail entity (salesorderdetail)
//SALES ORDER DETAIL: NOT WORKING
Entity orderDetail = new Entity("salesorderdetail");
var orderProduct = GetEntityCollection(_service, "salesorderdetail", "productid", r.ProductNumber, new ColumnSet("salesorderdetailid", "productid"));
Guid productOid = (Guid)orderProduct[0].Id;
orderDetail["salesorderdetailid"] = new EntityReference("salesorderdetail", new Guid(productOid.ToString()));
orderDetail["salesorderdetailid"] = salesorder.Id;
// Create an salesorder record
_orderId = _service.Create(salesorder);
_service.Create(orderDetail);
}
private static EntityCollection GetEntityCollection(IOrganizationService service, string entityName, string attributeName, string attributeValue, ColumnSet cols)
{
QueryExpression query = new QueryExpression
{
EntityName = entityName,
ColumnSet = cols,
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = attributeName,
Operator = ConditionOperator.Equal,
Values = { attributeValue }
}
}
}
};
return service.RetrieveMultiple(query);
}