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

(0) ShareShare
ReportReport
Posted on by 457
 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,247 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
    457 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,247 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
    457 on at
    Hi @Subra There is no validation for this at table and entity level.
  • Martin Dráb Profile Picture
    239,684 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
    137 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!

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 Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

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

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 681 Super User 2026 Season 1

#2
André Arnaud de Calavon Profile Picture

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

#3
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 579

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans