web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Error when calling custom D365 F&O service to create Purchase Order Lines

(0) ShareShare
ReportReport
Posted on by 35

Good day guys,

I have a client that wants to integrate F&O with another C# system, the approach I have used is :

  • to make a call to the service operation to create a Purchase Order header and return it to the external system (this part is working)
  • the second call is send purchase order lines with the Purchase Order ID generated from the first call (this part is not working)

I'm facing an error which says 

Response Description : {
"Message": "An exception occured when invoking the operation - ",
"ExceptionType": "ErrorException",
"ActivityId": "485a3d26-fb28-0003-6e44-5a4828fbd801"
}

Below is the c# code for the second call which is giving me the error above.

string purch_ID = label1.Text; //PurchOrderID generated from first call

string createPO_Lines = ClientConfiguration.Default.UriString + "api/services/CreatePurchOrderServiceGroup/CreatePurchaseOrderService/createPurchOrderLines";

var request = HttpWebRequest.Create(createPO_Lines);

request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader(true);

request.Method = "POST";

request.ContentType = "application/json";

// Pass parameters in JsonSerialize Object start

var requestContract = new
{
purchID = purch_ID,
itemID = "50100",
qty = 1
// siteID = "MainOffice",
// locationID = "MainOffice",
// warehouseID = "MainOffice"
};

var requestContractString = JsonConvert.SerializeObject(requestContract);

using (var stream = request.GetRequestStream())
{
using (var writer = new StreamWriter(stream))
{
writer.Write(requestContractString);
}
} // Pass parameters in JsonSerialize Object end

try
{
var response = request.GetResponse() as HttpWebResponse; //here is where im getting the exception
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
string responseString = streamReader.ReadToEnd();
//Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
//Assert.IsFalse(string.IsNullOrEmpty(responseString));
label2.Text = responseString;
Console.WriteLine(responseString);
}
}
}
catch (WebException ex)
{
Console.WriteLine("Exception Message :" + ex.Message);
if (ex.Status == WebExceptionStatus.ProtocolError)
{
var response = ((HttpWebResponse)ex.Response);
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)ex.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)ex.Response).StatusDescription);
Console.WriteLine(" meth : {0}", ((HttpWebResponse)ex.Response).Method);

var stream = response.GetResponseStream();
var reader = new StreamReader(stream);
var text = reader.ReadToEnd();
Console.WriteLine("Response Description : {0}", text);
label2.Text = text;

}

}


}

The code for the xpp method that will create the Purchase Order lines is as follows.

public str createPurchOrderLines(
//str strDataAreaId,
str purchID,
str itemID,
real qty
// str siteID,
// str locationID,
// str warehouseID
)
{
PurchLine purchline;

PurchTable purchTable_;

AxPurchLine axPurchLine;

str strDataAreaId = "USSI";

str result = "---";

itemID = "50100";

/* siteID = "MainOffice";

locationID = "MainOffice";

warehouseID = "MainOffice"; */

CreatePurchOrderClass createPO_Class = new CreatePurchOrderClass();


changecompany(strDataAreaId)
{
purchLine.clear();

purchTable_ = PurchTable::find(purchID);

purchLine.initFromPurchTable(purchTable_);

axPurchLine = AxPurchLine::newPurchLine(purchLine);
axpurchLine.parmItemId(itemID);
axPurchLine.parmPurchQty(10);
axPurchLine.parmPurchPrice(100);
// purchLine.InventDimId = createPO_Class.createInventDim(strDataAreaId, itemID, locationID, warehouseID, siteID);

axPurchLine.doSave();



result = (strFmt("New Purchase Line added to Purchase Order '%1'.", purchId));

}

return result;

}

Can you guys help me to debug this. 

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    239,036 Most Valuable Professional on at

    The error means that your X code threw an exception. Use the debugger to find out what's going on in your code.

    Please use Insert > Code to paste source code. It preserves line identation, which makes code much easier to read. Also, it shows line numbers, allows using syntax highlighting of C# code and so on. For example:

    try
    {
    	var response = request.GetResponse() as HttpWebResponse; //here is where im getting the exception
    	using (Stream responseStream = response.GetResponseStream())
    	{
    		using (StreamReader streamReader = new StreamReader(responseStream))
    		{
    			string responseString = streamReader.ReadToEnd();
    			//Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
    			//Assert.IsFalse(string.IsNullOrEmpty(responseString));
    			label2.Text = responseString;
    			Console.WriteLine(responseString);
    		}
    	}
    }

    By the way, couldn't you use don't you use OData services instead? There already are public data entities form purchase order headers and lines.

  • Tapiwa Louis Profile Picture
    35 on at

    Hi Martin, thank you for responding. I managed to debug my code and fixed the error.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 468 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 333 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans