Programmatically add direct assignment ADSAccountInADSGroup even as deferred operation

Hi all, Community

I am trying to programmatically set up some deferred operations in order to directly assign an ADSGroup to an AdsAccount. This should be done also for existing indirect assignments.

Usually, if I have to do it “runtime” (not deferred operation), I can execute from the script a sql query (mySQLExec.SqlExecuteNonQuery(“update ADSAccountInADSGroup set XOrigin = x where …. ”) ) but this cannot be done in a deferred operation block as it is executed immediately. In deferred operation block I need to base the updates I want to persist throughout the deferred operation to IsingleObject or Entity, but as I tried to update the field XOrign I keep on getting permission exceptions, no matter what I do.

So, can someone give me some ideas how to solve this problem? Maybe there is a better way than to directly querying the DB. Some method or else linked to ISingleDbObject or Entity …

Thank you in advance to anyone who can provide detail.

 

Alberto

  • Yes Markus,

    the delete behavior is pretty clear as I have a lot of experience in deleting assignments Blush

    On the contrary I was quite surprised that, in order to “increase” the XOrigin, it was enough to create an entity or an ISingleDbObject and save it, as in the last couple of years, every time I was asked to implement such feature, I had to deal with SqlExecutor. But this time, I couldn’t have it working because of the deferred operation.

    Anyway, I really look forward to read your suggestions

     

    A

  • So here is the correct solution. To create a direct assignment for an existing membership, you can call the AssignDirect method.

    I've updated my sample code (using your variables) to reflect this change.

    Public Sub CCC_AssignDirect(UID_ADSAccount As String, uid_group As String)
             ' Create a new DeferredBlock for a point in time.
             Using New VI.DB.DeferredOperations.DeferredBlock(Session, DateTime.UtcNow.AddDays(45), "Optional description for operation")
             ' Operations done here are done deferred
    
                      Using uow = Session.StartUnitOfWork()
                               Dim dbEntity As IEntity = Nothing
                               Dim dbKey As DbObjectKey = New DbObjectKey(Table.ADSAccountInADSGroup, New String() {UID_ADSAccount, uid_group})
                               ' Check if memberships already exists
    
                               If Session.Source().TryGet(dbKey, EntityLoadType.Default, dbEntity) Then
                                        'if exists call AssignDirect method
                                        dbEntity.CallMethod("AssignDirect")
                               Else
                                        'if not exists create new membershio
                                        dbEntity = Session.Source().CreateNew(Table.ADSAccountInADSGroup)
                                        dbEntity.PutValue(Table.ADSAccountInADSGroup.UID_ADSAccount, UID_ADSAccount)
                                        dbEntity.PutValue(Table.ADSAccountInADSGroup.UID_ADSGroup, uid_group)
                               End If
                               uow.Put(dbEntity)
                               uow.Commit()
                      End Using
             End Using
    End Sub

  • Markus,

    I tested your solution several times and it seems to be perfect.

    I didn’t know about the existence of the method “AssignDirect”, and of course it made the magic!

    Thank you again

    Alberto

     

    Just one more think, even if it is not so important. In this case, why to incapsulate the instruction in a using uow while it should be the same than calling entity.Save(Session)?

     

  • In this case (single operation), it is just my coding style. Using an entity.Save(Session) would be sufficient as well.