function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
grahamjroygrahamjroy 

Can you automate record sharing in Salesforce to Salesforce with Apex Triggers?

Hi.


I have been working through this article which shows how to set up a Salesforce2Salesforce connection:

http://wiki.developerforce.com/index.php/An_Introduction_to_Salesforce_to_Salesforce

I have managed to set up the connection between the two installations but the code for the automation doesn't seem to work. 

What I want to be able to do is create an apex trigger on a custom object (myObject_c) which when created automatically puts the myObject_c and parent Account object across to the partner. My code looks like:

trigger shareWithPartner on myObject_c(after insert) {
PartnerNetworkRecordConnection newConnection =
                    new PartnerNetworkRecordConnection(
                        ConnectionId = networkId,
                        LocalRecordId = newContact.Id,
                        SendClosedTasks = false,
                        SendOpenTasks = false,
                        SendEmails = false,
                        ParentRecordId = myObject_c.AccountId);
insert newConnection;
}

but I get the error that networkId variable doesn't exist. Do I need to put in the connection id for the partner relationship in here? If I use the Id that is present in the url when I look at the connection:

https://na7.salesforce.com/p/pnetwork/Response/d?id=xyz123456789

in the code then I get the following errors:

                        ConnectionId = xyz123456789,
returns: Compile Error: expecting a right parentheses, found 'xyz123456789'

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You'll need to enquote the id:

 

 ConnectionId = 'xyz123456789',

 

All Answers

bob_buzzardbob_buzzard

You'll need to enquote the id:

 

 ConnectionId = 'xyz123456789',

 

This was selected as the best answer
grahamjroygrahamjroy

Hi Bob.

 

Thanks for your help. The problem I am now having is the LocalRecordId = newContact.Id I guess should be the id of the current object (myObject_c.id rather than the newContact.Id). Is there a way to grab the id of the current object?

 

I appreciate these are fairly n00b questions but I can't find a decent resource that teaches this stuff.

 

Regards,

 

Graham

bob_buzzardbob_buzzard

You can get at the id of the current object from the trigger.new list:

 

LocalRecordId = trigger.new[0].id,

 obviously this will only use the first element in the array, so if there's a bulk change you'll need to iterate the list of records passed to the trigger.

grahamjroygrahamjroy

Thanks again Bob.

 

One last question. There's a reference to ParentRecordId which isn't working. myObject.AccountId is returning 'variable does not exist'. The myObject has a Master-Detail relationship with Account. I have tried with the API name (myObject_c.AccountId) and the child relationship name (myObject.AccountId) but neither seems to work.

 

I want to make sure I pass the associated parent Account object across too when the child gets inserted. What's the correct syntax for this.

 

Really appreciate all your help on this.

 

Graham

bob_buzzardbob_buzzard

As far as I recall, if you set the parentrecordid, that object needs to have been shared already with the partner organisation, so this smacks that the account you are referring to isn't known to the other org.

grahamjroygrahamjroy

Hi Bob.

 

Really appreciate all of your help with this. I have the parent object already set up as shared in the connections tab. My question was about being able to retrieve the Id of this object ithin the Apex. Because my object is a child one, you can't set auto update flag to ensure any changes are sent across. This is inherited fromt he parent object (the account) so I need to be able to reference that in my code.

 

Basically I need to get the parent object Id for the local record and pass this where I have placed the ???

 

insert newConnection;

PartnerNetworkRecordConnection newConnection =
                    new PartnerNetworkRecordConnection(
                        ConnectionId = networkId,
                        LocalRecordId = newContact.Id,
                        SendClosedTasks = false,
                        SendOpenTasks = false,
                        SendEmails = false,
                        ParentRecordId = ???);
bob_buzzardbob_buzzard

The parent record id presumably is the account?  That being the case, you should be able to use:

 

  ParentRecordId = newContact.AccountId

 

grahamjroygrahamjroy


Hi Bob.

 

Thanks again for your help.

 

It's not a contact that I am sharing. It's a custom object called Solar_Install. Didn't really want to post exact code but it seems that I'll have to (it's only custom object).

 

My code therefore reads:

 

trigger shareWithPartner on Solar_Install__c (after insert) {

                    new PartnerNetworkRecordConnection(

                        ConnectionId = '01abcdefghi23',

                        LocalRecordId = trigger.new[0].id,

                        SendClosedTasks = false,

                        SendOpenTasks = false,

                        SendEmails = false,

                        ParentRecordId = trigger.new[0].AccountId);

insert newConnection;

}

 

but I get an error that 

 

Error: Compile Error: Invalid field AccountID for SObject Solar_Install__c at line 10 column 57

 

I have tried adding AccountId as a look up field within the Solar_Install object and also various combinations of trigger.new[0].Account_r.AccountID but to no avail.

 

Graham

bob_buzzardbob_buzzard

It will be something like Account__c for a custom object.  If you look at the custom object definition and locate the account field, it will show the API name.  That is the ID.

grahamjroygrahamjroy

Cheers Bob.

 

Incredibly grateful for all your help. Finally got it to work with:

 

 ParentRecordId = trigger.new[0].Account__c

 

v.happy :))

 

Graham

GeigerJHGeigerJH

Grahm,

 

How did the test class you created look when you deployed this to production?

 

I am trying to move some code similar to this to production and am a bit stumped on where I should go with the test class.

 

Thanks!

SFBethSFBeth

I've been trying to follow this to write my first Apex trigger, and am having trouble.  

 

I want to share two custom objects, Project_c and related Project_Contact_c, automatically to a partner when new records are created.  I've written these triggers:

 

PROJECT CONTACT:

 

trigger shareWithPartnerPC on Project_Contact__c (after insert) {
                    new PartnerNetworkRecordConnection(
                        ConnectionId = 'xxxxxxxxxx',
                        LocalRecordId = trigger.new[0].id,
                        SendClosedTasks = false,
                        SendOpenTasks = false,
                        SendEmails = false,
                        ParentRecordId = trigger.new[0].Project__c);
}

 

PROJECT

 

trigger shareWithPartnerP on Project__c (after insert) {
                    new PartnerNetworkRecordConnection(
                        ConnectionId = 'xxxxxxxxxxx',
                        LocalRecordId = trigger.new[0].id,
                        SendClosedTasks = false,
                        SendOpenTasks = false,
                        SendEmails = false);                       
}

 

But I have some questions (it's not working and I know there are other pieces of code I need to add):

 

1) Where/how do I define what shareWithPartnerP and shareWithPartnerPC is?


2) The code I was following, that grahamjroy so kindly posted, contained 'insert newConnection;' at the end of the of the code.  I wasn't able to save my trigger when I had that in there- what does that do and do I need it?


3) To echo GeigerJH, can anyone give any guidance on the test class for this?

 

4) Are there any other pieces of code that I need to write to get this to work?

 

5) I would also like to have Contact (standard), which is a lookup of Project Contact, share automatically when Project Contact is shared.  Is there a way to do that as well?

 

Thanks in advance for any help anyone can give!  Links to resources on Apex code/triggers in Salesforce would be much appreciated as well!  I've looked through some but have yet to find one that really lays out all of the steps.

GeigerJTGeigerJT

Here is the test class I wrote, hope this helps!

 

@isTest

private class TestS2SAutoShareOnOpptyAfterInsert {

          

static testMethod void myUnitTest() {

       

        List<Account> newAcct = new List<Account>();

       

        Account acct1 = new Account();

        acct1.name = 'This is a test Account';

        newAcct.add(acct1);

       

        Account acct2 = new Account();

        acct2.name = 'The Results Companies';

        newAcct.add(acct2);

       

        insert newAcct;

       

        List<Opportunity> newOpp = new List<Opportunity>();

       

        Opportunity opp1 = new Opportunity();

        opp1.name = 'This is a test Oppty';

        opp1.CloseDate = system.today();

        opp1.StageName = 'Dead';

        opp1.AccountId = acct1.id;

        opp1.Portfolio_Lookup__c = acct2.id;

        newOpp.add(opp1);

       

        insert newOpp; 

       

        List<PartnerNetworkConnection> partnerNetConList =

           [Select id from PartnerNetworkConnection where connectionStatus = 'Accepted'];

       

        opp1 = [select id, name, Portfolio_Lookup__c from Opportunity where name='This is a test Oppty'];  

       

        system.assertEquals(opp1.name, 'This is a test Oppty'); 

        system.assertEquals(opp1.Portfolio_Lookup__c, acct2.id);

    }

}

Sourabh Tayal 5Sourabh Tayal 5
Hi Bob and Grahm,

I am implementing the same Salesforce to Salesforce automate record sharing with Apex Trigger for contact object and its related and master records.
I have did all prerequiste setting for this.

My trigger is firing(after insert, after update) and not giving any error but contact record is not sharing with other org.

Help me regarding it or let me know if i have left any action.
I am mentioning my code here:


Trigger AutoforwardContact on Contact(after Insert,after update){   
    PartnerNetworkConnection pp =[select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection
                                                               where ConnectionStatus = 'Accepted' and connectionName = '360DC'];
    system.debug('ppppp' + pp);
    Id networkId = pp.id;     
    System.debug('>>>>>>networkId' + networkId);
       
        List<PartnerNetworkRecordConnection> connections =  new    List<PartnerNetworkRecordConnection>();
        for (Contact newTest: Trigger.new) {                          
                PartnerNetworkRecordConnection newConnection = new PartnerNetworkRecordConnection(
                      ConnectionId = networkId,
                      LocalRecordId = newTest.Id,
                      SendClosedTasks = false,
                      SendOpenTasks = false,
                      SendEmails = false,
                      ParentRecordId = newTest.AccountId);
                System.debug('>>>>>>newConnection' + newConnection);    
                connections.add(newConnection);
            }
        database.insert(connections);   
}

Manuall sharing is working but whenever i try for automate using trigger it is not working.
Boris FrankBoris Frank
Hello! I recommend reading this helpful article (https://skyvia.com/blog/salesforce-to-salesforce-integration), which talks about Salesforce to Salesforce automatic sharing.