You need to sign in to do that
Don't have an account?
Øyvind Borgersen 10
Sharing related contacts
Hi,
I have a challenge that I want to share the related contacts to the other accounts contact. This is in a community scenario where a community user should be able to select any of the related contacts to the account.
As per now they are only able to select the contacts which has a direct relationship and not on related contacts.
Does anyone have a apex sharing for this scenario?
I have a challenge that I want to share the related contacts to the other accounts contact. This is in a community scenario where a community user should be able to select any of the related contacts to the account.
As per now they are only able to select the contacts which has a direct relationship and not on related contacts.
Does anyone have a apex sharing for this scenario?
The code is rectified and verified it at my end and it works as expected. This should work fine for you without any errors. Also, since the records are shared using a Salesforce to Salesforce connection, they should be done when the DML operations successfully commit their values to the database or else it might result in inconsistency. Hence I have changed the trigger to fire on After Insert and After Update events.
Trigger AutoforwardOpp on Opportunity(after insert, after update)
{
List <PartnerNetworkRecordConnection> prncList;
List <PartnerNetworkConnection> connectionList;
Map <ID, PartnerNetworkConnection> connMap;
Map <ID, ID> oppIdvsAccountIdMap;
Map<ID, Account> accountWithContactMap;
ID cid;
String status;
String connName;
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
connectionList = new List<PartnerNetworkConnection>();
prncList = new List<PartnerNetworkRecordConnection>();
//This would ideally return multiple active Connections if they exist hence it is best you use a
//criteria to ensure only the appropriate connection record is returned.
connMap = new Map<ID, PartnerNetworkConnection>(
[Select ID, ConnectionStatus, ConnectionName
From PartnerNetworkConnection
Where ConnectionStatus = 'Accepted']);
//get connection details
for(ID connId :connMap.keySet()){
cid = connMap.get(connId).ID;
status = connMap.get(connId).ConnectionStatus;
connName = connMap.get(connId).ConnectionName;
}
//Populate a map of Opp Ids and associated Account Ids
oppIdvsAccountIdMap = new Map<ID, ID>();
for(Opportunity oppRecord :Trigger.new){
if(oppRecord.Department__c == 'US'){
oppIdvsAccountIdMap.put(oppRecord.ID, oppRecord.Account.ID);
}
}
//Get associated Accounts and Contacts for every US opportunity if they exist
if(oppIdvsAccountIdMap.keySet().size() > 0){
accountWithContactMap = new Map<ID, Account>(
[Select ID, Name,
(Select ID, Name From Account.Contacts)
From Account
Where ID IN :oppIdvsAccountIdMap.values()]);
//Create PartnerNetworkRecordConnections for sharing the records
for(ID oppId : oppIdvsAccountIdMap.keySet()){
ID accountId = oppIdvsAccountIdMap.get(oppId);
//Share Opportunity
prncList.add(new PartnerNetworkRecordConnection(
ConnectionId = cid,
LocalRecordId = oppId,
SendClosedTasks = true,
SendOpenTasks = true,
SendEmails = true));
//Share Account
if(oppIdvsAccountIdMap.get(oppId) != null){
prncList.add(new PartnerNetworkRecordConnection(
ConnectionId = cid,
LocalRecordId = accountId,
SendClosedTasks = true,
SendOpenTasks = true,
SendEmails = true));
}
//Share associated Contacts
if(accountWithContactMap.get(accountId).Contacts != null){
for(Contact contact :accountWithContactMap.get(accountId).Contacts){
prncList.add(new PartnerNetworkRecordConnection(
ConnectionId = cid,
LocalRecordId = contact.ID,
SendClosedTasks = true,
SendOpenTasks = true,
SendEmails = true));
}
}
}//for
//Insert record conneections
if(!prncList.isEmpty()){
try{
insert prncList;
}
catch(System.Dmlexception dmlExceptionInstance){
System.debug('Record Share Error:' + dmlExceptionInstance.getMessage());
}
}
}//if
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
All Answers
The code is rectified and verified it at my end and it works as expected. This should work fine for you without any errors. Also, since the records are shared using a Salesforce to Salesforce connection, they should be done when the DML operations successfully commit their values to the database or else it might result in inconsistency. Hence I have changed the trigger to fire on After Insert and After Update events.
Trigger AutoforwardOpp on Opportunity(after insert, after update)
{
List <PartnerNetworkRecordConnection> prncList;
List <PartnerNetworkConnection> connectionList;
Map <ID, PartnerNetworkConnection> connMap;
Map <ID, ID> oppIdvsAccountIdMap;
Map<ID, Account> accountWithContactMap;
ID cid;
String status;
String connName;
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
connectionList = new List<PartnerNetworkConnection>();
prncList = new List<PartnerNetworkRecordConnection>();
//This would ideally return multiple active Connections if they exist hence it is best you use a
//criteria to ensure only the appropriate connection record is returned.
connMap = new Map<ID, PartnerNetworkConnection>(
[Select ID, ConnectionStatus, ConnectionName
From PartnerNetworkConnection
Where ConnectionStatus = 'Accepted']);
//get connection details
for(ID connId :connMap.keySet()){
cid = connMap.get(connId).ID;
status = connMap.get(connId).ConnectionStatus;
connName = connMap.get(connId).ConnectionName;
}
//Populate a map of Opp Ids and associated Account Ids
oppIdvsAccountIdMap = new Map<ID, ID>();
for(Opportunity oppRecord :Trigger.new){
if(oppRecord.Department__c == 'US'){
oppIdvsAccountIdMap.put(oppRecord.ID, oppRecord.Account.ID);
}
}
//Get associated Accounts and Contacts for every US opportunity if they exist
if(oppIdvsAccountIdMap.keySet().size() > 0){
accountWithContactMap = new Map<ID, Account>(
[Select ID, Name,
(Select ID, Name From Account.Contacts)
From Account
Where ID IN :oppIdvsAccountIdMap.values()]);
//Create PartnerNetworkRecordConnections for sharing the records
for(ID oppId : oppIdvsAccountIdMap.keySet()){
ID accountId = oppIdvsAccountIdMap.get(oppId);
//Share Opportunity
prncList.add(new PartnerNetworkRecordConnection(
ConnectionId = cid,
LocalRecordId = oppId,
SendClosedTasks = true,
SendOpenTasks = true,
SendEmails = true));
//Share Account
if(oppIdvsAccountIdMap.get(oppId) != null){
prncList.add(new PartnerNetworkRecordConnection(
ConnectionId = cid,
LocalRecordId = accountId,
SendClosedTasks = true,
SendOpenTasks = true,
SendEmails = true));
}
//Share associated Contacts
if(accountWithContactMap.get(accountId).Contacts != null){
for(Contact contact :accountWithContactMap.get(accountId).Contacts){
prncList.add(new PartnerNetworkRecordConnection(
ConnectionId = cid,
LocalRecordId = contact.ID,
SendClosedTasks = true,
SendOpenTasks = true,
SendEmails = true));
}
}
}//for
//Insert record conneections
if(!prncList.isEmpty()){
try{
insert prncList;
}
catch(System.Dmlexception dmlExceptionInstance){
System.debug('Record Share Error:' + dmlExceptionInstance.getMessage());
}
}
}//if
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha