Support Topics Documentation Slack YouTube Blog

Getting an objectId form UnitOfWork save operation

We are currently using Transactions in our project to save a single item then set a relationship. Everything works as expected.

We have a new requirement to return the saved objectId of the newly saved object.

after referring to the documentation we have tried the following example let objectIdReference = createPersonResult.resolveTo(propName: "objectId"), however this seems to get reference to the object and not the objectId itself.

our question is there away to return the objectId and not the reference, if not can we use the reference in further api calls which are not in the same scope as the transaction

 let unitOfWork = UnitOfWork()
        let saveOperation = unitOfWork.create(tableName: "Tickets", objectToSave: objectToSave)
    
    _ = unitOfWork.addToRelation(parentResult: saveOperation, columnName: "venu", childrenObjectIds: childrenObjectIds)
    
    let objectIdRef = saveOperation.resolveTo(propName: "objectId") //return reference to the objectID and not the actual object id
    
    unitOfWork.execute(responseHandler: { (result) in
        //return objectID as String 
        completion(.success(result.isSuccess))
    }) { (fault) in
        completion(.failure(fault))
    }

above is a code sample.

Thank you

Hello @Omar_Darwish

have you tried to get the result of saveOperation inside the responseHandler ?

Regards, Vlad

Hey Vladimir,

Im not sure i follow you here, the responseHandler is for the unit of work and objectIdRef returns the same value weather inside the response handler or outside.

can you please share an example.

Hello @Omar_Darwish,

You can get the saved object id this way:

unitOfWork.execute(responseHandler: { uowResult in
    if let saveOperationId = saveOperation.opResultId,
        let results = uowResult.results,
        let savedObjectResult = results[saveOperationId],
        let savedObject = savedObjectResult.result as? [String : Any],
        let savedObjectId = savedObject["objectId"] as? String {
            print(savedObjectId)
    }
}, errorHandler: { fault in
        // handle fault
})

Regards,
Olha

Hey Olha,

Thank you for your usual support. Your always to the rescue :muscle: