web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

How to auto generate LocationId by x++?

(1) ShareShare
ReportReport
Posted on by 1,907
Hi,

How do i make sure LocationId is auto generated all the time. Because If i'm doing the import via DMF, there is an "auto generate" flag to acheive this. But how to do it by x++?
                DirPartyLocationPostalAddressV2Entity partyPostalAddressEntity;

                partyPostalAddressEntity.PartyNumber        =  _partyNumber;
                partyPostalAddressEntity.Street             = _req.Street();
                partyPostalAddressEntity.City               = _req.City();
                partyPostalAddressEntity.County             = _req.County();
                partyPostalAddressEntity.State              = _req.State();
                partyPostalAddressEntity.CountryRegionId    = _req.CountryRegionId();
                partyPostalAddressEntity.ZipCode            = _req.PostCode();
                partyPostalAddressEntity.Roles              = "Delivery"
                partyPostalAddressEntity.Description        = _req.LocationName();
                 
                partyPostalAddressEntity.insert();

it's because if i provide values that exactly matches another address  but the locationName is different, then remmitance method of the entity gets called and it returns the old address instead of inserting a new one. How can I make sure the address is always inserted?

Categories:
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    236,312 Most Valuable Professional on at
    How to auto generate LocationId by x++?
    I don't think you need to do anything; the logic is already there. Look into LogisticsPostalAddressBaseEntity.initializeEntityDataSource(); you'll see that if LocationId is empty, the call of LogisticsLocation.initValue() isn't skipped (and a LocationId is generated there) and then the value is assigned to LocationId of the entity.
     
    This is the code:
     
    if (_dataSourceCtx.name() == dataEntityDataSourceStr(LogisticsPostalAddressBaseEntity, LogisticsLocation))
    {
        if (this.LocationId)
        {
            _dataSourceCtx.skipInitValue(true);
        }
    }
    
    ...
    
    super(_entityCtx, _dataSourceCtx);
    
    if (_dataSourceCtx.name() == dataEntityDataSourceStr(LogisticsPostalAddressBaseEntity, LogisticsLocation))
    {
        if (this.LocationId == "")
        {
            LogisticsLocation location = _dataSourceCtx.getBuffer();
            this.LocationId = location.LocationId;
        }
    }
     
  • .. Profile Picture
    1,907 on at
    How to auto generate LocationId by x++?
    Hi Martin,

    LogisticsPostalAddressBaseEntity.initializeEntityDataSource(); is not called when calling DirPartyLocationPostalAddressV2Entity.Insert();

    And as I said in the question, below is the bahvoiur I'm getting, and that's why I need to do sth:
    it's because if i provide values that exactly matches another address  but the locationName is different, then remmitance method of the entity gets called and it returns the old address instead of inserting a new one. How can I make sure the address is always inserted?
    I can avoid this in DMF by ticking "auto generate" but not sure what do via x++
  • Suggested answer
    Martin Dráb Profile Picture
    236,312 Most Valuable Professional on at
    How to auto generate LocationId by x++?
    LogisticsPostalAddressBaseEntity.initializeEntityDataSource() is called and the value is assigned there. Maybe your attempt fails before it can reach the right code.
     
     
    We're using DirPartyLocationPostalAddressV2Entity in code, but LogisticsPostalAddressBaseEntity is its data source that LocationId field is bound to.
  • .. Profile Picture
    1,907 on at
    How to auto generate LocationId by x++?
    Hi Martin,

    How did you insert the address?

    What i did is that I took the details for an existing active address, , then I only changed the LocationName when inserting. Did you try this exact scenario?

    I assume it worked for you because maybe you updated other fields like street, city...etc

    ​​​​​​​Or what you can do is insert the address the first time, then insert it again but by changing the locationName
     
    I don't think my attempt fails because i get no errors and I put a break point at the beginning of the method, so if it got called i would have seen this.


    Anyways, I fixed this by adding this line
     
    partyPostalAddressEntity.LocationId = LogisticsLocation::getNewLocationId();
     
  • Martin Dráb Profile Picture
    236,312 Most Valuable Professional on at
    How to auto generate LocationId by x++?
    I didn't import any file - I used your code; I just changed the values and fixed the compilation error.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,157

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 883 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 674 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans