/** * ─────────────────────────────────────────────────────────────────────────────────────────────────┐ * 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 FetchCollections_Batch implements Database.Batchable,Database.AllowsCallouts,database.stateful { global string collectionId{get;set;} String linkForNextPage = ''; Boolean isEnd = false; String store = 'Default'; String updated_at_min = ''; String updated_at_max = ''; List fieldMapping; list collectionFieldsList; public list configurationList = new list(); //Depreceated global FetchCollections_Batch(string collectionId){ this.collectionId = collectionId; } global FetchCollections_Batch(String linkForNextPage , Boolean isEnd, String Store){ this.linkForNextPage = linkForNextPage; this.store = store; } global FetchCollections_Batch(String linkForNextPage , Boolean isEnd, 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; } //Depreceated global FetchCollections_Batch(integer noOfPages){ System.debug('Deprecated'); } //Depreceated global FetchCollections_Batch(String linkForNextPage , Boolean isEnd){ this.linkForNextPage = linkForNextPage; } global Iterable start(Database.BatchableContext bc){ String collectionResponse = ''; String JSONResponse = ''; //Get Configurations for store String queryConfi = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__Chunk_Size__c,name,HIC_ShopifySync__Default_Field_Mapping__c, HIC_ShopifySync__Opportunity_Sync__c,HIC_ShopifySync__Specify_Close_Date__c, HIC_ShopifySync__Shopify_URL__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(queryConfi); List fieldMapping = 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__Collection__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; fieldMapping = database.query(query); if(collectionId != null){ List emptyObjectList = new List (); isEnd = true; return emptyObjectList; } if(!Test.isRunningTest()){ // Get Shopif's custom_collections JSON Response if(String.isNotBlank(linkForNextPage) && !isEnd){ JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('custom_collections.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c+'&page_info='+linkForNextPage, configurationList); }else{ JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('custom_collections.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c+(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) collectionResponse = String.valueof(main.get('response')); if(main.get('link') != null){ linkForNextPage = String.valueof(main.get('link')); }else{ isEnd = true; } } } }else{ collectionResponse = '{"custom_collections": [{"id": 100793057344, "handle": "test-collection", "title": "Test Collection", "updated_at": "2019-01-14T03:59:48-12:00", "body_html": "", "published_at": "2019-01-09T22:51:33-12:00", "sort_order": "best-selling", "template_suffix": "", "disjunctive": false, "rules": [ { "column": "title", "relation": "not_equals", "condition": "null" } ], "published_scope": "web", "admin_graphql_api_id": "gid://shopify/Collection/100793057344" } ]}'; } collectionFieldsList = new List (); collectionFieldsList = fieldMapping; Map meta = (Map) JSON.deserializeUntyped(collectionResponse); List objectList = (List) meta.get('custom_collections'); return objectList; } global void execute(Database.BatchableContext BC, List Scope){ for(object obj : Scope){ Map ObjectMap = (Map)obj; ObjectMap.put('type',(Object)'Custom'); } //Process Shopify Collection field Mapping if(!Test.isRunningTest()){ HIC_ShopifyUtility.processSobjectList(collectionFieldsList,Scope,'HIC_ShopifySync__Collection__c','HIC_ShopifySync__Shopify_Id__c',configurationList,false,store); } } global void finish(Database.BatchableContext BC){ if(!isEnd){ if(!Test.isRunningTest()){ //Call again for next pege (Pagination) FetchCollections_Batch callBatchAgain = new FetchCollections_Batch(linkForNextPage,false,store,updated_at_min,updated_at_max); Database.executeBatch(callBatchAgain,1); } }else{ //Sent Email Notification EmailErrorCSV.SendErrormail('HIC_ShopifySync__Collection__c'); //Call next batch for Custom Collection's Collect if(!Test.isRunningTest()){ FetchCollects_Batch callBatchAgain = new FetchCollects_Batch('',false,store,updated_at_min,updated_at_max); Database.executeBatch(callBatchAgain,1); } } } }