Hopefully a quick one on transactions.
I’m creating a new object with unitOfWork.create. I want the object created to be used in two setRelation, one as the parent and one as the child.
The first setRelation using the code below works fine (with the created object as the parent).
The second I’m having some trouble with, I appreciate that when setting the children the setRelation is expecting a collection and not a single object. I’ve tried to get around this by attempting to get the objectIdReference using .resolveTo and then putting that in a String array as the children. Unfortunately this doesn’t work.
The second relation does work if I create the newTeamMember with bulkCreate, but then I have issues trying to get it to work with the first setRelation.
Please can you provide guidance on how to best do this in a transaction where the created object needs to be a parent and a child.
UnitOfWork unitOfWork = new UnitOfWork();
Team newTeamMember = new Team();
OpResult addnewTeamMemberResult = unitOfWork.create( newTeamMember );
OpResultValueReference objectIdReference = addnewTeamMemberResult.resolveTo( "objectId" );
unitOfWork.setRelation( addnewTeamMemberResult, "venue", new String[]{venue.getObjectId()} );
unitOfWork.setRelation(venueOwner, "team", new String[]{objectIdReference.getOpResult().getOpResultId()});
In last line of code you need reference not to the transaction operation id (OpResultId), but to the objectId, which is represented by the objectIdReference.
unitOfWork.setRelation(venueOwner, "team", new String[]{objectIdReference});
You can put the OpResult object there, but unfortunately the function requires that its a collection of children and not a single child so errors out. (If you create newTeamMember with bulkCreate and put that in the setRelation call it works fine)
Sorry for inconvenience, the internal ticket for this issue is BKNDLSS-23872 (not BKNDLSS-20876). Ticket status is “open” but our developer will look into it as soon as possible.