I use dummy value for PO_NO and set inUpdate property of this field to False
because PO_NO will generate by before insert trigger on db server, After i insert on PO_Header i will use PO_NO from PO_Header to insert in PO_LinesItem But i can't get PO_NO value after i post PO_Header.
My Code is List in Bellow
function TcoGeneralPurchase.GeneratePO(const POIssueUser: WideString;
PRSLine_No: OleVariant): WideString;
var dToday: TDateTime;
i, intPONo,intPOLineNo: Integer;
BookMark: TBookMark;
begin
intPONo:=0;
dToday:=Date;
try
cdsPRSForGenPO.Close;
cdsPRSForGenPO.Open;
cdsPRSLinesItemForUpdate.Close;
cdsPRSLinesItemForUpdate.Open;
cdsPOHeader.Close;
cdsPOHeader.Open;
cdsPOLinesItem.Close;
cdsPOLinesItem.Open;
for i := 0 to VarArrayHighBound(PRSLine_No,1) do
begin
if cdsPRSForGenPO.Locate('LINE_NO',PRSLine_No,[]) then
begin
if not cdsPOHeader.Locate('Vendor_Code;Gen_PO_Date;Requster',
VarArrayOf([cdsPRSForGenPO.FieldByName('Vendor_Code').AsString,
dToday,
cdsPRSForGenPO.FieldByName('Reqester').AsString]),[]) then
begin
intPONo:=intPONo+1;
try
cdsPOHeader.Insert;
cdsPOHeader.FieldByName('PO_NO').AsFloat
:=intPONo;
cdsPOHeader.FieldByName('Vendor_Code').AsString
:=cdsPRSForGenPO.FieldByName('Vendor_Code').AsString;
cdsPOHeader.FieldByName('Gen_PO_Date').AsDateTime
:=dToday;
cdsPOHeader.FieldByName('Generate_PO_By').AsString
:=POIssueUser;
cdsPOHeader.FieldByName('Requster').AsString
:=cdsPRSForGenPO.FieldByName('Reqester').AsString;
cdsPOHeader.FieldByName('Currency_Code').AsString
:=cdsPRSForGenPO.FieldByName('Currency_code').AsString;
cdsPOHeader.FieldByName('Amount').AsFloat
:=cdsPOHeader.FieldByName('Amount').AsFloat
+cdsPRSForGenPO.FieldByName('Amount').AsFloat;
cdsPOHeader.Post;
BookMark:=cdsPOHeader.GetBookmark;
cdsPOHeader.ApplyUpdates(0);
cdsPOHeader.Refresh;
cdsPOHeader.GotoBookmark(BookMark);
cdsPOHeader.FreeBookmark(BookMark);
except
raise;
end;//try
end;//if
if cdsPOHeader.Locate('Vendor_Code;Gen_PO_Date;Requster',
VarArrayOf([cdsPRSForGenPO.FieldByName('Vendor_Code').AsString,
dToday,
cdsPRSForGenPO.FieldByName('Reqester').AsString]),[]) then
begin
try
intPONo:=cdsPOHeader.FieldByName('PO_No').AsInteger;
cdsPOLinesItem.Insert;
cdsPOLinesItem.FieldByName('Line_No').AsInteger:=i;
cdsPOLinesItem.FieldByName('PO_No').AsInteger
:=cdsPOHeader.FieldByName('PO_NO').AsInteger;
cdsPOLinesItem.FieldByName('Part_No').AsString
:=cdsPRSForGenPO.FieldByName('Part_No').AsString;
cdsPOLinesItem.FieldByName('Description').AsString
:=cdsPRSForGenPO.FieldByName('Description').AsString;
cdsPOLinesItem.FieldByName('Remain_Quantity').AsInteger
:=cdsPRSForGenPO.FieldByName('Quantity').AsInteger;
cdsPOLinesItem.FieldByName('Quantity').AsInteger
:=cdsPRSForGenPO.FieldByName('Quantity').AsInteger;
cdsPOLinesItem.FieldByName('Unit_Code').AsString
:=cdsPRSForGenPO.FieldByName('Unit_Code').AsString;
cdsPOLinesItem.FieldByName('Price_Per_Unit').AsFloat
:=cdsPRSForGenPO.FieldByName('Price_Per_Unit').AsFloat;
cdsPOLinesItem.FieldByName('Amount').AsFloat
:=cdsPRSForGenPO.FieldByName('Amount').AsFloat;
cdsPOLinesItem.Post;
BookMark:=cdsPOLinesItem.GetBookmark;
cdsPOLinesItem.ApplyUpdates(0);
cdsPOLinesItem.Refresh;
cdsPOLinesItem.GotoBookmark(BookMark);
intPOLineNO := cdsPOLinesItem.FieldByName('LINE_NO').AsInteger;
cdsPOLinesItem.FreeBookmark(BookMark);
except
raise;
end;//try
end;//if cdsPOHeader Locate
// Update PO_Status in PRS_Lines_item Table
if cdsPRSLinesItemForUpdate.Locate('LINE_NO',cdsPRSForGenPO.FieldByName('LINE_NO').AsInteger,[]) then
begin
try
cdsPRSLinesItemForUpdate.Edit;
cdsPRSLinesItemForUpdate.FieldByName('PRS_STATUS').AsString:='1';
cdsPRSLinesItemForUpdate.FieldByName('PO_NO').AsInteger:=intPONo;
cdsPRSLinesItemForUpdate.FieldByName('PO_LINE_NO').AsInteger:=intPOLineNO;//cdsPOLinesItem.FieldByName('PO_LINE_NO').AsInteger;
cdsPRSLinesItemForUpdate.Post;
cdsPRSLinesItemForUpdate.ApplyUpdates(0);
cdsPRSLinesItemForUpdate.Refresh;
except
raise;
end;//try
end;//if
end;//if Locate cdsPRSForGenPO
end;//for
result:='Success';
except
result:='Fail';
end;//try
end;