Groovy 28 – Add Same Sales Team on Contact as the One of the primary Account
Before Update Trigger Example
In this post, I will show how to create a Before Update Trigger on the Contact object to pull the Sales Team of the Contact's Primary Account into the Contact's sales team.
Go to Application Composer, Common area, Contacts Server scripts, and create a new Before Update In Database Trigger.
Trigger Code
// Groovy Code Snippet if (PrimaryCustomerId == null && isAttributeChanged('PrimaryCustomerId')) { def vo1 = newView('SalesAccountVO') def vc1 = vo1.createViewCriteria() def vcr1 = vc1.createRow() def vci11 = vcr1.ensureCriteriaItem('PartyId') vci11.setOperator('=') vci11.setValue(PartyId) vc1.insertRow(vcr1) vo1.appendViewCriteria(vc1) vo1.executeQuery() def ContactResourceIterator = vo1.first()?.SalesAccountResource try { while (ContactResourceIterator.hasNext()) { def resourceRow = ContactResourceIterator.next() if (OwnerPartyId != resourceRow.ResourceId) { resourceRow.remove() } } } catch (Exception e) { println(e) } } else if (PrimaryCustomerId != null && isAttributeChanged('PrimaryCustomerId')) { def vo1 = newView('SalesAccountVO') def vc1 = vo1.createViewCriteria() def vcr1 = vc1.createRow() def vci11 = vcr1.ensureCriteriaItem('PartyId') vci11.setOperator('=') vci11.setValue(PartyId) vc1.insertRow(vcr1) vo1.appendViewCriteria(vc1) vo1.executeQuery() def ContactResourceIterator = vo1.first()?.SalesAccountResource try { while (ContactResourceIterator.hasNext()) { def resourceRow = ContactResourceIterator.next() if (OwnerPartyId != resourceRow.ResourceId) { resourceRow.remove() } } } catch (Exception e) { println(e) } def v_salesAccountId = vo1.first()?.SalesAccountId def vo = newView('SalesAccountVO') def vc = vo.createViewCriteria() def vcr = vc.createRow() def vci1 = vcr.ensureCriteriaItem('PartyId') vci1.setOperator('=') vci1.setValue(PrimaryCustomerId) vc.insertRow(vcr) vo.appendViewCriteria(vc) vo.executeQuery() try { def A_row = vo.first()?.SalesAccountResource while (A_row.hasNext()) { def T_row = A_row.next() if (OwnerPartyId != T_row.ResourceId) { def addTeam = ContactResourceIterator.createRow() addTeam.setAttribute('ResourceId', T_row.ResourceId) addTeam.setAttribute('SalesAccountId', v_salesAccountId) addTeam.setAttribute('AccessLevelCode', "200") addTeam.setAttribute('AssignmentTypeCode', 'MANUAL') addTeam.setAttribute('LockAssignmentFlag', 'Y') ContactResourceIterator.insertRow(addTeam) } } } catch (Exception e) { println(e) } }
Explanation
The script has two parts:
- First, it checks if the Contact has a primary account associated with it. If it doesn't, it will clear the Contact's sales team and leave only the owner. If it does, it will add to the sales team of the Contact all the resources that are in the primary account team.
- The script uses the views SalesAccountVO and SalesAccountResource as both Contacts and Accounts are stored in these views. The difference between Contacts and Accounts is identified via their Party ID, which is a primary key in the HZ_PARTIES table.