MasterDetail behavior how to return details entity id on save #6111
reach2rv
started this conversation in
Tricks and Samples
Replies: 3 comments 2 replies
-
@VictorTomaili @volkanceylan can you please suggest anything on this |
Beta Was this translation helpful? Give feedback.
1 reply
-
Have resolved this by amending Master detail behavior. If anyone needed I can post code of the behavior. Thanks |
Beta Was this translation helpful? Give feedback.
1 reply
-
Following are the changes I have made in specific methods of MasterDetailRelation Behavior Added to the Class private SaveResponse saveResponse; Amended private void SaveDetail(IUnitOfWork uow, Row detail, object masterId, object detailId)
{
detail = detail.Clone();
foreignKeyField.AsObject(detail, masterId);
if (!ReferenceEquals(null, filterField))
filterField.AsObject(detail, filterValue);
((Field)((IIdRow)detail).IdField).AsObject(detail, detailId);
var saveHandler = DefaultHandlerFactory.SaveHandlerFor(rowType);
var saveRequest = DefaultHandlerFactory.SaveRequestFor(rowType);
// Lines added to get EntityId for Details
saveRequest.Entity = detail;
this.saveResponse = new SaveResponse();
this.saveResponse = saveHandler.Process(uow, saveRequest, detailId == null ? SaveRequestType.Create : SaveRequestType.Update);
}
public override void OnAfterSave(ISaveRequestHandler handler)
{
var newList = Target.AsObject(handler.Row) as IList;
if (newList == null)
return;
var masterId = masterKeyField.AsObject(handler.Row);
if (handler.IsCreate)
{
foreach (Row entity in newList) {
SaveDetail(handler.UnitOfWork, entity, masterId, null);
// Get ID from Save response and assign it to field
var id = this.saveResponse.EntityId;
var itemid = (Int64Field)entity.FindField("TransItemId");
itemid[entity] = Convert.ToInt64(id);
var transid = (Int64Field)entity.FindField("TransId");
transid[entity] =Convert.ToInt64(masterId);
}
return;
}
var oldList = new List<Row>();
if (!attr.CheckChangesOnUpdate)
{
var row = rowFactory();
var rowIdField = (row as IIdRow).IdField;
// if we're not gonna compare old rows with new ones
// no need to call list request handler
new SqlQuery()
.Dialect(handler.Connection.GetDialect())
.From(row)
.Select((Field)rowIdField)
.Where(
foreignKeyField == new ValueCriteria(masterKeyField.AsSqlValue(handler.Row)) &
queryCriteria)
.ForEach(handler.Connection, () =>
{
oldList.Add(row.Clone());
});
}
else
{
var listHandler = DefaultHandlerFactory.ListHandlerFor(rowType);
var listRequest = DefaultHandlerFactory.ListRequestFor(rowType);
listRequest.ColumnSelection = ColumnSelection.List;
listRequest.Criteria = foreignKeyCriteria == new ValueCriteria(masterKeyField.AsObject(handler.Row)) & filterCriteria;
var entities = listHandler.Process(handler.Connection, listRequest).Entities;
foreach (Row entity in entities)
oldList.Add(entity);
}
DetailListSave(handler.UnitOfWork, masterId, oldList, newList);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi @volkanceylan , @VictorTomaili
currently to process saved master detail entity I use following code to get details entity id and then post records to another table
how this can be done in masterdetail behavior to avoid this additional list request in aftersave.
Beta Was this translation helpful? Give feedback.
All reactions