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

Announcements

News and Announcements icon
Community site session details

Community site session details

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

Entity key behaving differently for two entities on duplicate record

(2) ShareShare
ReportReport
Posted on by 471
 Hi,
 
I am importing images from external system using DMF and using entities RetailMediaResourcesEntity , RetailMediaProductRelationEntity. With external system sending resource id in cases where the same record is sent twice for both entities  Media resource ignores it and media product media relation fails with error "The record already exists.Cannot create a record in Retail product media relationship (RetailMediaProductRelation)."
 
Ideally since both have resource id as key it should fail for both since all the fields sent second time for both entities were exactly same I am not sure why it did not fail for RetailMediaResourcesEntity
 
Thanks!
Categories:
I have the same question (0)
  • Suggested answer
    Subra Profile Picture
    1,676 on at
    Hi Dev,

    I hope there might some logic they have handled internally to skip or ignore the duplicate records and other entity it was not handled by internally.

    Did you checked the code for both the entity and debugging to idenify the problem.
     
    Thanks,
    Subra

    If this helped, please mark it as "Verified" for others facing the same issue
  • Dev Profile Picture
    471 on at
    Hi Subra,
     
    No I don't see any skip logic in the entity methods just this one to skip initvalue on media resource entity
     
    Also I use just two key values in my import file ProductNumber and ResourceId but in reality there are the following fields in key in RetailMediaProductRelationEntity . Since it does not throw error in mapping to use only 2 of them and I don't need other two fields in my import is that the reason why it fails in this entity . 
    The other confusion is why it doesn't fail for other entity when I import the exact same file again.
     
     
    Public void initializeEntityDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
    {
        super(_entityCtx, _dataSourceCtx);
    
        if (_dataSourceCtx.name() == dataEntityDataSourceStr(RetailMediaResourcesEntity, RetailMediaResource))
        {
            // We have to do this at this point so that mapEntityToDataSource can perform the correct mappings using the sub-type
            // otherwise the fields in the super-type are not mapped and saved properly.
    
            if (_dataSourceCtx.getDatabaseOperation() == DataEntityDatabaseOperation::Insert || this.InstanceRelationType == 0)
            {
                // Based upon the type (currently RetailMediaResource only has one - RetailImageResource
                // set so that the InstanceRelationType gets the proper value (e.g. 12876 instead of 5845 or 
                // tableNum(RetailImageResource) and tableNum(RetailMediaResource)).
                _dataSourceCtx.setBuffer(new DictTable(tableNum(RetailImageResource)).makeRecord());
                _dataSourceCtx.getBuffer().initValue();
                _dataSourceCtx.skipInitValue(true);
            }
        }
    }
     
  • Suggested answer
    Subra Profile Picture
    1,676 on at
    Hi @Dev

    Could you please check the validation as well between 2 data entities and check where the error is coming from in the second legal entity and check why those methods has not been callled in the first entity.
     
    Thanks,
    Subra

    If this helped, please mark it as "Verified" for others facing the same issue
     
  • Dev Profile Picture
    471 on at
    Hi @Subra There is no validation for this at table and entity level.
  • Martin Dráb Profile Picture
    240,000 Most Valuable Professional on at
    The expected behaviour is that the system finds the existing record and tries to update it. The fact that it tries to insert a new record suggests that the record can't be found based on the values you've provided.
    My guess is that it's caused by the fact that you're using empty values for some key fields. Check out if your existing entity records have empty values there; maybe one of the values was initialized to something else.
  • Suggested answer
    Shaluma Profile Picture
    369 on at
    Hi,

    Why RetailMediaProductRelationEntity fails:

    You're only providing 2 of the 4 key fields ProductNumber and  ResourceId. The other two CatalogId, InternalOrganizationPartyNumber default to empty/zero on first import. On second import they again default to empty/zero so the DB finds that exact composite key already exists and throws a duplicate key error instead of doing a find+skip or upsert.

    Overrides persistEntity to handle duplicate key during DMF import

    public boolean persistEntity(DataEntityRuntimeContext _entityCtx)
    {
    RetailMediaProductRelationEntity entityRecord;
    RetailMediaProductRelation          existing;

    if (_entityCtx.getOperation() == DataEntityDatabaseOperation::Insert)
    {
    entityRecord = _entityCtx.getEntityRecord() as RetailMediaProductRelationEntity;

    if (entityRecord)
    {
    /// your custom logic

    if (existing.RecId)
    {
    // Duplicate record found — skip to return success
    return true;
    }
    }
    }
    return super(_entityCtx);
    }
    Please find the screenshot for your ref.



    The relation includes both entities.

    If the issue still persists after following them, please raise a query, and we will be happy to support you further.

    Thanks,
    If you found it helpful, please consider marking it as verified â€” it may be useful for others in the future!

  • Martin Dráb Profile Picture
    240,000 Most Valuable Professional on at
    Shaluma, you're seems to be saying that DMF can't be used to update existing records, because the system will throw an exception if the record already exists. That's clearly not true. As I explained above, the fact that the record is being inserted instead of updated tells me that the record wasn't found (while you're saying the opposite). Could you elaborate your reasoning, please?
  • Shaluma Profile Picture
    369 on at
    Hi ,
    I stand corrected
  • Suggested answer
    Shaluma Profile Picture
    369 on at
    Hi,

    I have imported the two lines into RetailMediaProductRelationEntity, and after debugging, I found that a duplicate value was present in the staging table. I deleted the duplicate record from both the staging table and the entity in SSMS,After importing the two lines from the Excel file after that, it worked correctly.

    Please find the below screenshot for your ref.



    Thanks,
    If you found it helpful, please consider marking it as verified â€” it may be useful for others in the future!
  • Dev Profile Picture
    471 on at
    Thank you.
     
    I guess there is a key values that we don't use like catalog Id )which is always empty) so I excluded it from mapping as it was not mandatory and allowed me to remove it. I was actually looking for a way to skip the insert in this case if the resource id is same through code or some properties as @Shaluma suggested. Is there any way we can achieve this and is it not the correct approach according to you @Martin.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 428 Super User 2026 Season 1

#2
Subra Profile Picture

Subra 370

#3
Martin Dráb Profile Picture

Martin Dráb 283 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans