/** * ─────────────────────────────────────────────────────────────────────────────────────────────────┐ * This Batch class is use to get inventory Levels 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 FetchInventoryLevels implements Database.Batchable, Database.AllowsCallouts, Database.Stateful{ List locationsList = new List(); String inventoryLevelResponse; String linkForNextPage = ''; Boolean isEnd = false; List objectList; List objectListToPass; Set locationIds; string locationId; boolean flag; String store = 'Default'; String updated_at_min = ''; String updated_at_max = ''; public List configurationList = new List(); global FetchInventoryLevels(String linkForNextPage, string locationId, String store){ this.linkForNextPage = linkForNextPage; this.locationId = locationId; this.store = store; } global FetchInventoryLevels(String linkForNextPage, string locationId, String store, String updated_at_min, String updated_at_max){ this.linkForNextPage = linkForNextPage; this.locationId = locationId; this.store = store; this.updated_at_min = updated_at_min; this.updated_at_max = updated_at_max; } //Depreceated global FetchInventoryLevels(String linkForNextPage, string locationId){ this.linkForNextPage = linkForNextPage; this.locationId = locationId; } global FetchInventoryLevels(integer noOfPages){ System.debug('Deprecated'); } global FetchInventoryLevels(String linkForNextPage, boolean isEnd, String store){ this.linkForNextPage = linkForNextPage; this.isEnd = isEnd; this.store = store; } global FetchInventoryLevels(String linkForNextPage, boolean isEnd, String store, String updated_at_min, String updated_at_max){ this.linkForNextPage = linkForNextPage; this.isEnd = isEnd; this.store = store; this.updated_at_min = updated_at_min; this.updated_at_max = updated_at_max; } //Depreceated global FetchInventoryLevels(String linkForNextPage, boolean isEnd){ this.linkForNextPage = linkForNextPage; this.isEnd = isEnd; } global Database.QueryLocator start(Database.BatchableContext BC){ String queryConfiguration = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__Chunk_Size__c,name,HIC_ShopifySync__Default_Field_Mapping__c, HIC_ShopifySync__Shopify_URL__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 49999'; configurationList = database.query(queryConfiguration); objectListToPass = new List (); String query = ''; if(locationId != null && locationId != ''){ if(Schema.sObjectType.HIC_ShopifySync__Location__c.fields.HIC_ShopifySync__Shopify_Id__c.isAccessible()){ query = 'Select HIC_ShopifySync__Shopify_Id__c From HIC_ShopifySync__Location__c where Id= :locationId limit 1'; } }else{ if(Schema.sObjectType.HIC_ShopifySync__Location__c.fields.HIC_ShopifySync__Shopify_Id__c.isAccessible()){ query = 'Select HIC_ShopifySync__Shopify_Id__c From HIC_ShopifySync__Location__c where HIC_ShopifySync__is_processed__c = false and HIC_ShopifySync__Store__c =:store limit 1'; } } return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List scope){ String JSONResponse = ''; locationsList = scope; 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 ]); for (HIC_ShopifySync__Location__c sObj : scope){ if(sObj.Id != null && sObj.HIC_ShopifySync__Shopify_Id__c != null){ if(!Test.isRunningTest()){ locationId = sObj.Id; if(String.isNotBlank(linkForNextPage) && !isEnd){ JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('locations/'+sObj.HIC_ShopifySync__Shopify_Id__c+'/inventory_levels.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c+'&page_info='+linkForNextPage, configurationList); }else{ JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('locations/'+sObj.HIC_ShopifySync__Shopify_Id__c+'/inventory_levels.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c, configurationList); } if(String.isNotBlank(JSONResponse)){ Map main = (Map) JSON.deserializeUntyped(JSONResponse); if(!main.isEmpty() && main.containskey('response') && main.containskey('link') ){ if( main.get('response') != null) inventoryLevelResponse = String.valueof(main.get('response')); if(main.get('link') != null){ linkForNextPage = String.valueof(main.get('link')); }else{ isEnd = true; } } } } else{ if(String.isEmpty(locationId)){ 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" } ] }'; } else { inventoryLevelResponse = '{ "inventory_levels": [] }'; } } system.debug('inventoryLevelResponse response--> '+inventoryLevelResponse); if(inventoryLevelResponse != '' && inventoryLevelResponse != null ){ Map meta = (Map) JSON.deserializeUntyped(inventoryLevelResponse); if(meta.get('inventory_levels') !='[]' && meta.get('inventory_levels') !=null && meta.get('inventory_levels') != '' ){ objectList = (List) meta.get('inventory_levels'); objectListToPass.addAll(objectList); system.debug('**objectListToPass'+objectListToPass); } } } } } global void finish(Database.BatchableContext BC){ if(inventoryLevelResponse != '' && inventoryLevelResponse != null){ if(objectListToPass != null && !objectListToPass.isEmpty()){ /*string inventoryItems = ''; //if(!scope.isEmpty() && scope != null){ Integer listSize = objectListToPass.size(); Integer i = 1; for(Object inventoryLevel : objectListToPass){ 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); } }*/ FetchInventoryItems do1By1 = new FetchInventoryItems(objectListToPass,store); if(!Test.isRunningTest()){ Database.executeBatch(do1By1, 50); } } if(!isEnd){ FetchInventoryLevels doItAgain = new FetchInventoryLevels(linkForNextPage,locationId,store,updated_at_min,updated_at_max); if(!Test.isRunningTest()){ Database.executeBatch(doItAgain, 1); } }else{ if(!locationsList.isEmpty()){ if(Schema.sObjectType.HIC_ShopifySync__Location__c.fields.HIC_ShopifySync__is_processed__c.isUpdateable() && CheckAccessibility.isUpdateable('HIC_ShopifySync__is_processed__c','HIC_ShopifySync__Location__c')){ locationsList[0].HIC_ShopifySync__is_processed__c = true ; } if(Schema.getGlobalDescribe().get('HIC_ShopifySync__Location__c').getDescribe().isUpdateable() && CheckAccessibility.isUpsertable('id','HIC_ShopifySync__Location__c') && CheckAccessibility.isUpdateable('id','HIC_ShopifySync__Location__c') && Schema.sObjectType.HIC_ShopifySync__Location__c.isUpdateable()){ Schema.SObjectType objectType = Schema.getGlobalDescribe().get('HIC_ShopifySync__Location__c'); Map fieldMap = objectType.getDescribe().fields.getMap(); Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c'); EmailErrorCSV.makeCsvAndSendInEmail(JSON.serialize(locationsList),'HIC_ShopifySync__Location__c',locationsList,theField,false,store); } } list locList = new list([Select Id,HIC_ShopifySync__is_processed__c from HIC_ShopifySync__Location__c where HIC_ShopifySync__is_processed__c = false]); if(!locList.IsEmpty()){ FetchInventoryLevels doItAgain = new FetchInventoryLevels('',false,store,updated_at_min,updated_at_max); Database.executeBatch(doItAgain, 1); }else{ for(HIC_ShopifySync__Location__c loc : locList){ loc.HIC_ShopifySync__is_processed__c = false; } try{ database.update(locList,false); }catch(Exception e){ } FetchInventoryItems do1By1 = new FetchInventoryItems(true,store,updated_at_min,updated_at_max); Database.executeBatch(do1By1, 1); } } }else{ FetchInventoryItems do1By1 = new FetchInventoryItems(true,store,updated_at_min,updated_at_max); Database.executeBatch(do1By1, 1); } } }