This script will help you add as opportunity contacts all the opportunity account contacts.
In this way you can have on the opportunity same contacts as the Account linked to the opportunity has
Go To App Composer > Sales > Opportunity > Server Scripts > Create new Object Trigger Of Type Before Insert In database
Add This Code:
if(TargetPartyId != null ){
def accountContacts = [];
def OpportunityContacts = [];
def primaryAccountContact = Organization?.PreferredContactPersonId;
def relationship = Organization?.Relationship;
while(relationship.hasNext())
{
def resourceRow = relationship.next();
if (resourceRow.RelationshipType ==‘CONTACT’)
accountContacts.add(resourceRow.ObjectId);
}
//get Opportunity Contacts
def optyResource = OpportunityContact;
while(optyResource.hasNext()){
def row = optyResource.next();
OpportunityContacts.add(row.PartyId);
}
for(int i ; i <accountContacts.size();i++)
{
def x = 0;
for(String y : OpportunityContacts){
if(y.contains(accountContacts[i].toString()))
{
x++;
}
}
if(x == 0 ){
def var_insertRow = optyResource.createRow();
var_insertRow.setAttribute(‘PERPartyId’,accountContacts[i]);
if(primaryAccountContact != null && primaryAccountContact ==accountContacts[i]){
var_insertRow.setAttribute(‘PrimaryFlg’,‘Y’);
}
optyResource.insertRow(var_insertRow);
}
x = 0;
}
}
Expected Behavior
Trigger will execute each time an opportunity is created.
It will check if the opportunity has an account associated with it .
If it does then will collect all those account contacts and add them to the opportunity contacts and set as primary opportunity contact the same contact as the Account primary contact.
If opportunity create does not have an account then the script will do nothing.