1

Background: I threw together a quick script to pull AppAnalytics logs from my LMA programmatically via python. This part is working just fine. I am including my script here in case it helps someone and also for context:

import zeep
import urllib.request
import time
from datetime import datetime, timedelta

class PullLogFileFromSalesforce:
    session_id = ''
    enterprise_client = zeep.Client('salesforce_enterprise_wsdl.xml')  
    salesforce_enterprise_login_service = enterprise_client.create_service('{urn:enterprise.soap.sforce.com}SoapBinding', 'https://login.salesforce.com/services/Soap/c/46.0')
    salesforce_enterprise_normal_service = None

    def set_normal_service_session_id(self):
        try:
            response = self.salesforce_enterprise_login_service.login(username='######@#####.com', password='##########################')
            print(f'successfully logged in!\nresponse from salesforce:\n{response}')
            self.session_id = response["sessionId"]
            url_for_other_calls = response["serverUrl"]
            self.salesforce_enterprise_normal_service = self.enterprise_client.create_service('{urn:enterprise.soap.sforce.com}SoapBinding', url_for_other_calls)
        except Exception as err:
            print(f'Error: {err}')

    def create_app_analytics_query_request_last_24hours(self):
        try:
            app_analytics_query_request_type = self.enterprise_client.get_type('ns0:AppAnalyticsQueryRequest')
            last_hour_date_time = datetime.now() - timedelta(hours = 24)
            app_analytics_query_request = app_analytics_query_request_type(DataType='CustomObjectUsageLog', PackageIds='033500000008yUe', StartTime=last_hour_date_time.strftime('%Y-%m-%dT%H:%M:%S'))
            create_app_analytics_query_request_response = self.salesforce_enterprise_normal_service.create(_soapheaders={'SessionHeader':{'sessionId':self.session_id}}, sObjects=[app_analytics_query_request])
            print(f'successfully created An appAnalyticsQueryRequest!\nresponse from salesforce:\n{create_app_analytics_query_request_response}')
        except Exception as err:
            print(f'Error: {err}')

    def find_and_download_app_analytics(self):
        downloadUrl = ''
        number_of_tries = 0
        while (downloadUrl == '' or downloadUrl is None) and number_of_tries < 100:
            try:
                number_of_tries += 1
                query_response = self.salesforce_enterprise_normal_service.query(_soapheaders={'SessionHeader':{'sessionId':self.session_id}}, queryString='SELECT DownloadUrl FROM AppAnalyticsQueryRequest ORDER BY CreatedDate DESC NULLS FIRST LIMIT 1')
                print(f'Attempting to find the download link!\nresponse from salesforce:\n{query_response}')
                downloadUrl = query_response['body']['result']['records'][0]['DownloadUrl']
                time.sleep(30)
            except Exception as err:
                print(f'Error: {err}')

        print(f'download url is {downloadUrl}. Downloading to results.csv')
        urllib.request.urlretrieve(downloadUrl, 'results.csv')

pull_log_file_from_salesforce = PullLogFileFromSalesforce()
print('geting the session id!')
pull_log_file_from_salesforce.set_normal_service_session_id()
print('complete!')
print('\n\nsending the request for a log file')
pull_log_file_from_salesforce.create_app_analytics_query_request_last_24hours()
print('complete!')
print('\n\nWaiting for the file to be ready for downloading, then download it')
pull_log_file_from_salesforce.find_and_download_app_analytics()
print('complete: File downloaded!')

The trick is that I want to upload the resulting AppAnalytics CSV to another internal system for my company AND I want it to be human-readable. As such, I want to make sure that I enrich the log file before I upload it.

Specifically, I am looking to programmatically pull the list of subscriber org names from another Salesforce SOAP API call of some kind so that I can then use python to add a column with the org name to the CSV before I upload it to the other system. (the CSV from AppAnalytics DOES contain the org_id)

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_packagesubscriber.htm looks promising, but I haven't been able to figure out how to get that information via an API call yet, and I am hoping someone here could give me a push in the right direction.


Now for the actual question: Can I use Salesforce SOAP APIs to pull the org name for an org subscribing to my managed package? If so, how?

(Parallel posted to https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&id=9062I000000XlSzQAK )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.