/** * ─────────────────────────────────────────────────────────────────────────────────────────────────┐ * This Batch class is use for upsert Shopify's Smart collection into the Salesfroce * Collection is group of products based on category * Smart Collection is the collection of product which has set of rules/conditions * Custom Collection is the collection of product which has not set of rules/conditions * Collects all list of products which lies under the Collections * 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 FetchCollects_Batch implements Database.Batchable,Database.AllowsCallouts,database.stateful { global string collectionId{get;set;} String linkForNextPage = ''; Boolean isEnd = false; String JSONResponse = ''; String store = 'Default'; String updated_at_min = ''; String updated_at_max = ''; List collectionFieldsList; public List configurationList = new List(); Boolean dontRun = false; global FetchCollects_Batch(string collectionId){ this.collectionId = collectionId; dontRun = true; } global FetchCollects_Batch(String linkForNextPage , Boolean isEnd,String store){ this.linkForNextPage = linkForNextPage; this.isEnd = isEnd; this.store = store; } global FetchCollects_Batch(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; } global FetchCollects_Batch(integer noOfPages){ System.debug('Deprecated'); } //Depreceated global FetchCollects_Batch(String linkForNextPage , Boolean isEnd){ this.linkForNextPage = linkForNextPage; this.isEnd = isEnd; } global Iterable start(Database.BatchableContext bc){ String collectsResponse = ''; 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); if(!Test.isRunningTest()){ // Get Shopif's collects JSON Response if(String.isNotBlank(linkForNextPage) && !isEnd){ JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('collects.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c+'&page_info='+linkForNextPage, configurationList); }else{ if(String.isNotBlank(collectionId)){ JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('collects.json?collection_id='+collectionId+'&limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c,configurationList); //JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('/collections/' + collectionId + '/products.json?limit=' + configurationList[0].HIC_ShopifySync__Chunk_Size__c, configurationList ); }else if(updated_at_min!=''){ isEnd = true; }else{ JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('collects.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) collectsResponse = String.valueof(main.get('response')); if(main.get('link') != null){ linkForNextPage = String.valueof(main.get('link')); }else{ isEnd = true; } } } }else{ collectsResponse = ' { "collects": [ { "id": 10561557004352, "collection_id": 98764324928, "product_id": 2220823806016, "featured": false, "created_at": "2019-01-07T20:46:15-12:00", "updated_at": "2019-01-07T20:46:15-12:00", "position": 1, "sort_value": "0000000001" }, { "id": 10629828771904, "collection_id": 100793057344, "product_id": 2220862111808, "featured": false, "created_at": "2019-01-09T22:52:37-12:00", "updated_at": "2019-01-09T22:52:37-12:00", "position": 2, "sort_value": "0000000002" }, { "id": 10629828804672, "collection_id": 100793057344, "product_id": 2220823806016, "featured": false, "created_at": "2019-01-09T22:52:37-12:00", "updated_at": "2019-01-09T22:52:37-12:00", "position": 3, "sort_value": "0000000003" }, { "id": 10662849839168, "collection_id": 100793057344, "product_id": 2298561232960, "featured": false, "created_at": "2019-01-11T03:37:20-12:00", "updated_at": "2019-01-11T03:37:20-12:00", "position": 3, "sort_value": "0000000003" }, { "id": 10740677017664, "collection_id": 100793057344, "product_id": 2371482910784, "featured": false, "created_at": "2019-01-14T00:07:24-12:00", "updated_at": "2019-01-14T00:07:24-12:00", "position": 4, "sort_value": "0000000004" }, { "id": 10740828831808, "collection_id": 100793057344, "product_id": 2371754065984, "featured": false, "created_at": "2019-01-14T00:21:21-12:00", "updated_at": "2019-01-14T00:21:21-12:00", "position": 5, "sort_value": "0000000005" }, { "id": 10742718824512, "collection_id": 100793057344, "product_id": 2373655855168, "featured": false, "created_at": "2019-01-14T01:54:42-12:00", "updated_at": "2019-01-14T01:54:42-12:00", "position": 6, "sort_value": "0000000006" }, { "id": 10745159221312, "collection_id": 100793057344, "product_id": 2375645200448, "featured": false, "created_at": "2019-01-14T03:53:08-12:00", "updated_at": "2019-01-14T03:53:08-12:00", "position": 7, "sort_value": "0000000007" } ]}' ; } if(!String.isNotBlank(collectsResponse)){ List objectListEmpty = new List(); return objectListEmpty ; } collectionFieldsList = 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__Collect__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999' ; collectionFieldsList = database.query(query); Map meta = (Map) JSON.deserializeUntyped(collectsResponse); List objectList = (List) meta.get('collects'); return objectList; } global void execute(Database.BatchableContext BC, List Scope){ //Process Field Mapping for Collects System.debug('collect scope--'+Scope); Set productIds = new Set(); for(Object pr : Scope){ Map prdmap = (Map) pr; if(!prdmap.isEmpty()){ if(prdmap.containsKey('product_id')) { productIds.add(String.valueOf(prdmap.get('product_id'))); } } } Map productIdToNameMap = new Map(); if(!productIds.isEmpty()){ for(Product2 prod : [SELECT HIC_ShopifySync__Shopify_Id__c, Name FROM Product2 WHERE HIC_ShopifySync__Shopify_Id__c IN :productIds]) { productIdToNameMap.put(prod.HIC_ShopifySync__Shopify_Id__c, prod.Name); } } for(Object pr : Scope){ Map prdmap = (Map) pr; if(!prdmap.isEmpty() && prdmap != null){ //prdmap.put('collection_id',collectionId); //prdmap.put('product_id',String.valueOf(prdmap.get('id'))); prdmap.put('id',String.valueOf(prdmap.get('product_id'))+String.valueOf(prdmap.get('collection_id'))); if(prdmap.containsKey('product_id') && productIdToNameMap.containsKey(String.valueOf(prdmap.get('product_id')))) { prdmap.put('handle', productIdToNameMap.get(String.valueOf(prdmap.get('product_id')))); } } } if(!Test.isRunningTest()){ HIC_ShopifyUtility.processSobjectList(collectionFieldsList,Scope,'HIC_ShopifySync__Collect__c','HIC_ShopifySync__Shopify_Id__c',configurationList,dontRun,store); } } global void finish(Database.BatchableContext BC){ List configListToCheckObjects = new List (); String query = 'Select '+CheckAccessibility.isAccessible('id,HIC_ShopifySync__Default_Field_Mapping__c, HIC_ShopifySync__Draft_Order_Sync__c, HIC_ShopifySync__CollectionsBatchCompleted__c, HIC_ShopifySync__Collection_Sync__c,HIC_ShopifySync__IsCurrencyChanged__c,HIC_ShopifySync__Batch_Is_Processing__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__Inventory_Location__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(!isEnd){ //Call Batch again for next page (Pagination) if(!Test.isRunningTest()){ FetchCollects_Batch callBatchAgain = new FetchCollects_Batch(linkForNextPage,false,store,updated_at_min,updated_at_max); Database.executeBatch(callBatchAgain,1); } // Call next batch for location and inventory level and inventory item }else if(configListToCheckObjects[0].HIC_ShopifySync__Inventory_Location__c && !dontRun){ HIC_ShopifyUtility.fetchLocations(configurationList,store,updated_at_min,updated_at_max); configListToCheckObjects[0].HIC_ShopifySync__CollectionsBatchCompleted__c = true; // Call next batch for Draft order }else if(configListToCheckObjects[0].HIC_ShopifySync__Draft_Order_Sync__c && !dontRun){ HIC_ShopifyUtility.fetchDraftOrders(configListToCheckObjects,store,updated_at_min,updated_at_max); configListToCheckObjects[0].HIC_ShopifySync__CollectionsBatchCompleted__c = true; //Update Configuraion for tack batch process in the page }else if(!configListToCheckObjects[0].HIC_ShopifySync__Inventory_Location__c && !configListToCheckObjects[0].HIC_ShopifySync__Draft_Order_Sync__c && !dontRun){ if(CheckAccessibility.isUpdateable('HIC_ShopifySync__IsCurrencyChanged__c,HIC_ShopifySync__Batch_Is_Processing__c','HIC_ShopifySync__HIC_Configuration__c') && Schema.sObjectType.HIC_ShopifySync__HIC_Configuration__c.fields.HIC_ShopifySync__Batch_Is_Processing__c.isUpdateable() && Schema.sObjectType.HIC_ShopifySync__HIC_Configuration__c.fields.HIC_ShopifySync__IsCurrencyChanged__c.isUpdateable()){ configListToCheckObjects[0].HIC_ShopifySync__IsCurrencyChanged__c = false; configListToCheckObjects[0].HIC_ShopifySync__Batch_Is_Processing__c = false; } configListToCheckObjects[0].HIC_ShopifySync__CollectionsBatchCompleted__c = true; } if(isEnd && CheckAccessibility.isUpsertable('id','HIC_ShopifySync__HIC_Configuration__c') && CheckAccessibility.isUpdateable('id','HIC_ShopifySync__HIC_Configuration__c') && Schema.sObjectType.HIC_ShopifySync__HIC_Configuration__c.isUpdateable() ){ if(!configListToCheckObjects.isempty() && configListToCheckObjects != null ){ update configListToCheckObjects; //Sent Email Notification if(configListToCheckObjects[0].HIC_ShopifySync__Inventory_Location__c == false){ EmailErrorCSV.SuccessMailSend(); } } } } }