1

I came across Rakesh' Salesforce-to-Salesforce sharing through Flow & Process. But I am curious on how to get it down with Apex.

I was able to create this trigger and save it:

trigger AutoShareOpportunity on Opportunity (after insert) {

    List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>(
    [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection
        where ConnectionStatus = 'Accepted']
);
for(PartnerNetworkConnection network : connMap) {    
     PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

    newrecord.ConnectionId = network.Id;
    newrecord.LocalRecordId =OpportunityId;  
    newrecord.RelatedRecords = 'Contact,Opportunity,Orders__c';
    newrecord.SendClosedTasks = true;
    newrecord.SendOpenTasks = true;
    newrecord.SendEmails = true;   

    insert newrecord;   
}

But I am getting the following error:

Apex trigger AutoShareOpportunity caused an unexpected exception, contact your administrator: AutoShareOpportunity: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, Insufficient access on the entity.: []: Trigger.AutoShareOpportunity: line 19, column 1

1
  • You need not perform DML operation in for loop.it will cause DML Exception. May 22, 2017 at 12:38

1 Answer 1

0

You can't share a record before it's been inserted and has an Id. Your line newrecord.LocalRecordId = tid; wouldn't appear to have an Id on before insert. From the Object Reference on PartnerNetworkConnection, note the following:

  • To share a record, use the following fields: LocalRecordID and ConnectionId
  • To share a child of a parent record, use the following fields: LocalRecordID, ConnectionId, and ParentRecordID
  • To share a child of a parent record and its child records, use the following fields: LocalRecordID, ConnectionId, ParentRecordID, and RelatedRecords

Here's some sample sharing code from the Object Reference that could be used within your trigger that once you modified it for AfterInsert, instead of BeforeInsert:

List<PartnerNetworkConnection)> connMap = new List<PartnerNetworkConnection>(
    [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection
        where ConnectionStatus = 'Accepted']
);
for(PartnerNetworkConnection network : connMap) {    
     PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

    newrecord.ConnectionId = network.Id;
    newrecord.LocalRecordId = accountId;  
    newrecord.RelatedRecords = 'Contact,Opportunity,Orders__c';
    newrecord.SendClosedTasks = true;
    newrecord.SendOpenTasks = true;
    newrecord.SendEmails = true;   

    insert newrecord;   
}
4
  • 2
    You could have taken the INSERT statement out of for loop.
    – Reshma
    May 22, 2017 at 4:50
  • @Reshma As stated in my answer, the code above was copied verbatim from the documentation. Removing insert wouldn't solve the issue he's having. The above is a query for loop and is properly bulkified. The reality is that the LocalRecordId is NULL because it hasn't yet been inserted. The cross-object reference for sharing can't be created without it.
    – crmprogdev
    May 22, 2017 at 13:00
  • @crmprogdev thank you! I updated the code like you said, but now I am getting an error: Error: Compile Error: Variable does not exist: OpportunityId at line 11 column 30
    – B Wayne
    May 22, 2017 at 15:49
  • OpportunityId will be the Id of the opportunity record in trigger.new. You'll need to wrap for(PartnerNetworkConnection network : connMap) { inside a for loop of for(Opportunity o:trigger.new){. You'll likely also need to query for all the related related records for the opportunities before entering that loop.
    – crmprogdev
    May 22, 2017 at 20:34

You must log in to answer this question.

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