4

Goal - Avoid using SeeAllData=true for testmethods against APEX code that inserts PartnerNetworkRecordConnection objects (to test outbound sharing)

What I believe to be true

  1. PartnerNetworkConnection object can not be mocked - DML and test.loadData() both fail
  2. PartnerNetworkConnection rows are available for query even in a testmethod marked simply as @IsTest
  3. PartnerNetworkRecordConnection rows can be inserted in an @IsTest testmethod. However, the inserted rows have to be assigned to a PartnerNetworkConnection with connectionStatus of Active.

OK, not great but as we're only relying on a real active Connection but otherwise we've isolated test data.

Now, here is where I am stumped

  1. If you insert a PartnerNetworkRecordConnection (PNRC) through code (such as an Account), it will be assigned by SFDC a status of 'Invite'.
  2. If you then try and insert a PartnerNetworkRecordConnection for, say, an Opportunity, where the parentRecordId is the Account you successfully inserted in #1 above, the insert will fail with INVALID_PARTNER_NETWORK_STATUS. This is because the PNRC for the Account hasn't been moved to status = 'Sent' by SFDC.

So, how to mock the PNRC status to 'Sent' for the parent (e.g. Account) so the PNRC insert of the Opportunity succeeds?

Here is what I tried, all of which failed:

  • Test.loadData(...) // internal Salesforce error
  • DML update on PNRC // SObject is not updateable
  • Placing Test.stopTest() after the PNRC insert of the Account, signalling SFDC to do "async-y stuff" like it does for future and batches. // INVALID_PARTNER_NETWORK_STATUS on the PNRC insert for the Opportunity
  • Json.deserialize(..) into a PNRC object // doesn't help as the PNRC DML insert validates against a real database record, not an in-memory sObject

Research

  • S2S Best Practices - relies on already shared objects to use in testmethods; however, I believe this was written before Test Data Isolation was a feature
  • SFSE post - with similar conclusions but 12 months old
  • Numerous SFDC Developer forum questions from 2012 and earlier; all with same issue

Unsatisfactory options:

Option 1 - Give up my aversion to SeeAllData and hence, manage in every sandbox + PROD a known parent Account successfully shared on a known PartnerNetworkConnection via Custom Settings

Option2 - Replace in my PROD code the line that does the Database.insert(..) on the PNRC with a method call to an interface which in PROD context does the DML but in testmethod context creates a local object that is verified by testmethod asserts (sort of a shadow database). But I'm not really testing the DML to verify it can actually work in the test.

Option 3 - Get my code coverage as high as possible with as many asserts as possible; make my Apex code be loggable and fault tolerant (not addError a transaction just because of an S2S error).

I'm currently doing Option 3 but I'd love to hear if one of the SFSE wizards have solved this issue.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.