asset warranty saves
This commit is contained in:
@ -26,43 +26,73 @@ def animate():
|
||||
time.sleep(0.1)
|
||||
sys.stdout.write('\rDone! ')
|
||||
|
||||
# ...existing code for animate() function...
|
||||
|
||||
def extract_data(object_id, query, output_path='output', context='qa2'):
|
||||
def extract_data(object_id, query, output_path='output', context='qa2', use_rest=False):
|
||||
"""
|
||||
Extract data using Bulk API and save as CSV
|
||||
Extract data using either REST or Bulk API and save as CSV
|
||||
|
||||
Args:
|
||||
object_id (str): Salesforce object ID
|
||||
output_path (str): Path to save the output file (default 'output')
|
||||
context (str): Context name for credentials (e.g., 'qa2', 'prod')
|
||||
query (str): SOQL query
|
||||
output_path (str): Path to save the output file
|
||||
context (str): Context name for credentials
|
||||
use_rest (bool): Whether to use REST API instead of Bulk API
|
||||
"""
|
||||
global done
|
||||
done = False
|
||||
t = None
|
||||
|
||||
try:
|
||||
global done
|
||||
done = False
|
||||
|
||||
# Get Salesforce connection using the new module
|
||||
# Get Salesforce connection
|
||||
sf = get_sf_connection(context)
|
||||
|
||||
# Start animation thread
|
||||
t = threading.Thread(target=animate)
|
||||
t.start()
|
||||
|
||||
results = sf.bulk2.__getattr__(object_id).query_all(
|
||||
query
|
||||
)
|
||||
print(f'Extracting: {object_id}')
|
||||
for i, data in enumerate(results):
|
||||
with open(f"results/{object_id}.csv", "w", encoding="utf-8") as bos:
|
||||
bos.write(data)
|
||||
|
||||
# Choose API method based on use_rest flag
|
||||
if use_rest:
|
||||
# Use REST API query
|
||||
results = sf.query(query)
|
||||
records = results['records']
|
||||
|
||||
# Handle pagination if needed
|
||||
while not results['done']:
|
||||
results = sf.query_more(results['nextRecordsUrl'], identifier_is_url=True)
|
||||
records.extend(results['records'])
|
||||
|
||||
# Convert records to CSV format
|
||||
if records:
|
||||
df = pd.DataFrame(records)
|
||||
df = df.drop('attributes', axis=1) # Remove Salesforce metadata
|
||||
output_file = f"results/{object_id}.csv"
|
||||
os.makedirs('results', exist_ok=True)
|
||||
df.to_csv(output_file, index=False)
|
||||
|
||||
else:
|
||||
# Use Bulk API query
|
||||
results = sf.bulk2.__getattr__(object_id).query(
|
||||
query, max_records=2000000
|
||||
)
|
||||
|
||||
# Write results to file
|
||||
output_file = f"results/{object_id}.csv"
|
||||
os.makedirs('results', exist_ok=True)
|
||||
for data in results:
|
||||
with open(output_file, "w", encoding="utf-8") as bos:
|
||||
bos.write(data)
|
||||
|
||||
time.sleep(10)
|
||||
done = True
|
||||
t.do_run = False
|
||||
return output_file
|
||||
|
||||
except Exception as e:
|
||||
done = True
|
||||
t.do_run = False
|
||||
raise ValueError(f'Error extracting data: {str(e)}')
|
||||
|
||||
finally:
|
||||
# Clean up thread and animation state
|
||||
done = True
|
||||
if t and t.is_alive():
|
||||
t.join(timeout=1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import argparse
|
||||
@ -92,23 +122,19 @@ if __name__ == '__main__':
|
||||
for query_def in queries:
|
||||
sobject = query_def['sobject']
|
||||
query_str = query_def['query'].replace('{country}', args.country)
|
||||
use_rest = query_def.get('useREST', False) # Default to False if not specified
|
||||
|
||||
# Skip if --sobjects is provided and current sobject isn't in the list
|
||||
if selected_sobjects and sobject not in selected_sobjects:
|
||||
print(f'Skipping {sobject} as it is not specified in --sobjects')
|
||||
continue
|
||||
|
||||
try:
|
||||
print(f'\nRunning query for {sobject}:')
|
||||
output_file = extract_data(
|
||||
object_id=sobject,
|
||||
query=query_str,
|
||||
output_path=args.output_path,
|
||||
context=args.context
|
||||
)
|
||||
print(f'File saved at: {output_file}')
|
||||
except Exception as e:
|
||||
print(f'Error running query for {sobject}: {str(e)}')
|
||||
# Optionally, you can choose to skip failed queries or exit here
|
||||
if not selected_sobjects or sobject in selected_sobjects:
|
||||
try:
|
||||
output_file = extract_data(
|
||||
object_id=sobject,
|
||||
query=query_str,
|
||||
output_path=args.output_path,
|
||||
context=args.context,
|
||||
use_rest=use_rest
|
||||
)
|
||||
print(f'Successfully extracted {sobject} to {output_file}')
|
||||
except Exception as e:
|
||||
print(f'Error extracting {sobject}: {str(e)}')
|
||||
|
||||
print('\nExtraction complete.')
|
@ -5,7 +5,7 @@
|
||||
"query": "SELECT Id, City__c, Country__c, GeoY__c, GeoX__c, PostalCode__c, Street__c, Extension__c, HouseNo__c, FlatNo__c, Floor__c FROM SCInstalledBaseLocation__c WHERE Country__c = '{country}' limit 3"
|
||||
},{
|
||||
"sobject": "SCInstalledBase__c",
|
||||
"query": "SELECT Id, Name, CommissioningDate__c,InstallationDate__c,ProductEnergy__c, ProductUnitClass__c,ArticleNo__c,SerialNo__c, SerialNoException__c, ProductUnitType__c, InstalledBaseLocation__c FROM SCInstalledBase__c WHERE Country__c = '{country}' limit 3"
|
||||
"query": "SELECT Id, Name, CommissioningDate__c,InstallationDate__c,ProductEnergy__c, ProductUnitClass__c,ArticleNo__c,SerialNo__c, SerialNoException__c, ProductUnitType__c, InstalledBaseLocation__c, WarrantyDuration__c, GuaranteeStandard__c, GuaranteeExtended__c FROM SCInstalledBase__c WHERE Country__c = '{country}' limit 3"
|
||||
},{
|
||||
"sobject": "Asset",
|
||||
"query": "SELECT Id, Serialnumber FROM Asset WHERE Location.ParentLocation.Name LIKE '%{country}'"
|
||||
@ -39,6 +39,10 @@
|
||||
},{
|
||||
"sobject": "SCContract__c",
|
||||
"query": "SELECT id, name, Template__c, status__c, Brand__r.Name, Country__c, Runtime__c, EndDate__c, StartDate__c, Account__c, AccountOwner__c, IoT_Registration_Status__c FROM SCContract__c WHERE Template__c != null AND EndDate__c >= TODAY AND Country__c = '{country}' limit 3"
|
||||
},{
|
||||
"sobject": "WarrantyTerm",
|
||||
"useREST": true,
|
||||
"query": "SELECT Id, WarrantyTermName, WarrantyDuration, WarrantyType, pricebook2.country__c FROM WarrantyTerm WHERE Pricebook2Id = null OR pricebook2.Country__c = '{country}'"
|
||||
}
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user