Your code is difficult to follow. You should use single-responsibiity methods with descriptive name (not just a bunch of code in clicked(), remove unused variable, use "_" prefix just for parameters and not local variable and so on.
Let me at least throw away unused variables (= most variables) and commented-out code and move variable declarations closer to where variables are used:
PurchId _localDialogPurchId = ParentPurchId.valueStr();
Amount _totalAmount = PurchLine::find(_localDialogPurchId).sumpurchLine();
MainAccountNum debitAccount = MainAccount.text();
MainAccountNum creditAccount = PurchLine::find(_localDialogPurchId).displayMainAccount();
Name dep = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"ADepartment");
Name divi = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"BDivisionals");
Name city = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"DCity");
Name region = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"CRegional");
Name area = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"EArea");
if(!_localDialogPurchId || !_totalAmount || !debitAccount || !creditAccount)
{
info(strFmt( " PurchId : %1 TotalAmount: %2 Debit Acount: %3 Credit Account: %4",_localDialogPurchId,_totalAmount,debitAccount,creditAccount));
throw error("Above field is missing");
}
super();
LedgerJournalTable journalTable;
journalTable.JournalNum = JournalTableData::newTable(journalTable).nextJournalId();
journalTable.JournalName = "RC-13";
journalTable.Name = "Reclassification";
journalTable.CurrencyCode = "SAR";
journalTable.doinsert();
//Debit
LedgerJournalTrans journalTrans;
journalTrans.JournalNum = journalTable.JournalNum;
journalTrans.LineNum = LedgerJournalTrans::lastLineNum(journalTrans.JournalNum) 1;
journalTrans.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
journalTrans.AccountType = LedgerJournalACType::Ledger;
container container accCon = [debitAccount, debitAccount,5,"ADepartment",dep,"BDivisionals",divi,"CRegional",region,"DCity",city,"EArea",area];
//journalTrans.LedgerDimension = AxdDimensionUtil::getLedgerAccountId(accCon);
journalTrans.AmountCurDebit = _totalAmount;
journalTrans.AmountCurCredit = 0;
journalTrans.Txt = strfmt("%1-Reclassfication",_localDialogPurchId);
journalTrans.CurrencyCode = "SAR";
journalTrans.doinsert();
//Credit
LedgerJournalTrans journalTrans1;
offSetCon = [creditAccount, creditAccount, 5,"ADepartment",dep,"BDivisionals",divi,"CRegional",region,"DCity",city,"EArea",area];
journalTrans1.JournalNum = journalTable.JournalNum;
journalTrans1.LineNum = LedgerJournalTrans::lastLineNum(journalTrans.JournalNum) 1;
journalTrans1.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
journalTrans1.AccountType = LedgerJournalACType::Ledger;
journalTrans1.CurrencyCode = "SAR";
//journalTrans1.LedgerDimension = AxdDimensionUtil::getLedgerAccountId(offSetCon);
journalTrans1.AmountCurDebit = 0;
journalTrans1.AmountCurCredit = _totalAmount;
journalTrans.Txt = strfmt("%1-Reclassfication",_localDialogPurchId);
journalTrans1.doinsert();
//--------------------------------------
//for updating journal Num into PurchTable
PurchTable updatePurchT;
select forUpdate updatePurchT
where updatePurchT.PurchId == _localDialogPurchId;
if (updatePurchT)
{
ttsBegin;
updatePurchT.JournalNum = "" ;//journalTable.ledgerJournalTable().JournalNum;
updatePurchT.doupdate();
ttsCommit;
}
Args args = new Args();
args.record(LedgerJournalTable::find(journalTable.JournalNum));
new MenuFunction(MenuItemDisplayStr(LedgerJournalTable3),MenuItemType::Display).run(Args);
It seems to me that the relevant code is just this:
PurchTable updatePurchT;
select forUpdate updatePurchT
where updatePurchT.PurchId == _localDialogPurchId;
if (updatePurchT)
{
ttsBegin;
updatePurchT.JournalNum = "" ; //journalTable.ledgerJournalTable().JournalNum;
updatePurchT.doupdate();
ttsCommit;
}
Why are you doing that?
By the way, I see a plenty of other problems in your code. For example, you don't initialize LedgerJournalTable from journal name, don't initialize LedgerJournalTrans from LedgerJournalTable, forget to call validateWrite(), calling doinsert() instead of instert() on journal tables isn't correct (you'll be skipping businss logic by that), you don't insert records in a transaction and so on. This may lead to to many bugs and even data corruption in your production system.