-1

Right now I am using the below trigger to forward an Opportunity via Salesforce2Salesforce. Is it possible to transfer related account along with it?

Trigger Description: Whenever Department is set to "US" then the Opp automatically gets forwarded to Connection

Trigger AutoforwardOpp on Opportunity(after insert)
{
    String UserName = UserInfo.getName();
    String orgName = UserInfo.getOrganizationName();
    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;

        for(PartnerNetworkConnection network : connMap)
        {

            String cid = network.Id;
            String status = network.ConnectionStatus;
            String connName = network.ConnectionName;

            if(opp.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);
                insert newrecord;
            }
        }
    }
}
4
  • 1
    I am guessing you dimple add the code block for the account like you did the Opportunity.
    – Eric
    Commented Aug 19, 2017 at 0:23
  • The field doesn't exist on Account so i want to transfer opp when the field is set to "US" and transfer account when this opp is forwaded
    – Admin_BA
    Commented Aug 21, 2017 at 17:55
  • Why are you updating your question with the code provided as an answer? Please do not change your question from the original context.
    – Eric
    Commented Aug 22, 2017 at 19:33
  • Stop changing your bounty question. Your bounty is to get an answer that solve this specific issue not the everlasting gobstopper of questions. If you have a new question then start a new post
    – Eric
    Commented Aug 24, 2017 at 19:07

2 Answers 2

2
+50

I'm not sure if you've already tried this or not but it seems as if you should be able to share the Account in the same way you shared your Oppty. I moved your DML outside of your for-loops and added those records into a list so the code is bulkified.

    Trigger AutoforwardOpp on Opportunity(after insert)
{
    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.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;
    }
}
6
  • I tried your code and it throws error "Method does not exist or incorrect signature: void add(PartnerNetworkRecordConnection) from the type List<PartnerNetworkConnection> at line 39 column 32" also how do forward contacts related to the account?
    – Admin_BA
    Commented Aug 22, 2017 at 19:28
  • Is there any way to bring contacts with related accounts?
    – Admin_BA
    Commented Aug 23, 2017 at 19:52
  • Try the updated code, I believe it should work for you. Commented Aug 24, 2017 at 18:35
  • Thanks, i will try that, would that also bring contacts? i updated my post above
    – Admin_BA
    Commented Aug 24, 2017 at 18:56
  • For the Contacts, you'd probably want to restructure the code above. If you created another question and ping me on it, we could update the code to better fit your request. Commented Aug 24, 2017 at 21:28
2

As I said, simply add the code to do the account:

if(opp.Department__c == 'US')
            {
                //Send Account
                PartnerNetworkRecordConnection newAccrecord = new PartnerNetworkRecordConnection();
                newAccrecord.ConnectionId = cid;
                newAccrecord.LocalRecordId = opp.AccountId;
                newAccrecord.SendClosedTasks = true;
                newAccrecord.SendOpenTasks = true;
                newAccrecord.SendEmails = true;
                System.debug('Inserting New Account Record'+ newAccrecord);
                insert newAccrecord;

                PartnerNetworkRecordConnection newOpprecord = new PartnerNetworkRecordConnection();
                newOpprecord.ConnectionId = cid;
                newOpprecord.LocalRecordId = oId;
                newOppRecord.ParentRecordId = opp.AccountId OR newAccrecord; //not sure which
                newOpprecord.SendClosedTasks = true;
                newOpprecord.SendOpenTasks = true;
                newOpprecord.SendEmails = true;
                System.debug('Inserting New Opp Record'+ newOpprecord);
                insert newOpprecord;


            }

I am no S2S wizard so maybe since this seems so simple what I am saying will not work but from the code it looks like it should

9
  • your code works fine but does this support for bulk records? i see the account gets forwarded with Opportunity but contacts are not coming through
    – Admin_BA
    Commented Aug 22, 2017 at 19:30
  • @Admin_BA - Well thats not what you asked for. Your code as you have it in your question does not support bulk operations either. If you want to know how to bulkily the code that would be a different question right?
    – Eric
    Commented Aug 22, 2017 at 19:32
  • that's right, i can look into bulk records, any idea on how to transfer the related contacts and your code transfers opportunity and related account but account lookup field on opportunity comes blank, shouldn't it be populated with the account that got forwarded?
    – Admin_BA
    Commented Aug 22, 2017 at 20:22
  • @Admin_BA - The AccountId will only be blank if the Opportunity is not associated with an account. Without more information it is impossible to say how you would find the account if the AccountId is indeed blank.
    – Eric
    Commented Aug 22, 2017 at 20:23
  • Opportunities that i forward from instance always has an account related to it since it's mandatory field when the opportunity comes via connection through trigger it brings the related account as per your code but account name field stays blank
    – Admin_BA
    Commented Aug 22, 2017 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .