/** * ─────────────────────────────────────────────────────────────────────────────────────────────────┐ * This Batch class is use for upsert Shopify's Produt and Variant 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 * ────────────────────────────────────────────────────────────────────────────────────────────────── */ global with sharing class FetchLocations_Batch implements Database.Batchable,Database.AllowsCallouts, Database.Stateful{ String store = 'Default'; String linkForNextPage = ''; Boolean isEnd = false; String updated_at_min = ''; String updated_at_max = ''; public List configurationList = new List(); global List LocationListToupsert; global List LocationListToPass; List locationFieldsList; integer noOfPages = 0; global FetchLocations_Batch(integer noOfPages,String Store){ this.noOfPages = noOfPages; this.store = Store; } global FetchLocations_Batch(integer noOfPages,String Store, String updated_at_min, String updated_at_max){ this.noOfPages = noOfPages; this.store = Store; this.updated_at_min = updated_at_min; this.updated_at_max = updated_at_max; } global FetchLocations_Batch(String linkForNextPage, String Store, String updated_at_min, String updated_at_max){ this.linkForNextPage = linkForNextPage; this.store = Store; this.updated_at_min = updated_at_min; this.updated_at_max = updated_at_max; } //Deprecreated global FetchLocations_Batch(integer noOfPages){ this.noOfPages = noOfPages; } global Iterable start(Database.BatchableContext bc){ String JSONResponse = ''; if(updated_at_min != ''){ //List emptyObjectList = new List (); //return emptyObjectList; isEnd = true; } String query = 'Select '+CheckAccessibility.isAccessible('name, HIC_ShopifySync__Chunk_Size__c, 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(query); LocationListToPass = new List (); string locationResponse = ''; if(!Test.isRunningTest()){ if(String.isNotBlank(linkForNextPage) && !isEnd){ JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('locations.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c+'&page_info='+linkForNextPage, configurationList); }else{ JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('locations.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c+'&status=any'+(updated_at_min!=''?'&updated_at_min='+updated_at_min:'')+(updated_at_max!=''?'&updated_at_max='+updated_at_max:''), 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) locationResponse = String.valueof(main.get('response')); if(main.get('link') != null){ linkForNextPage = String.valueof(main.get('link')); }else{ isEnd = true; } } } //locationResponse = hic_shopifyUtility.fetchAllDataUsingId('locations.json?page='+ noOfPages, configurationList); } else{ locationResponse = '{ "locations": [ { "id": 23173333056, "name": "Address: A-45, Udhyog Marg, A Block, Sector 2, Noida, Uttar Pradesh 2013ujjawaligggffdgd", "address1": "Address: A-45, Udhyog Marg, A Block, Sector 2, Noida, Uttar Pradesh 201301", "address2": "56/37", "city": "Noida", "zip": "201301", "province": "Uttar Pradesh", "country": "IN", "phone": "8168642194", "created_at": "2019-01-07T20:19:16-12:00", "updated_at": "2019-01-11T03:02:28-12:00", "country_code": "IN", "country_name": "India", "province_code": "UP", "legacy": false, "active": true, "admin_graphql_api_id": "gid://shopify/Location/23173333056" }, { "id": 25860014144, "name": "Varanasi", "address1": "Varanasi", "address2": "Varanasi", "city": "Varanasi", "zip": "552525", "province": "Andaman and Nicobar", "country": "IN", "phone": "", "created_at": "2019-01-14T01:51:26-12:00", "updated_at": "2019-01-14T01:51:34-12:00", "country_code": "IN", "country_name": "India", "province_code": "AN", "legacy": false, "active": true, "admin_graphql_api_id": "gid://shopify/Location/25860014144" } ] }'; } Map meta = (Map) JSON.deserializeUntyped(locationResponse); List listoflocation = new List(); if(meta.containsKey('locations') && meta.get('locations') != null) listoflocation = (List) meta.get('locations'); return listoflocation; } global void execute(Database.BatchableContext bc , List scope){ HIC_ShopifySync__location__c location = new HIC_ShopifySync__location__c(); locationFieldsList = new list(); String 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__Location__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; locationFieldsList = database.query(query); if(!locationFieldsList.isEmpty() && !scope.isEmpty()){ LocationListToupsert = hic_shopifyUtility.fillObjectValues(locationFieldsList,'HIC_ShopifySync__Location__c',scope, configurationList); for(HIC_ShopifySync__Location__c locatn : LocationListToupsert){ locatn.HIC_ShopifySync__is_processed__c = false; locatn.HIC_ShopifySync__Store__c = store; } } try{ if(LocationListToupsert.Isempty()) return; 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'); LocationListToPass.addAll(LocationListToupsert); if(CheckAccessibility.isUpsertable('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') ) EmailErrorCSV.makeCsvAndSendInEmail(JSON.serialize(scope),'HIC_ShopifySync__Location__c',LocationListToupsert,theField,false,store); }catch(Exception e){ } } global void finish(Database.BatchableContext bc){ /*if(noOfPages > 1){ integer revisedNoOfPages = noOfPages - 1; FetchLocations_Batch callBatchAgain = new FetchLocations_Batch(revisedNoOfPages,store,updated_at_min,updated_at_max); if(!Test.isRunningTest()){ Database.executeBatch(callBatchAgain,1); } }*/ if(!isEnd){ if(!Test.isRunningTest()){ //Call again for next pege (Pagination) FetchLocations_Batch callBatchAgain = new FetchLocations_Batch(linkForNextPage,store,updated_at_min,updated_at_max); Database.executeBatch(callBatchAgain,1); } }else{ FetchInventoryLevels FetchInventoryLevels = new FetchInventoryLevels('',false,store,updated_at_min,updated_at_max); if(!Test.isRunningTest()){ Database.executeBatch(FetchInventoryLevels,1); } } } }