In this post I will show how to make a Before Update Trigger on contact object to pull the Sales Team of the Contact Primary Account into the contacts sales team.
So go to Application Composer , Common area, Contacts Sever scripts and create a new Before Update In database Trigger.
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)
}
}