Groovy 40 – Create a Copy Button for Account Object

Create a copy button for account object that will  also copy the address, the resources from team and the contacts.

 

Create a Custom text field on Account object  – RaisePopup_c

Do not expose this field.

Next implement the below functions.


1 – Create a Account Object function 1


Function Name: getAssociatedContacts

Returns: List

No Parameter

Code:

def var_relationship = Relationship
def var_contact=[]
def var_info = []
while(var_relationship.hasNext()){
def row = var_relationship.next()
var_contact.add(row.getAttribute(‘ObjectId’))
}
def arrayLength = var_contact.size()
def x
for(def i =0; i< arrayLength; i++ )
{
if(var_contact[i] != null){
def var_contact_view = newView(‘PersonProfile’)
def criteria = newViewCriteria(var_contact_view)
def randCont = criteria.createRow()
def vctr = randCont.ensureCriteriaItem(‘PartyId’)
vctr.setOperator(‘=’)
vctr.setValue(var_contact[i])
criteria.insertRow(randCont)
var_contact_view.appendViewCriteria(criteria)
var_contact_view.executeQuery()
while(var_contact_view.hasNext()){
def row2 = var_contact_view.next()
var_info.add(row2.PartyId)
}
}
}
return var_info


2 – Create a Account Object function 2


Function Name: raisePopupFunction

Returns: void

No Parameter

Code:

def ran = new Random();
setAttribute(‘RaisePopup_c’,ran.nextInt())


3 – Create Action Button with the code below


//Account  
//copyAccount  
def tm = now()
def nme = nvl(OrganizationName,)+‘-‘+tm
def locid
def locvo
def newloc
// Create Address  
if(PartySite.getEstimatedRowCount() != 0){
locvo = newView(‘Location’)
newloc = locvo?.createRow()
newloc.setAttribute(‘CreatedByModule’,‘ZCM’)
newloc.setAttribute(‘Country’,PrimaryAddressCountry)
newloc.setAttribute(‘City’,PrimaryAddressCity)
newloc.setAttribute(‘State’,PrimaryAddressState)
newloc.setAttribute(‘PostalCode’,PrimaryAddressPostalCode)
newloc.setAttribute(‘Address1’,PrimaryAddressLine1)
newloc.setAttribute(‘Address2’,PrimaryAddressLine2)
locvo.insertRow(newloc)
locid = newloc.getAttribute(‘LocationId’)
}
// Create OrganizationParty  
def opvo = newView(‘OrganizationParty’)
def newop = opvo?.createRow()
newop.setAttribute(‘CreatedByModule’,‘ZCM’)
opvo.insertRow(newop)
// Create PartySite for OrganizationParty  
def newPtySiteColl
def newPtySite
if(PartySite.getEstimatedRowCount() != 0){
newPtySiteColl = newop.PartySite
newPtySite = newPtySiteColl?.createRow()
newPtySite.setAttribute(‘CreatedByModule’,‘ZCM’)
newPtySite.setAttribute(‘LocationId’,locid)
newPtySiteColl.insertRow(newPtySite)
}
// Create PartySiteUse for PartySite  
def newPtySiteUseColl
def newPtySiteUse
if(PartySite.getEstimatedRowCount() != 0){
newPtySiteUseColl = newPtySite.PartySiteUse
newPtySiteUse = newPtySiteUseColl?.createRow()
newPtySiteUse.setAttribute(‘CreatedByModule’,‘ZCM’)
newPtySiteUse.setAttribute(‘SiteUseType’,‘SELL_TO’)
newPtySiteUseColl.insertRow(newPtySiteUse)
}
// Create OrganizationProfile for OrganizationParty  
def vo = newop.OrganizationProfile
def newAcct = vo?.createRow()
newAcct.setAttribute(‘OrganizationName’,nme)
newAcct.setAttribute(‘CreatedByModule’,‘ZCM’)
vo.insertRow(newAcct)
// Create PartySite for PartyUsageAssignment  
def newPSUColl = newop.PartyUsageAssignment
def newPSU = newPSUColl?.createRow()
newPSU.setAttribute(‘CreatedByModule’,‘ZCM’)
newPSU.setAttribute(‘PartyUsageCode’,‘SALES_ACCOUNT’)
newPSUColl.insertRow(newPSU)
def allContacts = [];
allContacts = getAssociatedContacts();
def primaryContact = PreferredContactPersonId;
//Add the Contacts  
if(allContacts.size() != 0){
for(int i = 0 ; i < allContacts.size(); i++)
{
def relationshipcollection = newop.Relationship;
def relationshipRow = relationshipcollection.createRow();
relationshipRow.setAttribute(‘SubjectType’,‘PERSON’);
relationshipRow.setAttribute(‘SubjectTableName’,‘HZ_PARTIES’);
relationshipRow.setAttribute(‘ObjectType’,‘ORGANIZATION’);
relationshipRow.setAttribute(‘ObjectTableName’,‘HZ_PARTIES’);
relationshipRow.setAttribute(‘SubjectId’,allContacts[i]);
relationshipRow.setAttribute(‘ObjectId’,newop.PartyId);
relationshipRow.setAttribute(‘CreatedByModule’,‘ZCM’);
relationshipRow.setAttribute(‘RelationshipCode’,‘CONTACT’);
relationshipRow.setAttribute(‘RelationshipType’,‘CONTACT’);
relationshipRow.setAttribute(‘PartyUsageCodeForObject’,‘CUSTOMER_CONTACT’);
relationshipRow.setAttribute(‘Role’,‘CONTACTORG’);
relationshipcollection.insertRow(relationshipRow);
if(PreferredContactPersonId == allContacts[i])
{
newop.setAttribute(‘PreferredContactPersonId’,allContacts[i])
}
}
}
//CreateSalesAccount  
def salesAccountObject = newView(‘SalesAccountVO’);
def salesAccountParty = salesAccountObject.createRow();
salesAccountParty.setAttribute(‘PartyId’,newop .PartyId);
salesAccountParty.setAttribute(‘ExistingFlag’,‘N’);
salesAccountParty.setAttribute(‘NamedAccountFlag’,‘N’);
salesAccountParty.setAttribute(‘AssgnExceptionFlag’,‘N’);
salesAccountObject.insertRow(salesAccountParty);
//Add team members  
def teamIter = Team;
teamIter.reset();
while(teamIter.hasNext())
{
def teamRow = teamIter.next();
if(teamRow?.ResourceId != adf.util.getUserPartyId()){
def SalesAccountResourceIterator = salesAccountParty?.SalesAccountResource;
def addMemeber = SalesAccountResourceIterator.createRow()
addMemeber.setAttribute(‘SalesAccountId’,salesAccountParty.SalesAccountId)
addMemeber.setAttribute(‘ResourceId’,teamRow?.ResourceId)
addMemeber.setAttribute(‘AccessLevelCode’,200)
addMemeber.setAttribute(‘LockAssignmentFlag’,‘Y’)
addMemeber.setAttribute(‘AssignmentTypeCode’,‘MANUAL’)
SalesAccountResourceIterator.insertRow(addMemeber)
}
}
raisePopupFunction()


4 – Create Field Validation rule on RaisePopup_c


Error Message: Copy record created, please click save/save and close to keep the copy

Code:

adf.error.warn(null)

Leave a Reply

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