Programmatically Create Purchase Order Lines using Script Editor

While creating Purchase Order and multiple lines on button click using script editor system is throwing error “Invalid Sequence.” for your reference error and code is attached below

BLE

private void CreatePurchaseOrder(string Company, int VendorNum)
	{
		POAdapter objPOAdapter = new POAdapter(oTrans);
		objPOAdapter.BOConnect();
		if (objPOAdapter.GetNewPOHeader())
		{																			
			objPOAdapter.POData.Tables["POHeader"].Rows[0]["Company"]   = Company;
			objPOAdapter.POData.Tables["POHeader"].Rows[0]["VendorNum"] = VendorNum;  
			objPOAdapter.POData.Tables["POHeader"].Rows[0]["BuyerID"] = "KMR";       
			objPOAdapter.POData.Tables["POHeader"].Rows[0]["RowMod"]    = "A";				
		}		
		objPOAdapter.Update();

		EpiDataView edvBpmData = ((EpiDataView)(this.oTrans.EpiDataViews["CallContextBpmData"]));
		System.Data.DataRow edvBpmDataRow = edvBpmData.CurrentDataRow;
		if (edvBpmDataRow != null)
		{
			int PONum = Convert.ToInt32(edvBpmDataRow["Number01"]);
			if(PONum > 0)
			{
				EpiDataView edvUD101A = (EpiDataView)oTrans.EpiDataViews["UD101A"];
				if ((edvUD101A != null) && (edvUD101A.dataView.Count > 0))
				{
					//bool result = objPOAdapter.GetNewPODetail(PONum);
					int Line = 1;
					foreach(DataRow objdrUD101A in edvUD101A.dataView.Table.Rows) 
			    	{						
						var objdr = objPOAdapter.POData.PODetail.NewPODetailRow();
						objdr.BeginEdit();
						objdr["Company"]   = objdrUD101A["Company"].ToString();
						objdr["PONum"]     = PONum;
						objdr["POLine"]    = Line;
						objdr["PartNum"]   = objdrUD101A["ShortChar01"].ToString();
						objdr["LineDesc"]  = objdrUD101A["Character01"].ToString();
						objdr["IUM"]  	 = objdrUD101A["ShortChar06"].ToString();
						objdr["PUM"]  	 = objdrUD101A["ShortChar06"].ToString();
						objdr["QtyOption"] = "Our";
						objdr["OrderQty"]  = Convert.ToDecimal(objdrUD101A["Number01"]);
						objdr["XOrderQty"] = Convert.ToDecimal(objdrUD101A["Number01"]);
						objdr["CalcOurQty"]  = Convert.ToDecimal(objdrUD101A["Number01"]);
						objdr["CalcVendQty"] = Convert.ToDecimal(objdrUD101A["Number01"]);
						objdr["DocUnitCost"] = objdrUD101A["Number02"];
						objdr["QtyChgReq"]   = false;			  		  
						objdr["CalcTranType"] = "PUR-STK";
						objdr["RowMod"]     = "A";
						objdr.EndEdit();
						objPOAdapter.POData.PODetail.AddPODetailRow(objdr);
						objPOAdapter.Update();						
						Line++;																		
					}		
				}
			}					
			objPOAdapter.Dispose();	
			EpiMessageBox.Show("Purchase Order No." + edvBpmDataRow["Number01"].ToString() + " created sucessfully !!");
		} 
	}

thanks in advance.

Can you really assign the line number to the PO?

@utaylor , If we are not passing the PO line then system is throwing error “PO Lines does not allow nulls.”

Did you pass 0?

Or did you pass nothing?

Try making that row with GetNewPODetail.

1 Like

I have done the below mentioned changes now it’s working fine.

private void CreatePurchaseOrder(string Company, int VendorNum)
{
	POAdapter objPOAdapter = new POAdapter(oTrans);
	objPOAdapter.BOConnect();
	if (objPOAdapter.GetNewPOHeader())
	{																			
		objPOAdapter.POData.Tables["POHeader"].Rows[0]["Company"]   = Company;
		objPOAdapter.POData.Tables["POHeader"].Rows[0]["VendorNum"] = VendorNum;  
		objPOAdapter.POData.Tables["POHeader"].Rows[0]["BuyerID"]   = "KMR";        
		objPOAdapter.POData.Tables["POHeader"].Rows[0]["RowMod"]    = "A";				
	}		
	objPOAdapter.Update();
	objPOAdapter.Dispose();
	
	// Get New PONum
	EpiDataView edvBpmData = ((EpiDataView)(this.oTrans.EpiDataViews["CallContextBpmData"]));
	System.Data.DataRow edvBpmDataRow = edvBpmData.CurrentDataRow;
	int PONum = Convert.ToInt32(edvBpmDataRow["Number01"]);
	if(PONum > 0)
	{
		EpiDataView edvUD101A = (EpiDataView)oTrans.EpiDataViews["UD101A"];
		if ((edvUD101A != null) && (edvUD101A.dataView.Count > 0))
		{
				int i = 0;
				foreach(DataRow objdrUD101A in edvUD101A.dataView.Table.Rows) 
			    {	
					POAdapter objAdapter = new POAdapter(oTrans);				
					objAdapter.BOConnect();
					objAdapter.GetByID(PONum);
					if (objAdapter.GetNewPODetail(PONum))
					{	
						objAdapter.POData.Tables["PODetail"].Rows[i]["POLine"]  = i+1;																		
						//objAdapter.POData.Tables["PODetail"].Rows[i]["PartNum"] = objdrUD101A["ShortChar01"].ToString();
						string part = objdrUD101A["ShortChar01"].ToString();
						bool a = false;
						bool b = false;
						System.Guid sysRowID = new System.Guid("00000000-0000-0000-0000-000000000000");
						objAdapter.ChangeDetailPartNum(ref part, sysRowID, "", a, out b);
						objAdapter.POData.Tables["PODetail"].Rows[i]["QtyOption"]   = "Our";
						decimal Qty = Convert.ToDecimal(objdrUD101A["Number01"]);
						objAdapter.POData.Tables["PODetail"].Rows[i]["OrderQty"]    = Qty;
						objAdapter.POData.Tables["PODetail"].Rows[i]["XOrderQty"]   = Qty;
					
						objAdapter.ChangeDetailTranType("PUR-STK");
						objAdapter.ChangeDetailCalcOurQty(Qty);
						objAdapter.ChangeDetailCalcVendQty(Qty);
						decimal UnitPrice = Convert.ToDecimal(objdrUD101A["Number02"]);
						objAdapter.POData.Tables["PODetail"].Rows[i]["UnitCost"]   = UnitPrice;
						objAdapter.POData.Tables["PODetail"].Rows[i]["DocUnitCost"]   = UnitPrice;        
						objAdapter.POData.Tables["PODetail"].Rows[i]["RowMod"]      = "A";	
						objAdapter.Update();		
					}
					objAdapter.Dispose();
					i++;
				}		
		}
		EpiMessageBox.Show("Purchase Order No." + PONum + " created sucessfully !!");
	} 
}
1 Like

Unit price is showing zero in Purchase Order, Any suggestion?

Haven’t done this with PO’s before, but aren’t you missing something after changing the unit price ? Mine calls ChangeUnitPriceConfirmOverride and ChangeUnitPrice. Maybe have a look at that ?

I also follow you but this error turns like it can’t generate POrel:

Server Side Exception

BPM runtime caught an unexpected exception of ‘NullReferenceException’ type.
See more info in the Inner Exception section of Exception Details.

Exception caught in: Epicor.ServiceModel

Error Detail

Description: BPM runtime caught an unexpected exception of ‘NullReferenceException’ type.
See more info in the Inner Exception section of Exception Details.
Program: ERP.PORel.Triggers.dll
Method: A001_CustomCodeAction
Original Exception Type: NullReferenceException

Client Stack Trace

at Epicor.ServiceModel.Channels.ImplBase`1.ShouldRethrowNonRetryableException(Exception ex, DataSet[] dataSets)
at Erp.Proxy.BO.POImpl.Update(PODataSet ds)
at Erp.Adapters.POAdapter.OnUpdate()
at Ice.Lib.Framework.EpiBaseAdapter.Update()
at Script.CallPOAdapterGetNewPOHeaderMethod(Int32 DMRNum, Int32 VendorNum, String VendorID, String BuyerID, String PartNum, Decimal DMRHead_TotRejectedQty, Decimal UnitPrice)

Inner Exception

Object reference not set to an instance of an object.
here is my code:

private void CallPOAdapterGetNewPOHeaderMethod(int DMRNum, int VendorNum, string VendorID, string BuyerID, string PartNum, decimal DMRHead_TotRejectedQty, decimal UnitPrice)
	{
		try
		{
			// Declare and Initialize EpiDataView Variables
			// Declare and create an instance of the Adapter.
			POAdapter adapterPO = new POAdapter(this.oTrans);
			adapterPO.BOConnect();

			// Call Adapter method
			if(adapterPO.GetNewPOHeader())
			{
				adapterPO.POData.Tables["POHeader"].Rows[0]["VendorNum"] = VendorNum;
				adapterPO.ChangeVendor(VendorID);
				adapterPO.POData.Tables["POHeader"].Rows[0]["BuyerID"]   = BuyerID;   
				adapterPO.Update();	
			}	
			//New PODetail
			string sConfirmMsg = string.Empty;
			bool a = false, b = false;	
			int i=0, Line =1;	
			Guid sysRowID = new System.Guid("00000000-0000-0000-0000-000000000000");
			int PONumNew = Int32.Parse(adapterPO.POData.Tables["POHeader"].Rows[0]["PONum"].ToString());
			
			/*var PODetailNew = adapterPO.POData.PODetail.NewPODetailRow();
			PODetailNew.BeginEdit();
			PODetailNew["Company"]   = adapterPO.POData.Tables["POHeader"].Rows[0]["Company"].ToString();
			PODetailNew["PONum"]     = PONumNew;
			PODetailNew["POLine"]    = Line;
			PODetailNew["PartNum"]   = PartNum;			
			PODetailNew["LineDesc"]  = "Hải test thử";
			PODetailNew["IUM"]  	 = "EA";
			PODetailNew["PUM"]  	 = "EA";
			PODetailNew["QtyOption"] = "Our";
			PODetailNew["DueDate"] =  DateTime.Now.Date;				
			PODetailNew["OrderQty"]  = DMRHead_TotRejectedQty;
			PODetailNew["XOrderQty"] = DMRHead_TotRejectedQty;
			PODetailNew["DocScrUnitCost"]  = UnitPrice;
			PODetailNew["UnitCost"] = UnitPrice;
			PODetailNew["DocUnitCost"] = UnitPrice;
			PODetailNew["QtyChgReq"]   = false;			  		  
			PODetailNew["CalcTranType"] = "PUR-STK";			
			if (UnitPrice ==0)
				PODetailNew["CheckBox02"]  = true;//Gift
			PODetailNew["RowMod"]     = "A";
			PODetailNew.EndEdit();
			adapterPO.POData.PODetail.AddPODetailRow(PODetailNew);
			adapterPO.Update();	*/				
			if (adapterPO.GetNewPODetail(Int32.Parse(adapterPO.POData.Tables["POHeader"].Rows[0]["PONum"].ToString())))
			{	
				adapterPO.POData.Tables["PODetail"].Rows[i]["POLine"]= Line;				
				adapterPO.ChangeDetailPartNum(ref PartNum, sysRowID, "", a, out b);
				adapterPO.POData.Tables["PODetail"].Rows[i]["QtyOption"]   = "Our";	
				adapterPO.POData.Tables["PODetail"].Rows[i]["LineDesc"]  = "Hải test thử";
				adapterPO.POData.Tables["PODetail"].Rows[i]["DueDate"]   = DateTime.Now.Date;				
				adapterPO.POData.Tables["PODetail"].Rows[i]["OrderQty"]    = DMRHead_TotRejectedQty;
				adapterPO.POData.Tables["PODetail"].Rows[i]["XOrderQty"]   = DMRHead_TotRejectedQty;
				adapterPO.POData.Tables["PODetail"].Rows[i]["DocScrUnitCost"]   = UnitPrice;
				adapterPO.POData.Tables["PODetail"].Rows[i]["DocUnitCost"]  = UnitPrice;
				adapterPO.POData.Tables["PODetail"].Rows[i]["UnitCost"]  = UnitPrice;
				adapterPO.ChangeDetailTranType("PUR-STK");
				adapterPO.ChangeDetailCalcOurQty(DMRHead_TotRejectedQty);
				adapterPO.ChangeDetailCalcVendQty(DMRHead_TotRejectedQty);					
				if (UnitPrice ==0)
					adapterPO.POData.Tables["PODetail"].Rows[i]["CheckBox02"]  = true;//Gift				
				adapterPO.ChangeUnitPriceConfirmOverride(out sConfirmMsg);     
				adapterPO.ChangeUnitPrice();				   
				//adapterPO.POData.Tables["PODetail"].Rows[i]["RowMod"]      = "A";
				//if (adapterPO.GetNewPORel(Int32.Parse(adapterPO.POData.Tables["POHeader"].Rows[0]["PONum"].ToString()),1))
				//{
				//	adapterPO.POData.Tables["PORel"].Rows[i]["DueDate"]   = DateTime.Now.Date;		
				//}
				adapterPO.Update();	
			}
			// Cleanup Adapter Reference						
			adapterPO.Dispose();

		} catch (System.Exception ex)
		{
			ExceptionBox.Show(ex);
		}
	}

:thinking: :thinking: :thinking:
Thank you have a nice day! :smiling_face_with_three_hearts: :smiling_face_with_three_hearts:

Remove that.

I think that SysRowID would be this one
objAdapter.POData.Tables["PODetail"].Rows[i]["SysRowID"]

2 Likes

for currency currency, the CurrencySwitch property is False and for foreign currency it will be True, the default system is True
adapterPO.POData.Tables[“PODetail”].Rows[i][“CurrencySwitch”] = false;

private void CallPOAdapterGetNewPOHeaderMethod( int VendorNum, string VendorID, DataTable tblVenNumPOGen)
	{
		try
		{
			// Declare and Initialize EpiDataView Variables
			// Declare and create an instance of the Adapter.
			POAdapter adapterPO = new POAdapter(this.oTrans);
			adapterPO.BOConnect();

			// Call Adapter method
			if(adapterPO.GetNewPOHeader())
			{
				adapterPO.POData.Tables["POHeader"].Rows[0]["VendorNum"] = VendorNum;
				adapterPO.ChangeVendor(VendorID);
				adapterPO.POData.Tables["POHeader"].Rows[0]["BuyerID"]   = BuyerID;   
				adapterPO.Update();	
			}	
			//New PODetail
			string sConfirmMsg = string.Empty, PartNum =string.Empty;
			bool a = false, b = false;	
			int i=0, Line =1, DMRNum=0;	
			Guid sysRowID = new System.Guid("00000000-0000-0000-0000-000000000000");
			int PONumNew = Int32.Parse(adapterPO.POData.Tables["POHeader"].Rows[0]["PONum"].ToString());
            decimal DMRHead_TotRejectedQty=0, UnitPrice = 0;
			//adapterPO.GetByID(PONumNew);	
			foreach (DataRow dtPOLine in tblVenNumPOGen.Rows)
			{		
				DMRNum = Int32.Parse(dtPOLine["DMRHead_DMRNum"].ToString());
				PartNum = dtPOLine["DMRHead_PartNum"].ToString();
				DMRHead_TotRejectedQty = Convert.ToDecimal(dtPOLine["DMRHead_TotRejectedQty"].ToString());
				UnitPrice = Convert.ToDecimal(dtPOLine["APInvDtl_UnitCost"].ToString());				
				if (adapterPO.GetNewPODetail(PONumNew))
				{	
					adapterPO.POData.Tables["PODetail"].Rows[i]["POLine"]= Line;				
					adapterPO.ChangeDetailPartNum(ref PartNum, Guid.Parse(adapterPO.POData.Tables["PODetail"].Rows[i]["SysRowID"].ToString()), "", a, out b);
					adapterPO.POData.Tables["PODetail"].Rows[i]["QtyOption"]   = "Our";			
					adapterPO.POData.Tables["PODetail"].Rows[i]["DueDate"]   = DateTime.Now.Date;				
					//adapterPO.POData.Tables["PODetail"].Rows[i]["OrderQty"]    = DMRHead_TotRejectedQty;//Suppler Qty (VenQty)
					//adapterPO.POData.Tables["PODetail"].Rows[i]["XOrderQty"]   = DMRHead_TotRejectedQty;//Our Qty					
					adapterPO.ChangeDetailTranType("PUR-STK");
					adapterPO.ChangeDetailCalcOurQty(DMRHead_TotRejectedQty);
					//adapterPO.ChangeDetailCalcVendQty(DMRHead_TotRejectedQty);					
					if (UnitPrice ==0)
					{
						adapterPO.POData.Tables["PODetail"].Rows[i]["CheckBox02"]  = true;//Gift	
					}
					//set lại giá sau Qty để không bị lấy giá mặc trong PriceList
					//nếu là tiền VND cần update lại không sẽ lấy giá về 0					
					if (adapterPO.POData.Tables["POHeader"].Rows[0]["CurrencyCode"].ToString()=="VND")
					{
						adapterPO.POData.Tables["PODetail"].Rows[i]["CurrencySwitch"]   = false;
					}		
					adapterPO.POData.Tables["PODetail"].Rows[i]["DocScrUnitCost"]   = UnitPrice;
					adapterPO.POData.Tables["PODetail"].Rows[i]["DocUnitCost"]  = UnitPrice;//cột này là cột lưu đúng theo giá nếu là ngoại tệ sẽ là cột này
					//adapterPO.POData.Tables["PODetail"].Rows[i]["DocExtCost"]  = UnitPrice;
					//adapterPO.POData.Tables["PODetail"].Rows[i]["UnitCost"]  = UnitPrice;//cột này hệ thống sẽ tự quy đổi về tiền theo quốc gia của hệ thống ở VN là VND			
					adapterPO.ChangeUnitPriceConfirmOverride(out sConfirmMsg);     
					//adapterPO.ChangeUnitPrice();	
					adapterPO.POData.Tables["PODetail"].Rows[i]["RowMod"] = "A";
					adapterPO.Update();						
					adapterPO.POData.Tables["PORel"].Rows[i]["Number02"] = DMRNum;
					adapterPO.POData.Tables["PORel"].Rows[i]["Date01"] = adapterPO.POData.Tables["PODetail"].Rows[i]["DueDate"] ;//Delivery Date
					adapterPO.Update();
				}
				i++;
				Line++;
			}
			if (POLimit==1)
			{
				adapterPO.POData.Tables["POHeader"].Rows[0]["Approve"] = true;
				adapterPO.POData.Tables["POHeader"].Rows[0]["ApprovalStatus"] = "A";
			}
			else
			{
				adapterPO.POData.Tables["POHeader"].Rows[0]["ApprovalStatus"] = "P";
			}
			adapterPO.Update();
			// Cleanup Adapter Reference						
			adapterPO.Dispose();

		} catch (System.Exception ex)
		{
			ExceptionBox.Show(ex);
		}
	}

Thank you have a nice day! :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: