/** * ─────────────────────────────────────────────────────────────────────────────────────────────────┐ * This Batch class is use to get inventory Items from the Shopify and upsert into the Salesforce. * 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 * ────────────────────────────────────────────────────────────────────────────────────────────────── */ global with sharing class FetchInventoryItems implements Database.Batchable, Database.AllowsCallouts, Database.Stateful{ List objList = new List (); boolean BatchFinish = false; String store = 'Default'; String updated_at_min = ''; String updated_at_max = ''; public List configurationList = new List(); global FetchInventoryItems(List objList,String store){ this.objList = objList; this.store = store; } global FetchInventoryItems(boolean BatchFinish,String store){ this.BatchFinish = BatchFinish; this.store = store; } global FetchInventoryItems(boolean BatchFinish,String store, String updated_at_min, String updated_at_max){ this.BatchFinish = BatchFinish; this.store = store; this.updated_at_min = updated_at_min; this.updated_at_max = updated_at_max; } //Depreceated global FetchInventoryItems(List objList){ this.objList = objList; } //Depreceated global FetchInventoryItems(boolean BatchFinish){ this.BatchFinish = BatchFinish; } global Iterable start(Database.BatchableContext bc){ String query = 'Select '+CheckAccessibility.isAccessible('name, HIC_ShopifySync__Shopify_URL__c,HIC_ShopifySync__Default_Field_Mapping__c, HIC_ShopifySync__Specify_Close_Date__c,HIC_ShopifySync__API_Key__c, HIC_ShopifySync__Private_Key_Password__c, HIC_ShopifySync__Opportunity__c, HIC_ShopifySync__Order__c, HIC_ShopifySync__Opportunity_Line_Item__c, HIC_ShopifySync__Order_Line_Item__c, HIC_ShopifySync__Order_Fulfillment__c, HIC_ShopifySync__Person_Account__c ','HIC_ShopifySync__HIC_Configuration__c')+' from HIC_ShopifySync__HIC_Configuration__c where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Configuration__c') +' = \''+store+'\' limit 1'; configurationList = database.query(query); if(BatchFinish){ List emptylist = new List(); return emptylist; }else{ return objList; } } global void execute(Database.BatchableContext BC, List scope){ string inventoryItems = ''; //if(!scope.isEmpty() && scope != null){ Integer listSize = scope.size(); Integer i = 1; for(Object inventoryLevel : scope){ Map inventoryLevelMap = (Map)inventoryLevel; inventoryItems += inventoryLevelMap.get('inventory_item_id') + ','; } //} if(inventoryItems != null && inventoryItems != ''){ string inventoryLevelResponse = ''; if(!Test.isRunningTest()){ inventoryLevelResponse = HIC_ShopifyUtility.fetchAllData('inventory_items.json?ids='+inventoryItems, configurationList); } else{ inventoryLevelResponse = '{ "inventory_levels": [ { "inventory_item_id": 21523210764352, "location_id": 23173333056, "available": 5, "updated_at": "2019-01-09T22:34:56-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21523210764352" }, { "inventory_item_id": 21523413696576, "location_id": 23173333056, "available": 56, "updated_at": "2019-01-11T00:34:44-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21523413696576" }, { "inventory_item_id": 21523718242368, "location_id": 23173333056, "available": 551, "updated_at": "2019-01-08T21:02:04-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21523718242368" }, { "inventory_item_id": 21523718275136, "location_id": 23173333056, "available": 5, "updated_at": "2019-01-10T03:25:32-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21523718275136" }, { "inventory_item_id": 21649822777408, "location_id": 23173333056, "available": 54, "updated_at": "2019-01-08T21:09:08-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21649822777408" }, { "inventory_item_id": 21680012296256, "location_id": 23173333056, "available": 65, "updated_at": "2019-01-08T23:06:51-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21680012296256" }, { "inventory_item_id": 21681311449152, "location_id": 23173333056, "available": 85, "updated_at": "2019-01-08T23:11:57-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21681311449152" }, { "inventory_item_id": 21681651843136, "location_id": 23173333056, "available": 555, "updated_at": "2019-01-10T02:08:22-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21681651843136" }, { "inventory_item_id": 21681941413952, "location_id": 23173333056, "available": 846, "updated_at": "2019-01-08T23:14:26-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21681941413952" }, { "inventory_item_id": 21707042914368, "location_id": 23173333056, "available": 54, "updated_at": "2019-01-09T01:15:11-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21707042914368" }, { "inventory_item_id": 21723101986880, "location_id": 23173333056, "available": 52, "updated_at": "2019-01-09T02:10:25-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21723101986880" }, { "inventory_item_id": 21733332877376, "location_id": 23173333056, "available": 45, "updated_at": "2019-01-09T02:58:09-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21733332877376" }, { "inventory_item_id": 21733592793152, "location_id": 23173333056, "available": 465, "updated_at": "2019-01-09T02:59:47-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21733592793152" }, { "inventory_item_id": 21856092782656, "location_id": 23173333056, "available": 8, "updated_at": "2019-01-09T22:37:11-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=21856092782656" }, { "inventory_item_id": 22030277836864, "location_id": 23173333056, "available": 12, "updated_at": "2019-01-11T03:37:20-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=22030277836864" }, { "inventory_item_id": 22620080570432, "location_id": 23173333056, "available": 5412, "updated_at": "2019-01-14T00:07:24-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=22620080570432" }, { "inventory_item_id": 22620080635968, "location_id": 23173333056, "available": 0, "updated_at": "2019-01-14T00:07:23-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=22620080635968" }, { "inventory_item_id": 22623988842560, "location_id": 23173333056, "available": 25, "updated_at": "2019-01-14T00:21:21-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=22623988842560" }, { "inventory_item_id": 22647018618944, "location_id": 23173333056, "available": 0, "updated_at": "2019-01-14T02:13:12-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=22647018618944" }, { "inventory_item_id": 22650449887296, "location_id": 23173333056, "available": 10, "updated_at": "2019-01-14T02:44:58-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=22650449887296" }, { "inventory_item_id": 22656224624704, "location_id": 23173333056, "available": 0, "updated_at": "2019-01-14T03:54:59-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=22656224624704" }, { "inventory_item_id": 22656588972096, "location_id": 23173333056, "available": 232, "updated_at": "2019-01-14T03:59:10-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=22656588972096" }, { "inventory_item_id": 22656629309504, "location_id": 23173333056, "available": 2, "updated_at": "2019-01-14T03:59:50-12:00", "admin_graphql_api_id": "gid://shopify/InventoryLevel/22851813440?inventory_item_id=22656629309504" } ] }'; } Map meta = (Map) JSON.deserializeUntyped(inventoryLevelResponse); List objectList = (List) meta.get('inventory_items'); /*FetchInventoryItems do1By1 = new FetchInventoryItems(objectList,store); if(!Test.isRunningTest()){ Database.executeBatch(do1By1, 50); }*/ //} List inventoryFieldsList = new List([Select HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c , HIC_ShopifySync__IsSFID__c, HIC_ShopifySync__Default_Value__c from HIC_ShopifySync__HIC_Field_Mapping__c Where HIC_ShopifySync__Object_Name__c = 'HIC_ShopifySync__Inventory__c' AND HIC_ShopifySync__Store__c =: store]); try{ if(!inventoryFieldsList.isEmpty() && !scope.isEmpty()) { HIC_ShopifyUtility.processSobjectList(inventoryFieldsList,objectList,'HIC_ShopifySync__Inventory__c','HIC_ShopifySync__Shopify_Id__c', configurationList,false,store); } }catch(Exception e){ } } List inventoryLevelList = new List([Select HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c , HIC_ShopifySync__IsSFID__c, HIC_ShopifySync__Default_Value__c from HIC_ShopifySync__HIC_Field_Mapping__c Where HIC_ShopifySync__Object_Name__c = 'HIC_ShopifySync__Inventory_Level__c' AND HIC_ShopifySync__Store__c =: store]); if(!inventoryLevelList.isEmpty() && !scope.isEmpty()){ try{ HIC_ShopifyUtility.processSobjectList(inventoryLevelList,scope,'HIC_ShopifySync__Inventory_Level__c','HIC_ShopifySync__Shopify_Id__c',configurationList,false,store); }catch(Exception e){ } } } global void finish(Database.BatchableContext BC){ try{ if(BatchFinish){ List configListToCheckObjects = new List (); String query = 'Select '+CheckAccessibility.isAccessible('id, HIC_ShopifySync__Draft_Order_Sync__c, HIC_ShopifySync__InventoryBatchCompleted__c,HIC_ShopifySync__IsCurrencyChanged__c,HIC_ShopifySync__Batch_Is_Processing__c,HIC_ShopifySync__Private_Key_Password__c,HIC_ShopifySync__Shopify_URL__c,HIC_ShopifySync__API_Key__c, HIC_ShopifySync__Collection_Sync__c, HIC_ShopifySync__Inventory_Location_Sync__c, HIC_ShopifySync__Enable_Draft_Order__c, name,HIC_ShopifySync__Opportunity__c,HIC_ShopifySync__Order__c,HIC_ShopifySync__Opportunity_Line_Item__c,HIC_ShopifySync__Order_Line_Item__c, HIC_ShopifySync__Order_Fulfillment__c, HIC_ShopifySync__Person_Account__c','HIC_ShopifySync__HIC_Configuration__c')+' from HIC_ShopifySync__HIC_Configuration__c Where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c', 'HIC_ShopifySync__HIC_Configuration__c')+' = \''+store+'\' LIMIT 49999'; configListToCheckObjects = database.query(query); if(configListToCheckObjects[0].HIC_ShopifySync__Draft_Order_Sync__c){ HIC_ShopifyUtility.fetchDraftOrders(configListToCheckObjects,store,updated_at_min,updated_at_max); if(CheckAccessibility.isUpsertable('HIC_ShopifySync__InventoryBatchCompleted__c','HIC_ShopifySync__HIC_Configuration__c')){ configListToCheckObjects[0].HIC_ShopifySync__InventoryBatchCompleted__c = true; } if(!configListToCheckObjects.isempty() && configListToCheckObjects != null ){ if(Schema.sObjectType.HIC_ShopifySync__HIC_Configuration__c.isUpdateable() && CheckAccessibility.isUpdateable('id', 'HIC_ShopifySync__HIC_Configuration__c')){ try{ update configListToCheckObjects; }catch(Exception e){ } } } }else{ if(CheckAccessibility.isUpsertable('HIC_ShopifySync__IsCurrencyChanged__c,HIC_ShopifySync__Batch_Is_Processing__c,HIC_ShopifySync__InventoryBatchCompleted__c','HIC_ShopifySync__HIC_Configuration__c')){ configListToCheckObjects[0].HIC_ShopifySync__IsCurrencyChanged__c = false; configListToCheckObjects[0].HIC_ShopifySync__Batch_Is_Processing__c = false; configListToCheckObjects[0].HIC_ShopifySync__InventoryBatchCompleted__c = true; } if(!configListToCheckObjects.isempty() && configListToCheckObjects != null ){ if(Schema.sObjectType.HIC_ShopifySync__HIC_Configuration__c.isUpdateable() && CheckAccessibility.isUpdateable('id', 'HIC_ShopifySync__HIC_Configuration__c')) try{ update configListToCheckObjects; }catch(Exception e){ } EmailErrorCSV.SuccessMailSend(); } } } }catch(Exception e){ } } }