Friends, below code will help you to create projects and sub projects using code. If there is already a parent project and we need to create sub projects based on the format which is already defined, the sample job below is an example. [Please note: Try, test and modify as per your requirements]
static void SR_CreateProjects_subProjects(Args _args)
{
ProjInvoiceTable projInvoiceTable;
ProjTableType projTableType;
NumberSeq projNumberSeq;
ProjId projIdLastSon, projectId;
ProjTable projTable;
ProjId projMask;
Integer sonNum;
ProjType _projType = ProjType::TimeMaterial;
ProjGroupId _projGroupId = ‘TM1’;
ProjName _projName = “Construction – Prj”;
ProjInvoiceProjId _projInvoiceProjId = ‘100104’;
ProjParentId _projParentId = ‘121’; // define parent project here
NumberSequenceFormat _numberSequenceMask;
ProjLinePropertyId _projInvoiceStatusId;
;
if (_projParentId)
{
projTable.type().initFromParent(_projParentId);
projIdLastSon = ProjTable::projIdLastSon(projTable.ParentId);
if (projIdLastSon)
{
projMask = substr(projIdLastSon, strlen(projTable.ParentId) + 1, (strlen(projIdLastSon) – strlen(projTable.ParentId) + 1));
sonNum = ProjTable::numRemTemplate(projMask, ProjTable::find(projTable.ParentId).Format);
}
projTable.ProjId = projTable.ParentId + NumberSeq::numInsertFormat(sonNum + 1, ProjTable::find(projTable.ParentId).Format);
}
else
{
projNumberSeq = NumberSeq::newGetNum(ProjParameters::numRefProjId(), true);
if (projNumberSeq)
{
projTable.ProjId = projNumberSeq.num();
}
}
projectId = projTable.ProjId;
projTable.Type = _projType;
projTable.ProjGroupId = _projGroupId;
projTable.Name = _projName;
projTableType = projTable.type();
projTableType.initProjTable();
projTable.ProjInvoiceProjId = _projInvoiceProjId;
projInvoiceTable = ProjInvoiceTable::find(_projInvoiceProjId);
projTable.CustAccount = projInvoiceTable.InvoiceAccount;
projTable.initFromInvoice(projInvoiceTable);
projTable.Format = _numberSequenceMask;
projTable.CheckBudget = ProjGroup::find(_projGroupId).CheckBudget;
if (_projInvoiceStatusId)
{
ProjLinePropertySetup::updateLinePropertyProj(projTable.ProjId, _projInvoiceStatusId, TableGroupAll::Table, true);
}
projTable.initFromCustTable(CustTable::find(projTable.CustAccount));
if (ProjTable::exist(projTable.ProjId))
{
// Project already exists.
throw error(“@SYS56494”);
}
if (!projTableType.validateWrite())
throw error (“Validations failed”);
projTable.insert();
if (projNumberSeq)
{
projNumberSeq.used();
}
else
{
projTable.clear();
if (projNumberSeq)
{
projNumberSeq.abort();
}
}
info (strfmt(‘Project %1 successfully created ‘, projectId));
}