Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

How to auto generate LocationId by x++?

(1) ShareShare
ReportReport
Posted on by 1,173
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:
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,596 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,173 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
    Martin Dráb 230,596 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;
        }
    }
     

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

Congratulations 2024 Spotlight Stars!

Kudos to all of our 2024 community stars!

Meet the Top 10 leaders for December!

Congratulations to our December super stars!

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,576 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,596 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans