Search code examples
breezehottowel

Find id for specific entity after savechanges


I have a situation, where I am saving multiple types of entities during a single SaveChanges. In some cases, this save will include my "target entity", in some cases not. In those cases where the save does include a "target entity", i need to be able to trap the entity's id as returned from the server using saveChanges() saveResult.

I have been trying to figure out how to use the Breeze EntityType to see if my "target entity" in the the saveResult, but I keep on getting undefined in the approach below. Clearly I'm not understanding how to use this feature?

function trapTargetEntityId(saveResult) {
        saveResult.entities.forEach(function(entity) {
            if (entity.EntityType === 'targetEntity') {
                targetEntitId = entity.id;
            }
            return;
        });
     }

Solution

  • Not sure I understand. But if you are looking for a specific entity by key after the save

    // make sure that targetType is NOT null
    var targetType = myEntityManager.metadataStore.getEntityType("Customer");
    var targetId = 23452; // arbitrary id
    
    function trapTargetEntityId(saveResult) {
    
        saveResult.entities.forEach(function(entity) {
            // assumes that you have a data property called 'id' on the 'Customer' entityType
            if (entity.entityType === targetType && entity.id === targetId) {
               targetEntity = entity;
               // do something with 'targetEntity'
            }
    
        });
     }
    

    ... and be careful with your casing - in your example it should have been 'entity.entityType' not 'entity.EntityType'