Update AxdSalesorder using srv SalesorderServiceClient()

Hello,

I have a salesorder on my AX 2012, i am using the SalesSalesorderService. I am unable to update the AxdSalesorder. below is my code (if there is anything wrong can you please provide me with the correct code):

public Boolean UpdateAxSalesOrder(string SalesId)

{

SalesOrderServiceClient proxy = new SalesOrderServiceClient();

CallContext context = new CallContext();

context.Company = ConfigurationManager.AppSettings[“AxOrganization”].ToString();

//Create KeyList

CustomerService.EntityKey[] readRespKeys = new CustomerService.EntityKey[1];

readRespKeys[0] = new CustomerService.EntityKey();

readRespKeys[0].KeyData = new CustomerService.KeyField[1];

readRespKeys[0].KeyData[0] = new CustomerService.KeyField();

readRespKeys[0].KeyData[0].Field = “SalesId”;

readRespKeys[0].KeyData[0].Value = SalesId;

//Fetch SalesOrder

AxdSalesOrder salesOrder = null;

salesOrder = proxy.find(context, createQueryCriteria(SalesId));

try

{

if (salesOrder.SalesTable.Length > 0)

{

foreach (AxdEntity_SalesTable salesTable in salesOrder.SalesTable)

{

foreach (AxdEntity_SalesLine salesLine in salesTable.SalesLine)

{

salesLine.SalesQty = 10;

}

}

}

proxy.update(context, readRespKeys, salesOrder);

return true;

}

catch (System.ServiceModel.FaultException ex)

{

throw ex.Message;

}

}

I am facing the following error:

Invalid document schema. The following error was returned: The element ‘SalesTable’ in namespace ‘http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder’ has invalid child element ‘QuotationId’ in namespace ‘http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder’. List of possible elements expected: ‘PurchOrderFormNum’ in namespace ‘http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder’.

The error means that you some required elements (at least PurchOrderFormNum) don’t exist in the document.

Your code tries to update sales order even if salesOrder.SalesTable.Length is zero - isn’t that the problem?

Hello Martin,

Actually salesOrder.SalesTable.Length is bigger than Zero (it is equal to one… it already exist in AX)

The error changed to:
{System.ServiceModel.FaultException`1[WebApplication1.CustomerService.AifFault]: Invalid document schema. The following error was returned: The element ‘SalesTable’ in namespace ‘http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder’ has invalid child element ‘QuotationId’ in namespace ‘http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder’. List of possible elements expected: ‘PurchOrderFormNum’ in namespace ‘http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder’. (Fault Detail is equal to WebApplication1.CustomerService.AifFault).}

My code is the below:

//Fetch SalesOrder
AxdSalesOrder salesorder = proxy.find(context, createQueryCriteria(SalesId));

try
{
if (salesorder.SalesTable.Length > 0)
{
foreach (AxdEntity_SalesTable salesTable in salesorder.SalesTable)
{
salesTable.QuotationId = " ";
foreach (AxdEntity_SalesLine salesLine in salesTable.SalesLine)
{
salesLine.PurchorderFormNum = string.Empty;

salesLine.SalesQty = 10;
salesLine.action = AxdEnum_AxdEntityAction.update;
salesLine.actionSpecified = true;
}

}
}
salesorder.SalesTable[0].action = AxdEnum_AxdEntityAction.update;
salesorder.SalesTable[0].actionSpecified = true;
salesorder.DocPurpose = AxdEnum_XMLDocPurpose.Original;
salesorder.DocPurposeSpecified = true;
proxy.update(context, readRespKeys, salesorder);

Do you have any idea?

As I already said, the error means that PurchOrderFormNum is set as mandatory but you didn’t provide any value (therefore the element isn’t in the document). The error basically says: “I looked for PurchOrderFormNum but it’s not there, I found QuoationId where I expected PurchOrderFormNum”. Either provide a value for PurchOrderFormNum or change it not to be mandatory for the service.

Yes I understand what you are saying,

but i am unable to find the X++ code responsible for this mandatory characteristic for the PurchOrderFormNum.

Any advice?

Best Regards.

You’ll find the definition in AxdSalesOrder class, initMandatoryFieldsMap() method.

under my mandatoryFieldsMap() method in AxdSalesOorder Class:

protected void initMandatoryFieldsMap()
{
super();
this.setParmMethodAsMandatory(classNum(AxSalesLine),methodStr(AxSalesLine,parmSalesQty)) ;
this.setParmMethodAsMandatory(classNum(AxSalesLine),methodStr(AxSalesLine,parmSalesUnit)) ;

//this.setParmMethodAsMandatory(classNum(AxSalesTable), methodStr(AxSalesTable,parmPurchOrderFormNum)) ;

if (isConfigurationkeyEnabled(configurationKeyNum(SalesDeliveryDateControl)))
{
this.setParmMethodAsMandatory(classNum(AxSalesTable), methodStr(AxSalesTable,parmReceiptDateRequested)) ;
}
else
{
this.setParmMethodAsMandatory(classNum(AxSalesTable), methodStr(AxSalesTable,parmDeliveryDate)) ;
}

}

After modifying the method code (commenting the 4th line) i did the following:

  1. Saving the Method
  2. Compiling the class
  3. Updating document service (for my SalesSalesorderService with update AxBC classes)
  4. Refresh the SalesSalesorderService in the AifService form
  5. Compile into .Net CIL

So you can say i did everything. And I am still unable to update the AxdSalesorder (with the same error)

Is your code still there after you rebuilt the service? What the schema says? Is the field still mandatory there? Try to do some analysis by yourself (and not just saying “it doesn’t work”) - you’re the only one who can do that.