-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;
            }
        }
    }
}
  • 1
    I am guessing you dimple add the code block for the account like you did the Opportunity. – Eric Aug 19 '17 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 Aug 21 '17 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 Aug 22 '17 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 Aug 24 '17 at 19:07
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;
    }
}
|improve this answer|||||
  • 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 Aug 22 '17 at 19:28
  • Is there any way to bring contacts with related accounts? – Admin_BA Aug 23 '17 at 19:52
  • Try the updated code, I believe it should work for you. – sfdcJameson Aug 24 '17 at 18:35
  • Thanks, i will try that, would that also bring contacts? i updated my post above – Admin_BA Aug 24 '17 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. – sfdcJameson Aug 24 '17 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

|improve this answer|||||
  • 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 Aug 22 '17 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 Aug 22 '17 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 Aug 22 '17 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 Aug 22 '17 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 Aug 22 '17 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.