Question

Prevent saving a record using EntityEventsBeforeArgs

Dear community, 

How can I prevent saving a record in the DB if it fails to meet certain conditions?

Here is the outline of my logic. Any help to complete this would be much appreciated!

[EntityEventListener(SchemaName = "Activity")]
public class ActivityEntityEventListener : BaseEntityEventListener
{
    public override void OnInserting(object sender, EntityAfterEventArgs e) {
        base.OnSaved(sender, e);
        var entity = (Entity) sender;
        var userConnection = entity.UserConnection;

// Logic to check if record passes certain criteria

if(criteria passed)
{
// Save record statement. Please help here
}

else
{
//throw error
//prevent saving the record
//please help here
}
    }
}

Thanks

Like 0

Like

1 comments

In case you don't want to save the record you would need to set IsCanceled of EntityBeforeEventArgs parameter to false, in case you want it to send you don't need to specify anything. Here is an example: 

    [EntityEventListener(SchemaName = "Activity")]

    public class Activity : BaseEntityEventListener 

    {

        public override void OnInserting(object sender, EntityBeforeEventArgs e) 

        {

            base.OnInserting(sender, e);

            var entity = (Entity) sender;

            var userConnection = entity.UserConnection;

            if (entity.GetTypedColumnValue<Guid>("ActivityCategoryId") == new Guid("2365ae4f-58e6-df11-971b-001d60e938c6"))

                e.IsCanceled =true;

        }

    }

    

Show all comments