External Objects

All About Salesforce Extarnal Objects:

If You looking for any theoretical knowledge, then I suggest you to go reffer to this link.
https://www.mstsolutions.com/technical/salesforce-external-object-relationship-indirect-lookup/

Some Screen Shots for understanding .

1.

2.

3.

4.

5.

What is Indirect Lookup ?
It links a child External object to a parent Standard or Custom object. External child object’s indirect lookup relationship field will be mapped to custom unique + external Id on parent object.

Now let us discuss in detail about indirect lookup relationship configuration with an example.

Below example shows an Payments (External Child Object) associated with Account (Standard Salesforce Parent Object).

Before creating relationship with those two above objects, it is mandatory to create/specify the external data source and external object as shown in the above screenshot .

// Querying the External Object Data 
List < Prices__x > pricesX = ExternalObjectQuery.records(
[Select Account_external_Id__c, Product_code__c, Gross_price_without_taxes__c,
Net_unit_price__c, Net_discount__c, Start_Date_Of_Price__c,
End_date_of_price__c, Unit_special_discount_per_kg__c
FROM Prices__x
WHERE Account_external_Id__c =: accountExternalId AND
Start_Date_Of_Price__c =: startDateOfPrice
ORDER BY Start_Date_Of_Price__c DESC
]);

The most difficult thing i face in test class is the code coverage , Lets see how can we write the Test class for ExternalObject

// Description: Class created for creating mock records for External Objects
public virtual inherited sharing class ExternalObjectQuery
{
   public static List<SObject> records(List<SObject> records)
   {
      return instance.passThrough(records);
   }

   static ExternalObjectQuery instance = new ExternalObjectQuery();
   @TestVisible static void setMock(ExternalObjectQuery mock) { instance = mock; }

   protected virtual List<SObject> passThrough(List<SObject> records)
   {
      return records;
   }
}
@isTest
public class CreatePriceUpdateRequestItemCtrlTest {
    //Overriding the passThrough method from ExternalObjectQuery
    class Mock extends ExternalObjectQuery {
        final List < Prices__x > externalRecords;
        Mock(List < Prices__x > externalRecords) {
            this.externalRecords = externalRecords;
        }

protected override List < SObject > passThrough(List < SObject > records) {
            return externalRecords;
        }
    }
    @TestSetup static void makeData() {
    ....
    ....
    ....
}
@isTest
    static void PURIForPinnacleTest() {
    ....
    ....
    ....
List < Prices__x > PricesxList = new List < Prices__x > ();
        Prices__x px = new Prices__x();
        px.Account_external_Id__c = 'EXP001.IT_LG.SAP';
        px.Product_code__c = '000001';
        px.Product_name__c = 'BOMB.KG 10 V MIX';
        px.Gross_unit_price__c = 17.77;
        px.Net_unit_price__c = 17.77;
        px.Net_discount__c = 0.2;
        px.Unit_special_discount_per_kg__c = -0.07;
        px.Start_Date_Of_Price__c = System.Today();
        PricesxList.add(px);

        ExternalObjectQuery.setMock(new Mock(PricesxList));
        ExternalObjectQuery.records(PricesxList);
}
System.runAs(user1) {
    ....
    ....
    ....
}