103 lines
8.0 KiB
OpenEdge ABL
103 lines
8.0 KiB
OpenEdge ABL
/**
|
|
* ─────────────────────────────────────────────────────────────────────────────────────────────────┐
|
|
* This Batch class is use for upsert Shopify's Inventory into the Salesfroce
|
|
* Shopify API Documentation: https://shopify.dev/docs/admin-api/rest/reference
|
|
* Create Shopify Store: https://www.shopify.com/signup?itcat=identity&itterm=create-new-store
|
|
* Shopify API Release 2020: https://www.shopify.in/partners/blog/shopify-api-release-january-2020
|
|
* What is Person Account in Salesforce ? https://help.salesforce.com/articleView?id=account_person.htm&type=5
|
|
* ──────────────────────────────────────────────────────────────────────────────────────────────────
|
|
* @author Sumit <sumit@hicShopify.com>
|
|
* ──────────────────────────────────────────────────────────────────────────────────────────────────
|
|
*/
|
|
global with sharing class FetchInventoryWebhook implements Database.Batchable<String>, Database.allowscallouts, Database.Stateful{
|
|
|
|
String inventoryItemId;
|
|
String inventoryLevelResponse;
|
|
List<HIC_ShopifySync__HIC_Configuration__c > configurationList;
|
|
String store = 'Default';
|
|
|
|
public FetchInventoryWebhook(String inventoryItemId, String inventoryLevelResponse, List<HIC_ShopifySync__HIC_Configuration__c > configurationList,String store){
|
|
|
|
this.inventoryItemId = inventoryItemId;
|
|
this.inventoryLevelResponse = inventoryLevelResponse;
|
|
this.configurationList = configurationList;
|
|
this.store = store;
|
|
|
|
}
|
|
|
|
global Iterable<String> start(Database.BatchableContext bc){
|
|
|
|
|
|
if(configurationList.isEmpty()){
|
|
return new List<String>(); // No configuration found, exit batch job
|
|
}
|
|
String inventoryItemResponse = '';
|
|
List <String> responseList = new List <String>();
|
|
if(!configurationList.isEmpty()){
|
|
|
|
if(!Test.IsRunningTest()){
|
|
inventoryItemResponse = HIC_ShopifyUtility.fetchAllDataUsingId('inventory_items.json?ids='+inventoryItemId+'.json',configurationList);
|
|
}else{
|
|
inventoryItemResponse = '{ "inventory_items": [ { "id": 21681651843136, "sku": "phone 6", "created_at": "2019-01-08T23:13:17-12:00", "updated_at": "2019-01-09T01:00:08-12:00", "cost": "14000.00", "tracked": true, "admin_graphql_api_id": "gid://shopify/InventoryItem/21681651843136" } ] }';
|
|
}
|
|
}else{
|
|
return new List<String>();
|
|
}
|
|
responseList.add(inventoryItemResponse);
|
|
|
|
return responseList;
|
|
}
|
|
|
|
global void execute(Database.BatchableContext bc , List<String> scope){
|
|
Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(scope[0]);
|
|
List<Object> objectList = (List<Object>) meta.get('inventory_items');
|
|
list<HIC_ShopifySync__Inventory__c> listofInventoryItemExist = new list<HIC_ShopifySync__Inventory__c>();
|
|
|
|
if(inventoryItemId !='' && inventoryItemId != null){
|
|
String Inventoryquery = 'Select '+CheckAccessibility.isAccessible('id,HIC_ShopifySync__Shopify_Id__c','HIC_ShopifySync__Inventory__c') +' from HIC_ShopifySync__Inventory__c Where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Shopify_Id__c','HIC_ShopifySync__Inventory__c')+' = \''+ inventoryItemId +'\' LIMIT 49999';
|
|
listofInventoryItemExist = database.query(Inventoryquery);
|
|
}
|
|
|
|
String query;
|
|
if(listofInventoryItemExist.isEmpty() || listofInventoryItemExist == null || listofInventoryItemExist.size() == 0){
|
|
list<HIC_ShopifySync__HIC_Field_Mapping__c> inventoryFieldsList = new list<HIC_ShopifySync__HIC_Field_Mapping__c>();
|
|
query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c , HIC_ShopifySync__IsSFID__c, HIC_ShopifySync__Default_Value__c ','HIC_ShopifySync__HIC_Field_Mapping__c') +' from HIC_ShopifySync__HIC_Field_Mapping__c Where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Object_Name__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \'HIC_ShopifySync__Inventory__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999';
|
|
inventoryFieldsList = database.query(query);
|
|
list<HIC_ShopifySync__Inventory__c> inventoryListToupsert = HIC_ShopifyUtility.fillObjectValues(inventoryFieldsList,'HIC_ShopifySync__Inventory__c',objectList,configurationList);
|
|
system.debug('**inventoryListToupsert--'+inventoryListToupsert);
|
|
if(inventoryListToupsert.Isempty()) return;
|
|
Schema.SObjectType objectType = Schema.getGlobalDescribe().get('HIC_ShopifySync__Inventory__c');
|
|
Map<String, SObjectField> fieldMap = objectType.getDescribe().fields.getMap();
|
|
Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c');
|
|
|
|
if(!Test.isRunningTest()){
|
|
EmailErrorCSV.makeCsvAndSendInEmail(JSON.serialize(scope),'HIC_ShopifySync__Inventory__c',inventoryListToupsert,theField,true,store);
|
|
}
|
|
}
|
|
|
|
String companyTimeZone = [SELECT Id, TimeZoneSidKey FROM Organization Limit 1][0].TimeZoneSidKey;
|
|
Map<String, Object> inventoryLevelmeta = (Map<String, Object>) JSON.deserializeUntyped(inventoryLevelResponse);
|
|
list<HIC_ShopifySync__HIC_Field_Mapping__c> inventoryLevelList = new list<HIC_ShopifySync__HIC_Field_Mapping__c>();
|
|
query = '';
|
|
query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c , HIC_ShopifySync__IsSFID__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Is_Override__c ','HIC_ShopifySync__HIC_Field_Mapping__c')+' from HIC_ShopifySync__HIC_Field_Mapping__c Where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Object_Name__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \'HIC_ShopifySync__Inventory_Level__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999';
|
|
inventoryLevelList = database.query(query);
|
|
String listType = 'List<' + 'HIC_ShopifySync__Inventory_Level__c' + '>';
|
|
list<sobject> inventoryLevelListToupsert = (List<SObject>)Type.forName(listType).newInstance();
|
|
if(!inventoryLevelList.isEmpty() && !inventoryLevelmeta.isEmpty()){
|
|
inventoryLevelListToupsert.add(HIC_ShopifyUtility.processFieldTypeAndMapping(inventoryLevelList,'HIC_ShopifySync__Inventory_Level__c',inventoryLevelmeta,configurationList,companyTimeZone));
|
|
}
|
|
if(inventoryLevelListToupsert.Isempty()) return;
|
|
try{
|
|
Schema.SObjectType inventoryLevelobjectType = Schema.getGlobalDescribe().get('HIC_ShopifySync__Inventory_Level__c');
|
|
Map<String, SObjectField> inventoryLevelfieldMap = inventoryLevelobjectType.getDescribe().fields.getMap();
|
|
Schema.SobjectField inventoryLevelField = inventoryLevelfieldMap.get('HIC_ShopifySync__Shopify_Id__c');
|
|
EmailErrorCSV.makeCsvAndSendInEmail(JSON.serialize(scope),'HIC_ShopifySync__Inventory_Level__c',inventoryLevelListToupsert,inventoryLevelField,true,store);
|
|
}catch(Exception e){
|
|
System.debug('DML EXception '+e.getMessage()+ 'Line Number '+e.getLineNumber());
|
|
}
|
|
}
|
|
|
|
global void finish(Database.BatchableContext bc){
|
|
|
|
}
|
|
} |