/** * ─────────────────────────────────────────────────────────────────────────────────────────────────┐ * This Batch class is use for upsert Shopify's Inventory 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 FetchInventoryWebhook implements Database.Batchable, Database.allowscallouts, Database.Stateful{ String inventoryItemId; String inventoryLevelResponse; List configurationList; String store = 'Default'; public FetchInventoryWebhook(String inventoryItemId, String inventoryLevelResponse, List configurationList,String store){ this.inventoryItemId = inventoryItemId; this.inventoryLevelResponse = inventoryLevelResponse; this.configurationList = configurationList; this.store = store; } global Iterable start(Database.BatchableContext bc){ if(configurationList.isEmpty()){ return new List(); // No configuration found, exit batch job } String inventoryItemResponse = ''; List responseList = new List (); if(!configurationList.isEmpty()){ if(!Test.IsRunningTest()){ inventoryItemResponse = HIC_ShopifyUtility.fetchAllDataUsingId('inventory_items.json?ids='+inventoryItemId+'.json',configurationList); }else{ inventoryItemResponse = '{ "inventory_items": [ { "id": 21681651843136, "sku": "phone 6", "created_at": "2019-01-08T23:13:17-12:00", "updated_at": "2019-01-09T01:00:08-12:00", "cost": "14000.00", "tracked": true, "admin_graphql_api_id": "gid://shopify/InventoryItem/21681651843136" } ] }'; } }else{ return new List(); } responseList.add(inventoryItemResponse); return responseList; } global void execute(Database.BatchableContext bc , List scope){ Map meta = (Map) JSON.deserializeUntyped(scope[0]); List objectList = (List) meta.get('inventory_items'); list listofInventoryItemExist = new list(); if(inventoryItemId !='' && inventoryItemId != null){ String Inventoryquery = 'Select '+CheckAccessibility.isAccessible('id,HIC_ShopifySync__Shopify_Id__c','HIC_ShopifySync__Inventory__c') +' from HIC_ShopifySync__Inventory__c Where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Shopify_Id__c','HIC_ShopifySync__Inventory__c')+' = \''+ inventoryItemId +'\' LIMIT 49999'; listofInventoryItemExist = database.query(Inventoryquery); } String query; if(listofInventoryItemExist.isEmpty() || listofInventoryItemExist == null || listofInventoryItemExist.size() == 0){ list inventoryFieldsList = new list(); 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__Inventory__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; inventoryFieldsList = database.query(query); list inventoryListToupsert = HIC_ShopifyUtility.fillObjectValues(inventoryFieldsList,'HIC_ShopifySync__Inventory__c',objectList,configurationList); system.debug('**inventoryListToupsert--'+inventoryListToupsert); if(inventoryListToupsert.Isempty()) return; Schema.SObjectType objectType = Schema.getGlobalDescribe().get('HIC_ShopifySync__Inventory__c'); Map fieldMap = objectType.getDescribe().fields.getMap(); Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c'); if(!Test.isRunningTest()){ EmailErrorCSV.makeCsvAndSendInEmail(JSON.serialize(scope),'HIC_ShopifySync__Inventory__c',inventoryListToupsert,theField,true,store); } } String companyTimeZone = [SELECT Id, TimeZoneSidKey FROM Organization Limit 1][0].TimeZoneSidKey; Map inventoryLevelmeta = (Map) JSON.deserializeUntyped(inventoryLevelResponse); list inventoryLevelList = new list(); query = ''; 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__Is_Override__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__Inventory_Level__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; inventoryLevelList = database.query(query); String listType = 'List<' + 'HIC_ShopifySync__Inventory_Level__c' + '>'; list inventoryLevelListToupsert = (List)Type.forName(listType).newInstance(); if(!inventoryLevelList.isEmpty() && !inventoryLevelmeta.isEmpty()){ inventoryLevelListToupsert.add(HIC_ShopifyUtility.processFieldTypeAndMapping(inventoryLevelList,'HIC_ShopifySync__Inventory_Level__c',inventoryLevelmeta,configurationList,companyTimeZone)); } if(inventoryLevelListToupsert.Isempty()) return; try{ Schema.SObjectType inventoryLevelobjectType = Schema.getGlobalDescribe().get('HIC_ShopifySync__Inventory_Level__c'); Map inventoryLevelfieldMap = inventoryLevelobjectType.getDescribe().fields.getMap(); Schema.SobjectField inventoryLevelField = inventoryLevelfieldMap.get('HIC_ShopifySync__Shopify_Id__c'); EmailErrorCSV.makeCsvAndSendInEmail(JSON.serialize(scope),'HIC_ShopifySync__Inventory_Level__c',inventoryLevelListToupsert,inventoryLevelField,true,store); }catch(Exception e){ System.debug('DML EXception '+e.getMessage()+ 'Line Number '+e.getLineNumber()); } } global void finish(Database.BatchableContext bc){ } }