Access Parent-Child Objects
In this sample script, you're creating a child record of the Interaction object from the parent Opportunity object. Place this script in the 'Before Insert in Database' trigger event of the Opportunity object.
While creating parent-child records with composite associations, you must ensure that the parent record IDs are present in the child records at the time of creation since the child has no existence outside the context of the parent. You must add the parent record to the correct rowset. Only then, the child record will be able to identify the parent record to which it belongs.
def opptyUpdatedName = Name
def opptyOwnerId = OwnerResourcePartyId
def opptyAccountId = TargetPartyId
def voInteraction = newView('InteractionVO')
def createInt = voInteraction.createRow()
voInteraction.insertRow(createInt) // <-- This is shifted to a place before accessing the children.
def currentDateTime = now()
def interactionName = 'OptyNameModifed' + currentDateTime
createInt.setAttribute('InteractionDescription', interactionName)
createInt.setAttribute('InteractionStartDate', currentDateTime)
createInt.setAttribute('CustomerId',opptyAccountId)
def voIntAssociation = createInt.InteractionAssociation
def createIntAssc = voIntAssociation.createRow()
createIntAssc.setAttribute('AssociatedObjectUid',OptyId )
createIntAssc.setAttribute('AssociatedObjectCode','OPPORTUNITY')