Creating Purchase Requisition, Purchsae req line & Purchase order through X++

// Workflow “PurchReqReview” is modified as Current user is to approve the workflow.
//Below Job is to create a New Purchase Requisition,Purch Req line and on approval creating a Purchase Order.

static void CreatePurchRequisition(Args _args)
{
PurchReqTable purchReqTable;
PurchReqApprovalEventHandler PurchReqApprovalEventHandler;
PurchReqLine purchReqLine;
ProjTable projTable = projTable::find(“‪‪‪0000002”);
SalesLine salesLine = SalesLine::findInventTransId(“000222”);

ttsbegin;
purchReqTable.clear();
purchReqTable.initValue();
purchReqTable.PurchReqId = NumberSeq::newGetNum(PurchReqTable::numRefPurchReqId()).num();
purchReqTable.PurchReqName = “Testname”;
purchReqTable.ProjId = projTable.ProjId;
purchReqTable.ProjIdDataArea = projTable.dataAreaId;
purchReqTable.insert();

purchReqLine.clear();
purchReqLine.initValue();
purchReqLine.InventDimId = salesLine.InventDimId;
purchReqLine.initFromPurchReqTable(purchReqTable);
purchReqLine.ItemId = salesLine.ItemId;
purchReqLine.ItemId = “C0004”;
purchReqLine.ActivityNumber = salesLine.ActivityNumber;
purchReqLine.BuyingLegalEntity = CompanyInfo::find().RecId;
purchReqLine.InventDimIdDataArea = curext();
purchReqLine.ProcurementCategory = 22565423433;
purchReqLine.ReceivingOperatingUnit = 22565423099;
purchReqLine.Name = “Lifecam HD 5000”;
purchReqLine.PurchQty = 1;
purchReqLine.DefaultDimension = str2int64(“22565466744”);
purchReqLine.PurchUnitOfMeasure = 22565421199;
purchReqLine.PurchPrice = 39.95;
purchReqLine.LineAmount = 39.95;
purchReqLine.CurrencyCode = “USD”;
purchReqLine.initFromProjTable(projTable);
purchReqLine.VendAccount = “US-111”;
purchReqLine.insert();

purchReqLine.LineType = PurchReqLineType::Item;

if (purchReqLine.CurrencyCode != purchReqLine.orig().CurrencyCode && purchReqLine.orig().CurrencyCode)
{
purchReqLine.write();
}

if (PurchReqWFStatusTransitionHelper::setPurchReqTable2InReview(purchReqTable.RecId))
{
Workflow::activateFromWorkflowType(‘PurchReqReview’,
purchReqTable.RecId,
‘Auto-Submitted into workflow’,
false,
curUserId() );
}

PurchReqWFStatusTransitionHelper::setPurchReqTable2Approved(purchReqTable.RecId,false);

ttsCommit;
info(purchReqTable.PurchReqId);
}