Skip to main content

Notifications

Community site session details

Community site session details

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

Update first name and last name in dirpersonname through X++ code

(0) ShareShare
ReportReport
Posted on by 55

Hello,

I'm new in Dynamics 365 for finance and operations.

I have to update the first name and last name of a customer of type person, using x code.

I found that the fields FirstName and LastName are stored in the table DirPersonName. I tried the code below, but the name is not being updated.

Can anyone help me?

CustTable       custTable;
DirPartyTable   dirPartyTable;
DirPersonName   dirPersonName;
DirPerson       dirPerson;
AccountNum      accountNum;
FirstName       firstName;
LastName        lastName;
date            dateFrom,
                dateTo;

custTable = CustTable::find(accountNum, true);

if(custTable)
{
    ttsbegin;

    select forupdate dirPartyTable
        where dirPartyTable.RecId == custTable.Party;
    if(dirPartyTable.partyType() == dirPartyType::Person)
    {
        select forupdate dirPerson
        where dirPerson.PartyNumber == dirPartyTable.PartyNumber;
        if(dirPerson)
        {
            datefrom = 01\01\1900;
            dateTo = 31\12\2154;


            select forupdate validtimestate(dateFrom, dateTo) * from dirPersonName
                where dirPersonName.Person == dirPerson.RecId;

            if(dirPersonName)
            {
                info(strFmt("update dirpersonname %1",firstName));
                dirPersonName.FirstName = firstName;
                dirPersonName.LastName = lastName;
                
                dirperson.updateName(dirPersonName);
                dirPerson.update();
            }
        }
    } 
    ttscommit;       
}

  • Verified answer
    chrisrojas Profile Picture
    55 on at
    RE: Update first name and last name in dirpersonname through X++ code

    Hi, Gunjan.

    Thanks for answering my question, I tried the code that you suggested but I got the same results.

    It finally worked by replacing this code:

    dirperson.updateName(dirPersonName);
    dirPerson.update();

    With this one:

    dirPersonName.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
    dirPersonName.update();

  • Gunjan Bhattachayya Profile Picture
    35,421 on at
    RE: Update first name and last name in dirpersonname through X++ code

    Hi Chris,

    Did you debug your code to check if all the select statements are fetching the correct record and your update statement is getting called?

    Looking at your code, I think the issue could be with the select statement for dirPersonName. Please check this link to see how update operations can be performed using validtimeState.

    You can try this statement instead and see if the record gets updated. This will select the valid record as of today.

    Date currDate = systemDateGet();
    
    select forupdate validtimestate(currDate) * from dirPersonName
        where dirPersonName.Person == dirPerson.RecId;

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,217 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,978 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans