pythonsharepointsalesforce-service-cloud

Compare data list in python


I try to compare two lists in Python. The first list from a Salesforce Call and the other one from Sharepoint List using Shareplum.

The list from Salesforce is formatted like this:

    [OrderedDict([('attributes',  OrderedDict([('type', 'FlashS__c'),  ('url', '/services/data/v42.0/sobjects/FlashSale__c/123456')])),  ('Id', '123456'),  ('FlashSaleOpeningDate__c', '2021-10-13T00:00:00.000+0000'),  ('Endofflashsaledate__c', '2021-10-19T21:59:00.000+0000'),  ('ID__c', '350601FR'),  ('Is_Extended__c', False),  ('End_date_after_extension__c', None),  ('FS_Extension__c', None),  ('Stage__c', '5-Planned'),  ('BUGlobal__c', 'fr_FR')])]

[OrderedDict([('attributes',  OrderedDict([('type', 'FlashS__c'),  ('url', '/services/data/v42.0/sobjects/FlashSale__c/896587')])),  ('Id', '896587'),  ('FlashSaleOpeningDate__c', '2021-10-13T00:00:00.000+0000'),  ('Endofflashsaledate__c', '2021-10-19T21:59:00.000+0000'),  ('ID__c', '350601FR'),  ('Is_Extended__c', False),  ('End_date_after_extension__c', None),  ('FS_Extension__c', None),  ('Stage__c', '5-Planned'),  ('BUGlobal__c', 'fr_FR')])]

The list from Sharepoint is formatted like this:

[{
'Title': 'en_En - Mon titre - Test - 2022-06-06',
 'Opening Date': datetime.datetime(2022, 6, 6, 2, 0), 
 'Close Date': datetime.datetime(2022, 6, 12, 23, 59), 
 'Country': 'Switzerland', 
 'City': 'Davos', 
 'BU': 'en_En', 
 'ProductType': 'Maj', 
 'MHMT': 'No', 
  'SalesforceID': '123456', 
  'FS Stage': '5-Planned'
  }
{
'Title': 'en_En - Mon titre 2 - Test 2 - 2022-06-06',
 'Opening Date': datetime.datetime(2022, 6, 6, 2, 0), 
 'Close Date': datetime.datetime(2022, 6, 12, 23, 59), 
 'Country': 'Switzerland', 
 'City': 'Davos', 
 'BU': 'en_En', 
 'ProductType': 'Maj', 
 'MHMT': 'No', 
  'SalesforceID': '456789', 
  'FS Stage': '5-Planned'
  }

]

I would like to loop and find if a line match or not in the other list. I would like to register the result in a new list like this:

SalesForce Sharepoint
123456     123456
896587     KO
KO         456789

I have try to do this:

list_result = []

for i in range(len(sf_api_data['records'])):
    for j in range(len(shp_api_data)):
        if sf_api_data['records'][i]['Id'] == shp_api_data[j]['SalesforceID']:
            if sf_api_data['records'][i]['Id'] not in list_result:
                list_result[i]['Salesforce'] = sf_api_data['records'][i]['Id']
                list_result[i]['Sharepoint'] = shp_api_data[j]['SalesforceID']

print(list_result)

sf_api_data['records'] is the Salesforce list

shp_api_data is the Sharepoint list

Have you got an idea to do that ?

thanks


Solution

  • Build sets of the two IDs:

    sf_ids = {r['SalesforceID'] for r in sf_api_data['records']}
    shp_ids = {r['Id'] for r in shp_api_data}
    

    and then you can take their intersection and difference:

    print("SalesForce Sharepoint")
    for i in sf_ids & shp_ids:
        print(f"{i:<10} {i}")
    for i in sf_ids - shp_ids:
        print(f"{i:<10} KO")
    for i in shp_ids - sf_ids:
        print(f"KO         {i}")