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.

The code is the one bellow:

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)    
}    
}  
 

The script has 2 parts.

First it checks if the Contact has a primary accountant associated with it and if it doe snot have will clear the contact sales team and leave only the owner, if it has then it will add to the sales team of the contact all the resources that are in the primary account team.

The script utilize  only the view of SalesAccountVO and the view of SalesAccountResource as both Contact and Accounts are stored into this views and the difference between contact and accounts is done via their party id, which is a primary key of HZ_PARTIES table.

 

Leave a Reply

Your email address will not be published. Required fields are marked *