You need to sign in to do that
Don't have an account?
sam_Admin
Trigger to forward related contacts with account in s2s
Right now I am using the below trigger to forward an Opportunity via Salesforce2Salesforce. Is it possible to transfer related contacts along with account?
Trigger Description: Whenever Department is set to "US" then the Opp automatically gets forwarded to Connection
Trigger AutoforwardOpp on Opportunity(after insert, before update)
{
String UserName = UserInfo.getName();
String orgName = UserInfo.getOrganizationName();
List<PartnerNetworkConnection> connectionList = new List<PartnerNetworkConnection>();
List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>
([select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted']);
List<PartnerNetworkRecordConnection> prncList = new List<PartnerNetworkRecordConnection>();
for(Integer i =0; i< Trigger.size; i++)
{
Opportunity opp = Trigger.new[i];
String oId = opp.Id;
String accId = opp.AccountId;
for(PartnerNetworkConnection network : connMap)
{
String cid = network.Id;
String status = network.ConnectionStatus;
String connName = network.ConnectionName;
if(opp.Department__c == 'US')
{
PartnerNetworkRecordConnection newAccRecord = new PartnerNetworkRecordConnection();
newAccRecord.ConnectionId = cid;
newAccRecord.LocalRecordId = accId;
newAccRecord.SendClosedTasks = true;
newAccRecord.SendOpenTasks = true;
newAccRecord.SendEmails = true;
System.debug('Inserting New Record'+newAccrecord);
insert newAccrecord;
PartnerNetworkRecordConnection newOpprecord = new PartnerNetworkRecordConnection();
newOpprecord.ConnectionId = cid;
newOpprecord.LocalRecordId = oId;
newOpprecord.SendClosedTasks = true;
newOpprecord.SendOpenTasks = true;
newOpprecord.SendEmails = true;
System.debug('Inserting New Record'+newOpprecord);
insert newOpprecord;
}
}
}
}
Trigger Description: Whenever Department is set to "US" then the Opp automatically gets forwarded to Connection
Trigger AutoforwardOpp on Opportunity(after insert, before update)
{
String UserName = UserInfo.getName();
String orgName = UserInfo.getOrganizationName();
List<PartnerNetworkConnection> connectionList = new List<PartnerNetworkConnection>();
List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>
([select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted']);
List<PartnerNetworkRecordConnection> prncList = new List<PartnerNetworkRecordConnection>();
for(Integer i =0; i< Trigger.size; i++)
{
Opportunity opp = Trigger.new[i];
String oId = opp.Id;
String accId = opp.AccountId;
for(PartnerNetworkConnection network : connMap)
{
String cid = network.Id;
String status = network.ConnectionStatus;
String connName = network.ConnectionName;
if(opp.Department__c == 'US')
{
PartnerNetworkRecordConnection newAccRecord = new PartnerNetworkRecordConnection();
newAccRecord.ConnectionId = cid;
newAccRecord.LocalRecordId = accId;
newAccRecord.SendClosedTasks = true;
newAccRecord.SendOpenTasks = true;
newAccRecord.SendEmails = true;
System.debug('Inserting New Record'+newAccrecord);
insert newAccrecord;
PartnerNetworkRecordConnection newOpprecord = new PartnerNetworkRecordConnection();
newOpprecord.ConnectionId = cid;
newOpprecord.LocalRecordId = oId;
newOpprecord.SendClosedTasks = true;
newOpprecord.SendOpenTasks = true;
newOpprecord.SendEmails = true;
System.debug('Inserting New Record'+newOpprecord);
insert newOpprecord;
}
}
}
}
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 inconistency. Hence I have changed the trigger to fire on After Insert and After Update events.
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
All Answers
Refer the sample code below to do so. Moreover, you have insert statements written within a for loop which makes your code prone to hitting DML Limits. You would want to hold all the PartnerNetworkRecordConnection records within a List and would want to insert that list rather than doign inserts on an individual list. You may want to look at how to bulkify your Apex code (https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code) to avoid hitting the limits.
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
Illegal assignment from Schema.SObjectField to String at line 13 column 16
Trigger AutoforwardOpp on Opportunity(before insert, before update)
{
String UserName = UserInfo.getName();
String orgName = UserInfo.getOrganizationName();
List<PartnerNetworkRecordConnection> AcctConnectionList = new List<PartnerNetworkRecordConnection>();
List<PartnerNetworkRecordConnection> OpptyConnectionList = new List<PartnerNetworkRecordConnection>();
List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>
([select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted']);
List<PartnerNetworkRecordConnection> prncList = new List<PartnerNetworkRecordConnection>();
for(Integer i =0; i< Trigger.size; i++)
{
Opportunity opp = Trigger.new[i];
String oId = opp.Id;
String acctId = opp.AccountId;
for(PartnerNetworkConnection network : connMap)
{
String cid = network.Id;
String status = network.ConnectionStatus;
String connName = network.ConnectionName;
if(opp.FFCNSales_Department__c == 'US')
{
PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();
newrecord.ConnectionId = cid;
newrecord.LocalRecordId = oId;
newrecord.SendClosedTasks = true;
newrecord.SendOpenTasks = true;
newrecord.SendEmails = true;
System.debug('Inserting New Record'+newrecord);
OpptyConnectionList.add(newrecord);
PartnerNetworkRecordConnection newAcctRecord = new PartnerNetworkRecordConnection();
newAcctRecord.ConnectionId = cid;
newAcctRecord.LocalRecordId = acctId;
newAcctRecord.SendClosedTasks = true;
newAcctRecord.SendOpenTasks = true;
newAcctRecord.SendEmails = true;
System.debug('Inserting New Record'+newrecord);
AcctConnectionList.add(newAcctRecord);
}
}
}
if(AcctConnectionList.size()>0){
insert AcctConnectionList;
}
if(OpptyConnectionList.size()>0){
insert OpptyConnectionList;
}
}
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 inconistency. Hence I have changed the trigger to fire on After Insert and After Update events.
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
I changed it to before insert but still get same error, any idea why?
This has been fixed. Line # 37 was updated to as shown below. Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
You might want to try inserting the Account records first and I believe the Opportunity and Contact records should be shared auotmatically. Try and let me know.