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

Announcements

News and Announcements icon
Community site session details

Community site session details

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

Creating pending vendor invoice through x++ code to release retention

(0) ShareShare
ReportReport
Posted on by 145

Hi all,

I am trying to create a pending vendor invoice to release just the vendor retention amount from a custom form. When I tried to post the pending vendor invoice that I have created through x++ code I am getting a generic error “
Posting - Exception has been thrown by the target of an invocation.”. When I was debugging I found that when the debugger gets into the formLetterService class – runSinglethread method, it gets catched by Exception:CLRError and the value of updateError variable in that method gets updates to true which remains as false when I create a pending vendor invoice through standard procedure and post. Because of that the above generic error is been thrown. But I was not able to find the root cause of this error. Please find below my code I wrote to create new pending vendor invoice header and lines. It would be helpful if anyone could help whether I am missing anything in my code or with some tips on how could I find the root cause of a generic error. Also when I tried to post the created pending invoice through the code below, once the error strikes, the parmID field in vendinvoiceinfotable gets cleared and the respective recordin the purchparmUpdate also gets deleted.

Pending Vendor invoice code:

//creating a purchparmupdate

purchFormLetterParmData = PurchFormLetterParmData::newData(DocumentStatus::Invoice, VersioningUpdateType::Initial);

            purchFormLetterParmData.init();

            purchFormLetterParmData.parmOnlyCreateParmUpdate(true);

            purchFormLetterParmData.createData(false);

            purchParmUpdate = purchFormLetterParmData.parmParmUpdate();

 

 

            vendTableLoc    = VendTable::find(purchTableLocal.InvoiceAccount);

 

 

            vendInvoiceInfoTable.clear();

            vendInvoiceInfoTable.initValue();

            vendInvoiceInfoTable.initFromPurchTable(purchTableLocal);

            vendInvoiceInfoTable.defaultRow(purchTableLocal, null, true);

            vendInvoiceInfoTable.DocumentOrigin             =    DocumentOrigin::Manual;

            vendInvoiceInfoTable.ParmId                     =    purchParmUpdate.ParmId;

            vendInvoiceInfoTable.DeliveryName               =    purchTableLocal.DeliveryName;

            vendInvoiceInfoTable.DeliveryPostalAddress      =    purchTableLocal.DeliveryPostalAddress;

            vendInvoiceInfoTable.CurrencyCode               =    purchTableLocal.CurrencyCode;

            vendInvoiceInfoTable.Approver                   =    HcmWorker::userId2Worker(curUserId());

            vendInvoiceInfoTable.VendorRequestedWorkerEmail =    HcmWorker::find(vendInvoiceInfoTable.Approver).email();

            vendInvoiceInfoTable.BatchAdministration        =    NoYes::Yes;

            vendInvoiceInfoTable.InvoiceAccount             =    purchTableLocal.InvoiceAccount;

            vendInvoiceInfoTable.OrderAccount               =    purchTableLocal.InvoiceAccount;

            vendInvoiceInfoTable.ListCode                   =    Listcode::IncludeNot;

            vendInvoiceInfoTable.VendInvoiceSaveStatus      =    VendInvoiceSaveStatus::Pending;

            vendInvoiceInfoTable.PurchName                  =    vendTableLoc.name();

            vendInvoiceInfoTable.Hold                       =    NoYes::No;

            vendInvoiceInfoTable.ParmJobStatus              =    ParmJobStatus::Waiting;

            vendInvoiceInfoTable.Approved                   =    NoYes::Yes;

            vendInvoiceInfoTable.BatchAdministration        =    NoYes::No;

            vendInvoiceInfoTable.ExchRate                   =    100;

            vendInvoiceInfoTable.LastMatchVariance          =    LastMatchVarianceOptions::OK;

            vendInvoiceInfoTable.Ordering                   =    DocumentStatus::Invoice;

            vendInvoiceInfoTable.StartDateTime              =    DateTimeUtil::utcNow(); 

            vendInvoiceInfoTable.DocumentDate               =    today();

            vendInvoiceInfoTable.TransDate                  =    today();

            vendInvoiceInfoTable.FixedDueDate               =    today();

            vendInvoiceInfoTable.PurchId                    =    purchTableLocal.PurchId;

            vendInvoiceInfoTable.PaymMode                   =    purchTableLocal.PaymMode;

            vendInvoiceInfoTable.RequestStatus              =    VendInvoiceRequestStatus::Draft;

            vendInvoiceInfoTable.SettleVoucher              =    SettlementType::None;

            vendInvoiceInfoTable.Storno                     =    NoYes::No;

            vendInvoiceInfoTable.Payment                    =    VendTable::find(purchTableLocal.InvoiceAccount).PaymTermId;

            vendInvoiceInfoTable.LastMatchVariance          =    LastMatchVarianceOptions::Unknown;

            vendInvoiceInfoTable.insert();

           

           

 

            if(vendInvoiceInfoTable.RecId)

            {

                vendInvoiceInfoSubTable.clear();

                vendInvoiceInfoSubTable.initValue();

                vendInvoiceInfoSubTable.SourceAccountingEvent = AccountingEvent::getLastEvent(purchTableLocal.SourceDocumentHeader).RecId;

                vendInvoiceInfoSubTable.defaultRow();

                vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;

                vendInvoiceInfoSubTable.OrigPurchId = vendInvoiceInfoTable.PurchId;

                vendInvoiceInfoSubTable.PurchName   = vendInvoiceInfoTable.PurchName;

                vendInvoiceInfoSubTable.ParmId      = vendInvoiceInfoTable.ParmId;

                vendInvoiceInfoSubTable.insert();

            }

 

 

            purchLineLocal.clear();

            //selecting the purchlines which has retained amount

            while select purchLineLocal

               where purchLineLocal.PurchId == purchTableLocal.PurchId

               &&    purchLineLocal.PSATotalRetainAmount > 0

            {

                inventTransIdSum = InventTransIdSum::newTransOriginId(InventTransOriginPurchLine::findInventTransOriginId(purchLineLocal.DataAreaId, purchLineLocal.InventTransId));

 

                vendInvoiceInfoLine.clear();

                vendInvoiceInfoLine.initValue();

                vendInvoiceInfoLine.initFromPurchLine(purchLineLocal);

                vendInvoiceInfoLine.DeliveryName            =  vendInvoiceInfoTable.DeliveryName;

                vendInvoiceInfoLine.ParmId                  =  vendInvoiceInfoTable.ParmId;

                vendInvoiceInfoLine.TableRefId              =  vendInvoiceInfoTable.TableRefId;

                vendInvoiceInfoLine.currencyCode            =  vendInvoiceInfoTable.CurrencyCode;

                vendInvoiceInfoLine.InvoiceAccount          =  vendInvoiceInfoTable.InvoiceAccount;

                vendInvoiceInfoLine.OrderAccount            =  vendInvoiceInfoTable.InvoiceAccount;

                vendInvoiceInfoLine.DocumentOrigin          =  DocumentOrigin::Manual;

                vendInvoiceInfoLine.ItemId                  =  purchLineLocal.ItemId;

                vendInvoiceInfoLine.PurchPrice              =  purchLineLocal.PurchPrice;

                vendInvoiceInfoLine.PurchUnit               =  purchLineLocal.PurchUnit;

                vendInvoiceInfoLine.InventNow               =  purchLineLocal.PurchQty;

                vendInvoiceInfoLine.ProcurementCategory     =  EcoResProductCategory::findByItemIdCategoryHierarchyRole(purchLineLocal.ItemId, EcoResCategoryNamedHierarchyRole::Procurement).Category;

                vendInvoiceInfoLine.OrigPurchId             =  purchTableLocal.PurchId;

                vendInvoiceInfoLine.LineNum                 =  purchLineLocal.LineNumber;

                vendInvoiceInfoLine.InventTransId           =  purchLineLocal.InventTransId;

                vendInvoiceInfoLine.InventDimId             =  purchLineLocal.InventDimId;

                vendInvoiceInfoLine.DefaultDimension        =  purchLineLocal.DefaultDimension;

                vendInvoiceInfoLine.LineType                =  PurchInvoiceLineType::Standard;

                vendInvoiceInfoLine.RequestStatus           =  VendInvoiceRequestStatus::Draft;

                vendInvoiceInfoLine.PurchLineRecId          =  purchLineLocal.RecId;

                vendInvoiceInfoLine.PurchaseLineLineNumber  =  purchLineLocal.LineNumber;

                vendInvoiceInfoLine.RemainBefore            =  purchLineLocal.RemainPurchFinancial + purchLineLocal.RemainPurchPhysical;

                vendInvoiceInfoLine.RemainBeforeInvent      =  purchLineLocal.RemainInventPhysical + inventTransIdSum.physical();

                vendInvoiceInfoLine.RemainBeforeInventPhysical = purchLineLocal.RemainInventPhysical;

                //Divided the retention release amount that the user gives and splits among the purchlines of the purchase order

                //Stored it in a map with key as linenumber and value as the splitted amount

                vendInvoiceInfoLine.PSAReleaseAmount        =  requestedAmountMap.lookup(purchLineLocal.LineNumber);

                vendInvoiceInfoLine.PSARetainageBalance     =  purchLineLocal.PSATotalRetainAmount;

                vendInvoiceInfoLine.PSAReleasePercent       =  (vendInvoiceInfoLine.PSAReleaseAmount/vendInvoiceInfoLine.PSARetainageBalance) * 100;

                vendInvoiceInfoLine.ReceiveNow              =  0;

                vendInvoiceInfoLine.LineAmount              =  purchLineLocal.PurchPrice * vendInvoiceInfoLine.ReceiveNow;

                vendInvoiceInfoLine.insert();

 

                if(vendInvoiceInfoLine)

                {

                    vendInvoiceInfoSubLine.clear();

                    vendInvoiceInfoSubLine.initValue();

                    vendInvoiceInfoSubLine.defaultRow();

                    vendInvoiceInfoSubLine.ParmId               =   vendInvoiceInfoLine.ParmId;

                    vendInvoiceInfoSubLine.LineRefRecId         =   vendInvoiceInfoLine.RecId;

                    vendInvoiceInfoSubLine.ReceiveNow           =   vendInvoiceInfoLine.ReceiveNow;

                    vendInvoiceInfoSubLine.InventNow            =   vendInvoiceInfoLine.ReceiveNow;

                    vendInvoiceInfoSubLine.LineRefRecId         =   vendInvoiceInfoLine.RecId;

                    vendInvoiceInfoSubLine.insert();

                }

       

            }

I have the same question (0)

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

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

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 658 Super User 2026 Season 1

#2
André Arnaud de Calavon Profile Picture

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

#3
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 570

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans