Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

Create Project through x++

(0) ShareShare
ReportReport
Posted on by 85

Hello All,

I would like to create a project using x++ in 2012 R2.

First I've made a class that create a project contract using the following code,

str ContCr(str CustAcc)
{
    ProjInvoiceTable projInvoiceTable;
    CustTable custTable;
    CustAccount custAccount;
    ProjFundingSource projFundingSource;
    NumberSeq numSeq;


    info("Project contract is being created");

    ttsbegin;
    projInvoiceTable.clear();
    projInvoiceTable.initValue();
    custTable = CustTable::find(CustAcc);
    numSeq = NumberSeq::newGetNum(ProjParameters::numRefProjInvoiceProjId(),true,true);

    projInvoiceTable.ProjInvoiceProjId  = numSeq.num();
    projInvoiceTable.CurrencyId         = "EGP";
    projInvoiceTable.Description        = custTable.name();
    projInvoiceTable.insert();

    info("projInvoiceTable.ProjInvoiceProjId: " + projInvoiceTable.ProjInvoiceProjId);
    projFundingSource.ContractId         = projInvoiceTable.ProjInvoiceProjId;

    projFundingSource.CustAccount        = CustAcc;
    projFundingSource.FundingSourceId    = custTable.name();
    projFundingSource.FundingType        = ProjFundingType::Customer;

    projFundingSource.editProjFunder(true,projFundingSource.CustAccount);

    projFundingSource.insert();

    projFundingRule::createDefaultFundingRule(projInvoiceTable.ProjInvoiceProjId, projInvoiceTable.RecId);

    ttscommit;
    return projInvoiceTable.ProjInvoiceProjId;
}

It works successfully, But as a return of this contract I'm trying to create the project itself using the following code,

void clicked()
{

    ProjInvoiceTable projInvoiceTable;
    ProjTableType projTableType;
    NumberSeq projNumberSeq;
    ProjId projIdLastSon, projectId;
    ProjTable projTable;
    ProjId projMask;
    ProjName _projName;
    Integer sonNum;
    ProjType _projType;
    ProjGroupId _projGroupId;
    ProjInvoiceProjId _projInvoiceProjId;
    ProjLinePropertyId _projInvoiceStatusId;
    str ContID;



    super();

    ContID = this.ContCr("0006-Custom");

    _projType = ProjType::TimeMaterial;
    //1#
    _projGroupId = 'T&M';
    _projName = "Proj-Test";
    //2#
    _projInvoiceProjId = ContID;

    projTable.ProjId = '19992';
    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.Name;
    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));

}


Project is successfully created but all customer information is not provided to the project.

any help?

*This post is locked for comments

  • Ahmed Awwad Profile Picture
    Ahmed Awwad 85 on at
    RE: Create Project through x++

    BTW I've got this code from

    dynamicsaxgyan.wordpress.com/.../create-projects-or-sub-projects-using-x-dynamics-ax

    But I only wants to create a project with no sub projects so I've tried to modify the code

  • Ahmed Awwad Profile Picture
    Ahmed Awwad 85 on at
    RE: Create Project through x++

    Actually no, The error is not related to the project id as it not exist and when I run this code it creates it but with no customer data

    But if I added the following line

    projTable.CustAccount = "0006-Custom";

    Project is not created and give me this error

    Cannot create a record in Locations (LogisticsLocation). Location ID: 000002339, Custom - Ahmed Awwad.

    The record already exists.

  • Mea_ Profile Picture
    Mea_ 60,278 on at
    RE: Create Project through x++

    That's because you cannot have 2 projects with the same id and you have it hardcoded

    projTable.ProjId = '19992';

    Either change it or delete existing project.

  • Ahmed Awwad Profile Picture
    Ahmed Awwad 85 on at
    RE: Create Project through x++

    You are totally right I was testing and forget to return code back.

    Original code was

    projTable.CustAccount = projInvoiceTable.Name


    But it returns nothing because projInvoiceTable doesn't have name.

    Anyway for testing purpose I've provided the customer account manually like

    projTable.CustAccount = "0006-Custom";


    but It gives me error says something like record already exist

    I will provide the full screenshot for the error once I be in this location tomorrow morning.

    But seems like this code was not for R2 :(

  • Suggested answer
    Mea_ Profile Picture
    Mea_ 60,278 on at
    RE: Create Project through x++

    Hi Ahmed Awwad,

    Are you sure that 

    CustTable::find(projTable.CustAccount) 

    returns you custTable ? Because you initiliaze projTable.CustAccount with projInvoiceTable.Description that does not make a lot of sense.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,817 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans