/** * ─────────────────────────────────────────────────────────────────────────────────────────────────┐ * This web service class is use for handle Shopify's Order webhook response. * Trigger on Order update or create. * 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 * ────────────────────────────────────────────────────────────────────────────────────────────────── */ @RestResource(urlMapping='/UpdateOrderFromShopify/*') global with sharing class UpdateOrderFromShopify { public static String generic_value1 = ''; public static string Name = ''; public static list configurationList = new list(); // B2B: cache Account/Contact IDs across processInsertOpportunity → processOrder private static Id cachedB2BAccountId; private static Id cachedB2BContactId; @HttpPost global static void updateOrder(){ RestRequest request = RestContext.request; String store = 'Default'; Map params = request.params; if(!params.isEmpty() && params.containsKey('store') && params.get('store') != null) store = params.get('store'); String query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__Account_Contact__c,HIC_ShopifySync__Enable_Draft_Order__c, HIC_ShopifySync__Enable_Opportunity_for_SDO__c, HIC_ShopifySync__Number_of_pages_for_Account__c, HIC_ShopifySync__Default_Account__c,HIC_ShopifySync__Order_Status_Activated_To_Draft__c, name, HIC_ShopifySync__Salesforce_Pricebook__c, HIC_ShopifySync__Draft_Order_Sync__c,HIC_ShopifySync__Default_Field_Mapping__c,HIC_ShopifySync__Specify_Close_Date__c,HIC_ShopifySync__Batch_Is_Processing__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__Enable_Lead__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__Order_Sync__c, HIC_ShopifySync__Opportunity_Sync__c, HIC_ShopifySync__Customer_Email_Domain__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); RestResponse response = RestContext.response; String jSONRequestBody = request.requestBody.toString().trim(); System.debug('MapofOrder: ' + JSON.serializePretty(jSONRequestBody)); //system.debug('line34-->>'+Json.serializepretty(jSONRequestBody)); CreateOrderAndOpportuity(jSONRequestBody,store); } public static void CreateOrderAndOpportuity(String jSONRequestBody, String store){ String query = ''; if((!configurationList.isempty() && configurationList[0].HIC_ShopifySync__Opportunity__c== true && configurationList[0].HIC_ShopifySync__Order__c== true) || (!configurationList.isempty() && configurationList[0].HIC_ShopifySync__Opportunity__c== true && configurationList[0].HIC_ShopifySync__Order__c== false)){ query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c, HIC_ShopifySync__IsSFID__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')+' = \'Order\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; }else if((!configurationList.isempty() && configurationList[0].HIC_ShopifySync__Opportunity__c== false && configurationList[0].HIC_ShopifySync__Order__c== true) || (!configurationList.isempty() && configurationList[0].HIC_ShopifySync__Draft_Order_Sync__c == true)){ query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c, HIC_ShopifySync__IsSFID__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')+' = \'Order\' AND Name != \'Order-OpportunityId\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; } list OrderfieldMapping = new list(); if(query != ''){ OrderfieldMapping = database.query(query); } list OpportunityfieldMapping = new list(); query = ''; query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c, HIC_ShopifySync__IsSFID__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')+' = \'Opportunity\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; OpportunityfieldMapping = database.query(query); map OrderFieldvsShopifyfield = new map(); map ShopifyfieldvsNestedfield = new map(); if(!OrderfieldMapping.isEmpty()){ for(HIC_ShopifySync__HIC_Field_Mapping__c cstm : OrderfieldMapping){ OrderFieldvsShopifyfield.put(cstm.HIC_ShopifySync__SF_Field__c,cstm.HIC_ShopifySync__Shopify_Fields__c); ShopifyfieldvsNestedfield.put(cstm.HIC_ShopifySync__Shopify_Fields__c,cstm.HIC_ShopifySync__Nested__c); } } if(!configurationList.isempty() && configurationList[0].HIC_ShopifySync__Opportunity__c== true && configurationList[0].HIC_ShopifySync__Order__c== false ){ processInsertOpportunity(jSONRequestBody,OpportunityfieldMapping,configurationList,store); } else if(!configurationList.isempty() && configurationList[0].HIC_ShopifySync__Order__c == true && configurationList[0].HIC_ShopifySync__Opportunity__c == true ){ processInsertOpportunity(jSONRequestBody,OpportunityfieldMapping,configurationList,store); processOrder(jSONRequestBody,OrderfieldMapping,configurationList,store); }else if((!configurationList.isempty() && configurationList[0].HIC_ShopifySync__Order__c == true && configurationList[0].HIC_ShopifySync__Opportunity__c == false)){ processOrder(jSONRequestBody,OrderfieldMapping,configurationList,store); } } public static void processInsertOpportunity(string body, list OpportunityfieldMapping, list configurationList, String store){ //string body = '{ "id": 618960158784, "email": "uv@gmail.com", "closed_at": null, "created_at": "2018-12-21T15:54:18+05:30", "updated_at": "2018-12-21T15:54:18+05:30", "number": 11, "note": "", "token": "7ad316e8c59b1d18877b242d8244eea3", "gateway": "manual", "test": false, "total_price": "5280.50", "subtotal_price": "4475.00", "total_weight": 195, "total_tax": "805.50", "taxes_included": false, "currency": "INR", "financial_status": "paid", "confirmed": true, "total_discounts": "0.00", "total_line_items_price": "4475.00", "cart_token": null, "buyer_accepts_marketing": false, "name": "#1011", "referring_site": null, "landing_site": null, "cancelled_at": "2018-12-21T15:54:18+05:30", "cancel_reason": "N/A", "total_price_usd": "74.70", "checkout_token": null, "reference": null, "user_id": 18963628096, "location_id": null, "source_identifier": null, "source_url": null, "processed_at": "2018-12-21T15:54:18+05:30", "device_id": null, "phone": null, "customer_locale": null, "app_id": 1354745, "browser_ip": null, "landing_site_ref": null, "order_number": 1011, "discount_applications": [], "discount_codes": [], "note_attributes": [], "payment_gateway_names": [ "manual" ], "processing_method": "manual", "checkout_id": null, "source_name": "shopify_draft_order", "fulfillment_status": null, "tax_lines": [ { "price": "805.50", "rate": 0.18, "title": "IGST", "price_set": { "shop_money": { "amount": "805.50", "currency_code": "INR" }, "presentment_money": { "amount": "805.50", "currency_code": "INR" } } } ], "tags": "", "contact_email": "uv@gmail.com", "order_status_url": "https://checkout.shopify.com/10269687872/orders/7ad316e8c59b1d18877b242d8244eea3/authenticate?key=5415e623338fd5f08b77268cc72456b6", "presentment_currency": "INR", "total_line_items_price_set": { "shop_money": { "amount": "4475.00", "currency_code": "INR" }, "presentment_money": { "amount": "4475.00", "currency_code": "INR" } }, "total_discounts_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "total_shipping_price_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "subtotal_price_set": { "shop_money": { "amount": "4475.00", "currency_code": "INR" }, "presentment_money": { "amount": "4475.00", "currency_code": "INR" } }, "total_price_set": { "shop_money": { "amount": "5280.50", "currency_code": "INR" }, "presentment_money": { "amount": "5280.50", "currency_code": "INR" } }, "total_tax_set": { "shop_money": { "amount": "805.50", "currency_code": "INR" }, "presentment_money": { "amount": "805.50", "currency_code": "INR" } }, "total_tip_received": "0.0", "admin_graphql_api_id": "gid://shopify/Order/618960158784", "line_items": [ { "id": 1468974956608, "variant_id": 17740291244096, "title": "Product", "quantity": 1, "price": "255.00", "sku": "57", "variant_title": "25gm", "vendor": "SyncMyStore", "fulfillment_service": "manual", "product_id": 1799382958144, "requires_shipping": true, "taxable": true, "gift_card": false, "name": "Product - 25gm", "variant_inventory_management": "shopify", "properties": [], "product_exists": true, "fulfillable_quantity": 1, "grams": 55, "total_discount": "0.00", "fulfillment_status": null, "price_set": { "shop_money": { "amount": "255.00", "currency_code": "INR" }, "presentment_money": { "amount": "255.00", "currency_code": "INR" } }, "total_discount_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "discount_allocations": [], "admin_graphql_api_id": "gid://shopify/LineItem/1468974956608", "tax_lines": [ { "title": "IGST", "price": "45.90", "rate": 0.18, "price_set": { "shop_money": { "amount": "45.90", "currency_code": "INR" }, "presentment_money": { "amount": "45.90", "currency_code": "INR" } } } ] }, { "id": 1468974989376, "variant_id": 17740291342400, "title": "Product", "quantity": 2, "price": "535.00", "sku": "58", "variant_title": "50gm", "vendor": "SyncMyStore", "fulfillment_service": "manual", "product_id": 1799382958144, "requires_shipping": true, "taxable": true, "gift_card": false, "name": "Product - 50gm", "variant_inventory_management": "shopify", "properties": [], "product_exists": true, "fulfillable_quantity": 2, "grams": 55, "total_discount": "0.00", "fulfillment_status": null, "price_set": { "shop_money": { "amount": "535.00", "currency_code": "INR" }, "presentment_money": { "amount": "535.00", "currency_code": "INR" } }, "total_discount_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "discount_allocations": [], "admin_graphql_api_id": "gid://shopify/LineItem/1468974989376", "tax_lines": [ { "title": "IGST", "price": "192.60", "rate": 0.18, "price_set": { "shop_money": { "amount": "192.60", "currency_code": "INR" }, "presentment_money": { "amount": "192.60", "currency_code": "INR" } } } ] }, { "id": 1468975022144, "variant_id": 17732618125376, "title": "TestProduct", "quantity": 3, "price": "550.00", "sku": "50506", "variant_title": "5mm / black", "vendor": "SyncMyStore", "fulfillment_service": "manual", "product_id": 1798809452608, "requires_shipping": true, "taxable": true, "gift_card": false, "name": "TestProduct - 5mm / black", "variant_inventory_management": "shopify", "properties": [], "product_exists": true, "fulfillable_quantity": 3, "grams": 5, "total_discount": "0.00", "fulfillment_status": null, "price_set": { "shop_money": { "amount": "550.00", "currency_code": "INR" }, "presentment_money": { "amount": "550.00", "currency_code": "INR" } }, "total_discount_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "discount_allocations": [], "admin_graphql_api_id": "gid://shopify/LineItem/1468975022144", "tax_lines": [ { "title": "IGST", "price": "297.00", "rate": 0.18, "price_set": { "shop_money": { "amount": "297.00", "currency_code": "INR" }, "presentment_money": { "amount": "297.00", "currency_code": "INR" } } } ] }, { "id": 1468975054912, "variant_id": 17732618027072, "title": "TestProduct", "quantity": 3, "price": "500.00", "sku": "50505", "variant_title": "5mm / red", "vendor": "SyncMyStore", "fulfillment_service": "manual", "product_id": 1798809452608, "requires_shipping": true, "taxable": true, "gift_card": false, "name": "TestProduct - 5mm / red", "variant_inventory_management": "shopify", "properties": [], "product_exists": true, "fulfillable_quantity": 3, "grams": 5, "total_discount": "0.00", "fulfillment_status": null, "price_set": { "shop_money": { "amount": "500.00", "currency_code": "INR" }, "presentment_money": { "amount": "500.00", "currency_code": "INR" } }, "total_discount_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "discount_allocations": [], "admin_graphql_api_id": "gid://shopify/LineItem/1468975054912", "tax_lines": [ { "title": "IGST", "price": "270.00", "rate": 0.18, "price_set": { "shop_money": { "amount": "270.00", "currency_code": "INR" }, "presentment_money": { "amount": "270.00", "currency_code": "INR" } } } ] } ], "shipping_lines": [], "billing_address": { "first_name": "firstname1", "address1": "Noida City Centre Metro Sector 39", "phone": null, "city": "Noida", "zip": "201301", "province": "Uttar Pradesh", "country": "India", "last_name": "lastname1", "address2": "noida", "company": "test", "latitude": 28.57447, "longitude": 77.35561, "name": "firstname1 lastname1", "country_code": "IN", "province_code": "UP" }, "shipping_address": { "first_name": "firstname1", "address1": "Noida City Centre Metro Sector 39", "phone": null, "city": "Noida", "zip": "201301", "province": "Uttar Pradesh", "country": "India", "last_name": "lastname1", "address2": "noida", "company": "test", "latitude": 28.57447, "longitude": 77.35561, "name": "firstname1 lastname1", "country_code": "IN", "province_code": "UP" }, "fulfillments": [], "refunds": [], "customer": { "id": 926016995392, "email": "uv@gmail.com", "accepts_marketing": false, "created_at": "2018-12-12T16:43:59+05:30", "updated_at": "2018-12-21T15:54:18+05:30", "first_name": "firstname1", "last_name": "lastname1", "orders_count": 5, "state": "disabled", "total_spent": "18732.50", "last_order_id": 618960158784, "note": "Noteee", "verified_email": true, "multipass_identifier": null, "tax_exempt": false, "phone": "+918604065624", "tags": "Tagsss", "last_order_name": "#1011", "currency": "INR", "admin_graphql_api_id": "gid://shopify/Customer/926016995392", "default_address": { "id": 1041332338752, "customer_id": 926016995392, "first_name": "firstname1", "last_name": "lastname1", "company": "test", "address1": "Noida City Centre Metro Sector 39", "address2": "noida", "city": "Noida", "province": "Uttar Pradesh", "country": "India", "zip": "201301", "phone": "", "name": "firstname1 lastname1", "province_code": "UP", "country_code": "IN", "country_name": "India", "default": true } } } '; String presentmentCurrency = ''; Map objectMap = (Map) JSON.deserializeUntyped(body); Map accMap = new Map(); if(objectMap != null && !objectMap.isEmpty() && objectMap.containsKey('customer') && objectMap.get('customer') != null){ accMap = (Map)objectMap.get('customer'); } else{ System.debug('check-->'); accMap.put('id', 'Missing Account'); objectMap.put('customer', accMap); } if(objectMap != null && !objectMap.isEmpty() && objectMap.containsKey('company') && objectMap.get('company') != null){ //accMap = (Map)objectMap.get('company'); } else{ System.debug('check-->'); Map companyMap = new Map(); companyMap.put('id', 'business missing account'); objectMap.put('company', companyMap); } if (objectMap != null && !objectMap.isEmpty() && objectMap.containsKey('presentment_currency') && objectMap.get('presentment_currency') != null) { system.debug('check presentment value: ' + objectMap.get('presentment_currency')); presentmentCurrency = (String) objectMap.get('presentment_currency'); system.debug('presentmentCurrency===>'+presentmentCurrency); } Map ShopifyIdvsAccountId = new Map(); list OpportunityLinelist = new list(); list listOfexistingProductOpplineitem = new list(); String companyTimeZone = [SELECT Id, TimeZoneSidKey FROM Organization Limit 1][0].TimeZoneSidKey; if(Schema.sObjectType.Account.fields.getMap().containsKey( 'ispersonaccount' )){ if (configurationList[0].HIC_ShopifySync__Person_Account__c ){ if(accMap != null && !accMap.isEmpty() && objectMap != null && !objectMap.isEmpty()) if(!(String.valueOf(accMap.get('id'))).contains('Person ')) //objectMap.put('id','Person Account'+String.valueOf(objectMap.get('id'))); ShopifyIdvsAccountId.put('Person '+String.valueof(accMap.get('id')),objectMap.get('customer')); }else if(!configurationList[0].HIC_ShopifySync__Person_Account__c ){ if(accMap != null && !accMap.isEmpty() && objectMap != null && !objectMap.isEmpty()) if(!(String.valueOf(accMap.get('id'))).contains('Business ')) //objectMap.put('id','Business Account'+String.valueOf(objectMap.get('id'))); ShopifyIdvsAccountId.put('Business '+String.valueof(accMap.get('id')),objectMap.get('customer')); } }else{ if(accMap != null && !accMap.isEmpty() && objectMap != null && !objectMap.isEmpty() && !(String.valueOf(objectMap.get('id'))).contains('Business ') && objectMap.containsKey('customer')) ShopifyIdvsAccountId.put('Business '+String.valueof(accMap.get('id')),objectMap.get('customer')); } //ShopifyIdvsAccountId.put(String.valueof(accMap.get('id')),'false'); /*if(!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Person_Account__c== true){ HIC_ShopifyUtility.InitializeAccount(ShopifyIdvsAccountId, 'Account',true, configurationList); }else{ HIC_ShopifyUtility.InitializeAccount(ShopifyIdvsAccountId, 'Account',false,configurationList ); }*/ if (!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Customer_Email_Domain__c == true) { // B2B: Resolve Account by email domain String email = (accMap.containsKey('email') && accMap.get('email') != null) ? String.valueOf(accMap.get('email')) : (objectMap.containsKey('email') ? String.valueOf(objectMap.get('email')) : null); if (String.isNotBlank(email) && email.contains('@')) { String domain = email.substringAfter('@').substringBefore('.'); List existingAccounts = [SELECT Id, Name FROM Account WHERE Name = :domain LIMIT 1]; Id b2bAccountId; if (!existingAccounts.isEmpty()) { b2bAccountId = existingAccounts[0].Id; } else { Account acc = new Account(Name = domain); insert acc; b2bAccountId = acc.Id; } // Find Contact by Shopify ID Id b2bContactId; String customerId = String.valueOf(accMap.get('id')); String shopifyIdToSearch = 'Business ' + customerId; List contacts = [SELECT Id FROM Contact WHERE HIC_ShopifySync__Shopify_Id__c = :shopifyIdToSearch LIMIT 1]; if (!contacts.isEmpty()) { b2bContactId = contacts[0].Id; } // Store for later use after Opp/Order build objectMap.put('b2bAccountId', b2bAccountId); if (b2bContactId != null) objectMap.put('b2bContactId', b2bContactId); // Cache for processOrder reuse cachedB2BAccountId = b2bAccountId; cachedB2BContactId = b2bContactId; } // Skip normal InitializeAccount for B2B } else { if(accMap != null && !accMap.isEmpty()){ HIC_ShopifyUtility.InitializeAccount(ShopifyIdvsAccountId, configurationList, store); } } System.debug('ShopifyIdvsAccountId-->'+ShopifyIdvsAccountId); // ── Auto-convert Lead after Account/Contact upsert (only when Lead feature is ON) ── if (!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Enable_Lead__c) { String customerEmail = (String) accMap.get('email'); String shopifyCustomerId = String.valueOf(accMap.get('id')); String shopifyIdPattern = '%' + shopifyCustomerId; if (String.isNotBlank(customerEmail) && String.isNotBlank(shopifyCustomerId)) { List existingLead = [ SELECT Id, Email, HIC_ShopifySync__Shopify_Id__c FROM Lead WHERE HIC_ShopifySync__Shopify_Id__c = :shopifyCustomerId AND IsConverted = false LIMIT 1 ]; if (!existingLead.isEmpty()) { List matchingContact = new List(); // B2B: search Contact by 'Business ' + customerId (exact match) if (!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Customer_Email_Domain__c == true) { String b2bShopifyId = 'Business ' + shopifyCustomerId; matchingContact = [ SELECT Id, AccountId, HIC_ShopifySync__Shopify_Id__c FROM Contact WHERE HIC_ShopifySync__Shopify_Id__c = :b2bShopifyId LIMIT 1 ]; } else { matchingContact = [ SELECT Id, AccountId, HIC_ShopifySync__Shopify_Id__c FROM Contact WHERE Email = :customerEmail AND HIC_ShopifySync__Shopify_Id__c LIKE :shopifyIdPattern ORDER BY CreatedDate DESC LIMIT 1 ]; } if (!matchingContact.isEmpty()) { try { Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(existingLead[0].Id); lc.setAccountId(matchingContact[0].AccountId); lc.setContactId(matchingContact[0].Id); lc.setDoNotCreateOpportunity(true); lc.setConvertedStatus('Closed - Converted'); Database.convertLead(lc); } catch (Exception ex) { System.debug('Auto-convert exception: ' + ex.getMessage()); } } } } } try{ String listType = 'List<' + 'Opportunity' + '>'; list sobjListToUpsert = (List)Type.forName(listType).newInstance(); sobjListToUpsert.add(HIC_ShopifyUtility.processFieldTypeAndMapping(OpportunityfieldMapping,'Opportunity',objectMap,configurationList,companyTimeZone)); // B2B: Override Account and Contact on Opportunity if (!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Customer_Email_Domain__c == true && !sobjListToUpsert.isEmpty()) { SObject opp = sobjListToUpsert[0]; if (objectMap.containsKey('b2bAccountId') && objectMap.get('b2bAccountId') != null) { opp.putSObject('Account', null); opp.put('AccountId', (Id) objectMap.get('b2bAccountId')); if (objectMap.containsKey('b2bContactId') && objectMap.get('b2bContactId') != null) { opp.put('HIC_ShopifySync__Contact__c', (Id) objectMap.get('b2bContactId')); } } } system.debug('***sobjListToUpsert'+sobjListToUpsert); Type typLstSObject = Type.ForName('List'); List OpportunityLineItemListToupsert = (List)typLstSObject.NewInstance(); if(!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Opportunity_Line_Item__c== true){ list OpportunityLineItemFieldsList = new list(); String query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c, HIC_ShopifySync__IsSFID__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')+' = \'OpportunityLineItem\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; OpportunityLineItemFieldsList = database.query(query); map productVsPricebookEntryMap = new map(); map productVsUnitepriceMap = new map(); map UpdateproductVsPricebookEntryMap = new map(); map shopifyOrderIdVsExistMap = new map(); shopifyOrderIdVsExistMap.put(String.valueof(objectMap.get('id')),false); if(Schema.sObjectType.Opportunity.isAccessible() && Schema.sObjectType.Opportunity.fields.HIC_ShopifySync__Shopify_Id__c.isAccessible()){ for(Opportunity Opp : [Select HIC_ShopifySync__Shopify_Id__c from Opportunity Where HIC_ShopifySync__Shopify_Id__c IN: shopifyOrderIdVsExistMap.keyset()]){ shopifyOrderIdVsExistMap.put(Opp.HIC_ShopifySync__Shopify_Id__c, true); } } if(objectMap != null && !objectMap.isEmpty() && objectMap.containskey('line_items') ){ /* OpportunityLineItem Delete */ String OrderId = string.valueOf(objectMap.get('id')); list listtodeleteOrder = new list(); String Orderquery = 'Select '+CheckAccessibility.isAccessible('id,HIC_ShopifySync__Shopify_Id__c','opportunity') +' from opportunity Where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Shopify_Id__c','opportunity')+' = \''+String.escapeSingleQuotes(OrderId)+'\' limit 1' ; String query3 = ''; Boolean multiCurrencyEnabled = Schema.getGlobalDescribe().containsKey('CurrencyType'); if(multiCurrencyEnabled){ query3 = 'Select Pricebook2id, CurrencyIsoCode from opportunity where HIC_ShopifySync__Shopify_Id__c =: OrderId'; }else{ query3 = 'Select Pricebook2id from opportunity where HIC_ShopifySync__Shopify_Id__c =: OrderId'; } List currencyMappingList = new List(); String query2 = ''; if(multiCurrencyEnabled){ query2 = ' select '+CheckAccessibility.isAccessible('id,name,HIC_ShopifySync__Store_External_ID__c,HIC_ShopifySync__External_ID__c,HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c , HIC_ShopifySync__IsSFID__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' from HIC_ShopifySync__HIC_Field_Mapping__c where ' +CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c','HIC_ShopifySync__HIC_Field_Mapping__c') +'= \'CurrencyISOCODE\' AND ' +CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c', 'HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; currencyMappingList = database.query(query2); } List orderList = database.query(query3); /*Boolean tempFlag = false; for(Opportunity ordr : orderList){ if(ordr.Pricebook2id != configurationList[0].HIC_ShopifySync__Salesforce_Pricebook__c || (multiCurrencyEnabled && !currencyMappingList.isEmpty() && ordr.get('CurrencyIsoCode') != currencyMappingList[0].HIC_ShopifySync__Shopify_Fields__c)){ tempFlag = true; } } if(tempFlag){ listtodeleteOrder= database.query(Orderquery); } list listtodeleteOrderitem = new list(); if(!listtodeleteOrder.isEmpty()){ String OrderSFid= listtodeleteOrder[0].id; String Orderitemquery = 'Select '+CheckAccessibility.isAccessible('id,HIC_ShopifySync__Shopify_Id__c','OpportunityLineItem') +' from OpportunityLineItem Where '+CheckAccessibility.isAccessible('OpportunityId','OpportunityLineItem')+' = \''+OrderSFid+'\' limit 51231212' ; listtodeleteOrderitem = database.query(Orderitemquery); } try{ if(!listtodeleteOrderitem.isEmpty()){ if(CheckAccessibility.isDeletable('OpportunityLineItem')) // database.delete (listtodeleteOrderitem,false); } }catch(Exception e){ system.debug('Error: '+e.getMessage()); }*/ /* OpportunityLineItem Delete End*/ OpportunityLinelist = (list)objectMap.get('line_items'); System.debug('OpportunityLinelist-->'+OpportunityLinelist); for(Object oProdList : OpportunityLinelist){ Map oProdmap = (Map)oProdList; if(oProdmap.get('product_id') == null) oProdmap.put('product_id', 'N/A'); if(oProdmap.get('variant_id') == null) oProdmap.put('variant_id', 'N/A'); //updated After 1st release; // if(!oprodMap.isEmpty() && oprodMap.containsKey('product_exists') && oprodMap.get('product_exists') != null && String.valueof(oprodMap.get('product_exists')).equalsIgnoreCase('false')) continue; productVsPricebookEntryMap.put(String.valueof(oProdmap.get('product_id')),null); productVsUnitepriceMap.put(String.valueof(oProdmap.get('product_id')),String.valueof(oProdmap.get('price'))); oProdmap.put('opportunityId',string.valueOf(objectMap.get('id'))); /* if(oProdmap.get('product_id') == 'N/A' && oProdmap.get('variant_id') == 'N/A') oProdmap.put('id',String.valueOf(objectMap.get('id')) + OpportunityLinelist.indexOf(oProdList)); else oProdmap.put('id',String.valueOf(objectMap.get('id')) + String.valueof(oProdmap.get('variant_id')) + String.valueOf(oProdmap.get('product_id'))); */ //updated After 1st release; //listOfexistingProductOpplineitem.add(oProdList); } if(presentmentCurrency!=null && presentmentCurrency!=' '){ UpdateproductVsPricebookEntryMap = new Map(HIC_ShopifyUtility.InitializePricebookEntryIdorder(productVsPricebookEntryMap,productVsUnitepriceMap,configurationList,presentmentCurrency,store)); }else{ UpdateproductVsPricebookEntryMap = new Map(HIC_ShopifyUtility.InitializePricebookEntryId(productVsPricebookEntryMap,productVsUnitepriceMap,configurationList,store)); } // map UpdateproductVsPricebookEntryMap = new map(HIC_ShopifyUtility.InitializePricebookEntryIdorder(productVsPricebookEntryMap,productVsUnitepriceMap,configurationList,presentmentCurrency,store)); //if(productVsPricebookEntryMap2.containskey(string.valueOf(oProdmap.get('product_id')))) for(Object oProdList : OpportunityLinelist){ Map oProdmap = (Map)oProdList; if(oProdmap.get('product_id') == null) oProdmap.put('product_id', 'N/A'); if(oProdmap.get('variant_id') == null) oProdmap.put('variant_id', 'N/A'); //updated After 1st release; // if(!oprodMap.isEmpty() && oprodMap.containsKey('product_exists') && oprodMap.get('product_exists') != null && String.valueof(oprodMap.get('product_exists')).equalsIgnoreCase('false')) continue; oProdmap.put('PricebookEntryId',string.valueOf(UpdateproductVsPricebookEntryMap.get(string.valueOf(oProdmap.get('product_id'))))); oProdmap.put('UnitPrice',oProdmap.get('price')); oProdList = (Object)oProdmap; //updated After 1st release; if(oProdmap.containsKey('PricebookEntryId') && oProdmap.get('PricebookEntryId') != null && String.isNotBlank(String.valueof(oProdmap.get('PricebookEntryId')))) listOfexistingProductOpplineitem.add(oProdList); } HIC_ShopifyUtility.initializeProductandVaraint(OpportunityLinelist); } //updated After 1st release; OpportunityLineItemListToupsert.addAll(HIC_ShopifyUtility.fillObjectValues(OpportunityLineItemFieldsList,'OpportunityLineItem',listOfexistingProductOpplineitem,configurationList)); } if(!sobjListToUpsert.Isempty()){ Schema.SObjectType objectType = Schema.getGlobalDescribe().get('Opportunity'); Map fieldMap = objectType.getDescribe().fields.getMap(); Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c'); if (configurationList[0].HIC_ShopifySync__Draft_Order_Sync__c == true){ if (configurationList[0].HIC_ShopifySync__Enable_Draft_Order__c == true) { String shopifyOrderId = String.valueOf(objectMap.get('id')); List draftOrders = [ SELECT Id, HIC_ShopifySync__Shopify_Id__c, HIC_ShopifySync__Shopify_Order_ID__c FROM Order WHERE HIC_ShopifySync__Shopify_Order_ID__c = :shopifyOrderId AND IsDeleted = FALSE LIMIT 1 ]; if (!draftOrders.isEmpty()) { for (SObject ord : sobjListToUpsert) { ord.put('HIC_ShopifySync__Shopify_Draft_Order_ID__c', draftOrders[0].HIC_ShopifySync__Shopify_Id__c); } } } else if(configurationList[0].HIC_ShopifySync__Enable_Opportunity_for_SDO__c == true) { String shopifyOrderId = String.valueOf(objectMap.get('id')); List draftOpps = [ SELECT Id, HIC_ShopifySync__Shopify_Id__c, HIC_ShopifySync__Shopify_Order_ID__c FROM Opportunity WHERE HIC_ShopifySync__Shopify_Order_ID__c = :shopifyOrderId AND IsDeleted = FALSE LIMIT 1 ]; if (!draftOpps.isEmpty()) { for (SObject ord : sobjListToUpsert) { ord.put('HIC_ShopifySync__Shopify_Draft_Order_ID__c', draftOpps[0].HIC_ShopifySync__Shopify_Id__c); } } } } EmailErrorCSV.makeCsvAndSendInEmail(body,'Opportunity',sobjListToUpsert,theField,true,store); } system.debug('**!OpportunityLineItemListToupsert'+OpportunityLineItemListToupsert); if(!OpportunityLineItemListToupsert.isEmpty()){ system.debug('**OpportunityLineItemListToupsert--2'+OpportunityLineItemListToupsert); Schema.SObjectType objectType = Schema.getGlobalDescribe().get('OpportunityLineItem'); Map fieldMap = objectType.getDescribe().fields.getMap(); Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c'); EmailErrorCSV.makeCsvAndSendInEmail(body,'OpportunityLineItem',OpportunityLineItemListToupsert,theField,true,store); } }catch(Exception e){ CheckAccessibility.isAccessible('name,HIC_ShopifySync__Missing_Order_Info__c,HIC_ShopifySync__Shopify_id__c,', 'HIC_ShopifySync__Missing_Order__c'); if(CheckAccessibility.isInsertable('name, HIC_ShopifySync__Missing_Order_Info__c, HIC_ShopifySync__Shopify_id__c,', 'HIC_ShopifySync__Missing_Order__c')){ HIC_ShopifySync__Missing_Order__c missingOrder = new HIC_ShopifySync__Missing_Order__c(); missingOrder.name = string.valueof(objectMap.get('order_number')); String jSONRequestBody = body; if(jSONRequestBody!= null && jSONRequestBody != ''){ Integer lengthofjSONRequestBody = jSONRequestBody.length(); if(lengthofjSONRequestBody != null && lengthofjSONRequestBody != 0 && lengthofjSONRequestBody < 131072){ missingOrder.HIC_ShopifySync__Missing_Order_Info__c = jSONRequestBody; }else if(lengthofjSONRequestBody > 131072 && lengthofjSONRequestBody <= 262144){ missingOrder.HIC_ShopifySync__Missing_Order_Info__c = jSONRequestBody.substring(0,131072); missingOrder.HIC_ShopifySync__Missing_Order_Second_Info__c =jSONRequestBody.substring(131072,lengthofjSONRequestBody); }else if(lengthofjSONRequestBody > 262144){ missingOrder.HIC_ShopifySync__Missing_Order_Info__c = jSONRequestBody.substring(0,131072); missingOrder.HIC_ShopifySync__Missing_Order_Second_Info__c =jSONRequestBody.substring(131072,lengthofjSONRequestBody); missingOrder.HIC_ShopifySync__Missing_Order_Third_Info__c = jSONRequestBody.substring(262144,lengthofjSONRequestBody); } } missingOrder.HIC_ShopifySync__Shopify_id__c = String.valueof(objectMap.get('id')); database.insert(missingOrder); } } if(configurationList[0].HIC_ShopifySync__Order_Fulfillment__c== true && configurationList[0].HIC_ShopifySync__Order__c == false && body != null && body != '') insertOrderFulfillments(body,store); } public static void processOrder(string body, list OrderfieldMapping ,list configurationList, String store){ //string body = '{ "id": 618960158784, "email": "uv@gmail.com", "closed_at": null, "created_at": "2018-12-21T15:54:18+05:30", "updated_at": "2018-12-21T15:54:18+05:30", "number": 11, "note": "", "token": "7ad316e8c59b1d18877b242d8244eea3", "gateway": "manual", "test": false, "total_price": "5280.50", "subtotal_price": "4475.00", "total_weight": 195, "total_tax": "805.50", "taxes_included": false, "currency": "INR", "financial_status": "paid", "confirmed": true, "total_discounts": "0.00", "total_line_items_price": "4475.00", "cart_token": null, "buyer_accepts_marketing": false, "name": "#1011", "referring_site": null, "landing_site": null, "cancelled_at": "2018-12-21T15:54:18+05:30", "cancel_reason": "N/A", "total_price_usd": "74.70", "checkout_token": null, "reference": null, "user_id": 18963628096, "location_id": null, "source_identifier": null, "source_url": null, "processed_at": "2018-12-21T15:54:18+05:30", "device_id": null, "phone": null, "customer_locale": null, "app_id": 1354745, "browser_ip": null, "landing_site_ref": null, "order_number": 1011, "discount_applications": [], "discount_codes": [], "note_attributes": [], "payment_gateway_names": [ "manual" ], "processing_method": "manual", "checkout_id": null, "source_name": "shopify_draft_order", "fulfillment_status": null, "tax_lines": [ { "price": "805.50", "rate": 0.18, "title": "IGST", "price_set": { "shop_money": { "amount": "805.50", "currency_code": "INR" }, "presentment_money": { "amount": "805.50", "currency_code": "INR" } } } ], "tags": "", "contact_email": "uv@gmail.com", "order_status_url": "https://checkout.shopify.com/10269687872/orders/7ad316e8c59b1d18877b242d8244eea3/authenticate?key=5415e623338fd5f08b77268cc72456b6", "presentment_currency": "INR", "total_line_items_price_set": { "shop_money": { "amount": "4475.00", "currency_code": "INR" }, "presentment_money": { "amount": "4475.00", "currency_code": "INR" } }, "total_discounts_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "total_shipping_price_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "subtotal_price_set": { "shop_money": { "amount": "4475.00", "currency_code": "INR" }, "presentment_money": { "amount": "4475.00", "currency_code": "INR" } }, "total_price_set": { "shop_money": { "amount": "5280.50", "currency_code": "INR" }, "presentment_money": { "amount": "5280.50", "currency_code": "INR" } }, "total_tax_set": { "shop_money": { "amount": "805.50", "currency_code": "INR" }, "presentment_money": { "amount": "805.50", "currency_code": "INR" } }, "total_tip_received": "0.0", "admin_graphql_api_id": "gid://shopify/Order/618960158784", "line_items": [ { "id": 1468974956608, "variant_id": 17740291244096, "title": "Product", "quantity": 1, "price": "255.00", "sku": "57", "variant_title": "25gm", "vendor": "SyncMyStore", "fulfillment_service": "manual", "product_id": 1799382958144, "requires_shipping": true, "taxable": true, "gift_card": false, "name": "Product - 25gm", "variant_inventory_management": "shopify", "properties": [], "product_exists": true, "fulfillable_quantity": 1, "grams": 55, "total_discount": "0.00", "fulfillment_status": null, "price_set": { "shop_money": { "amount": "255.00", "currency_code": "INR" }, "presentment_money": { "amount": "255.00", "currency_code": "INR" } }, "total_discount_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "discount_allocations": [], "admin_graphql_api_id": "gid://shopify/LineItem/1468974956608", "tax_lines": [ { "title": "IGST", "price": "45.90", "rate": 0.18, "price_set": { "shop_money": { "amount": "45.90", "currency_code": "INR" }, "presentment_money": { "amount": "45.90", "currency_code": "INR" } } } ] }, { "id": 1468974989376, "variant_id": 17740291342400, "title": "Product", "quantity": 2, "price": "535.00", "sku": "58", "variant_title": "50gm", "vendor": "SyncMyStore", "fulfillment_service": "manual", "product_id": 1799382958144, "requires_shipping": true, "taxable": true, "gift_card": false, "name": "Product - 50gm", "variant_inventory_management": "shopify", "properties": [], "product_exists": true, "fulfillable_quantity": 2, "grams": 55, "total_discount": "0.00", "fulfillment_status": null, "price_set": { "shop_money": { "amount": "535.00", "currency_code": "INR" }, "presentment_money": { "amount": "535.00", "currency_code": "INR" } }, "total_discount_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "discount_allocations": [], "admin_graphql_api_id": "gid://shopify/LineItem/1468974989376", "tax_lines": [ { "title": "IGST", "price": "192.60", "rate": 0.18, "price_set": { "shop_money": { "amount": "192.60", "currency_code": "INR" }, "presentment_money": { "amount": "192.60", "currency_code": "INR" } } } ] }, { "id": 1468975022144, "variant_id": 17732618125376, "title": "TestProduct", "quantity": 3, "price": "550.00", "sku": "50506", "variant_title": "5mm / black", "vendor": "SyncMyStore", "fulfillment_service": "manual", "product_id": 1798809452608, "requires_shipping": true, "taxable": true, "gift_card": false, "name": "TestProduct - 5mm / black", "variant_inventory_management": "shopify", "properties": [], "product_exists": true, "fulfillable_quantity": 3, "grams": 5, "total_discount": "0.00", "fulfillment_status": null, "price_set": { "shop_money": { "amount": "550.00", "currency_code": "INR" }, "presentment_money": { "amount": "550.00", "currency_code": "INR" } }, "total_discount_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "discount_allocations": [], "admin_graphql_api_id": "gid://shopify/LineItem/1468975022144", "tax_lines": [ { "title": "IGST", "price": "297.00", "rate": 0.18, "price_set": { "shop_money": { "amount": "297.00", "currency_code": "INR" }, "presentment_money": { "amount": "297.00", "currency_code": "INR" } } } ] }, { "id": 1468975054912, "variant_id": 17732618027072, "title": "TestProduct", "quantity": 3, "price": "500.00", "sku": "50505", "variant_title": "5mm / red", "vendor": "SyncMyStore", "fulfillment_service": "manual", "product_id": 1798809452608, "requires_shipping": true, "taxable": true, "gift_card": false, "name": "TestProduct - 5mm / red", "variant_inventory_management": "shopify", "properties": [], "product_exists": true, "fulfillable_quantity": 3, "grams": 5, "total_discount": "0.00", "fulfillment_status": null, "price_set": { "shop_money": { "amount": "500.00", "currency_code": "INR" }, "presentment_money": { "amount": "500.00", "currency_code": "INR" } }, "total_discount_set": { "shop_money": { "amount": "0.00", "currency_code": "INR" }, "presentment_money": { "amount": "0.00", "currency_code": "INR" } }, "discount_allocations": [], "admin_graphql_api_id": "gid://shopify/LineItem/1468975054912", "tax_lines": [ { "title": "IGST", "price": "270.00", "rate": 0.18, "price_set": { "shop_money": { "amount": "270.00", "currency_code": "INR" }, "presentment_money": { "amount": "270.00", "currency_code": "INR" } } } ] } ], "shipping_lines": [], "billing_address": { "first_name": "firstname1", "address1": "Noida City Centre Metro Sector 39", "phone": null, "city": "Noida", "zip": "201301", "province": "Uttar Pradesh", "country": "India", "last_name": "lastname1", "address2": "noida", "company": "test", "latitude": 28.57447, "longitude": 77.35561, "name": "firstname1 lastname1", "country_code": "IN", "province_code": "UP" }, "shipping_address": { "first_name": "firstname1", "address1": "Noida City Centre Metro Sector 39", "phone": null, "city": "Noida", "zip": "201301", "province": "Uttar Pradesh", "country": "India", "last_name": "lastname1", "address2": "noida", "company": "test", "latitude": 28.57447, "longitude": 77.35561, "name": "firstname1 lastname1", "country_code": "IN", "province_code": "UP" }, "fulfillments": [], "refunds": [], "customer": { "id": 926016995392, "email": "uv@gmail.com", "accepts_marketing": false, "created_at": "2018-12-12T16:43:59+05:30", "updated_at": "2018-12-21T15:54:18+05:30", "first_name": "firstname1", "last_name": "lastname1", "orders_count": 5, "state": "disabled", "total_spent": "18732.50", "last_order_id": 618960158784, "note": "Noteee", "verified_email": true, "multipass_identifier": null, "tax_exempt": false, "phone": "+918604065624", "tags": "Tagsss", "last_order_name": "#1011", "currency": "INR", "admin_graphql_api_id": "gid://shopify/Customer/926016995392", "default_address": { "id": 1041332338752, "customer_id": 926016995392, "first_name": "firstname1", "last_name": "lastname1", "company": "test", "address1": "Noida City Centre Metro Sector 39", "address2": "noida", "city": "Noida", "province": "Uttar Pradesh", "country": "India", "zip": "201301", "phone": "", "name": "firstname1 lastname1", "province_code": "UP", "country_code": "IN", "country_name": "India", "default": true } } } '; String presentmentCurrency = ''; Map objectMap = (Map) JSON.deserializeUntyped(body); Map accMap = new Map(); if(objectMap != null && !objectMap.isEmpty() && objectMap.containsKey('customer') && objectMap.get('customer') != null){ accMap = (Map)objectMap.get('customer'); } else{ System.debug('check-->'); accMap.put('id', 'Missing Account'); objectMap.put('customer', accMap); } if(objectMap != null && !objectMap.isEmpty() && objectMap.containsKey('company') && objectMap.get('company') != null){ //accMap = (Map)objectMap.get('company'); } else{ System.debug('check-->'); Map companyMap = new Map(); companyMap.put('id', 'business missing account'); objectMap.put('company', companyMap); } String companyTimeZone = [SELECT Id, TimeZoneSidKey FROM Organization Limit 1][0].TimeZoneSidKey; if(!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Opportunity_Line_Item__c== true){ objectMap.put('Opportunityid',objectMap.get('id')); } Map ShopifyIdvsAccountId = new Map(); if(Schema.sObjectType.Account.fields.getMap().containsKey( 'ispersonaccount' )){ if (configurationList[0].HIC_ShopifySync__Person_Account__c ){ if(accMap != null && !accMap.isEmpty() && objectMap != null && !objectMap.isEmpty()) if(!(String.valueOf(accMap.get('id'))).contains('Person ')) //objectMap.put('id','Person Account'+String.valueOf(objectMap.get('id'))); ShopifyIdvsAccountId.put('Person '+String.valueof(accMap.get('id')),objectMap.get('customer')); }else if(!configurationList[0].HIC_ShopifySync__Person_Account__c ){ if(accMap != null && !accMap.isEmpty() && objectMap != null && !objectMap.isEmpty()) if(!(String.valueOf(accMap.get('id'))).contains('Business ')) //objectMap.put('id','Business Account'+String.valueOf(objectMap.get('id'))); ShopifyIdvsAccountId.put('Business '+String.valueof(accMap.get('id')),objectMap.get('customer')); } }else{ if(accMap != null && !accMap.isEmpty() && objectMap != null && !objectMap.isEmpty() && !(String.valueOf(objectMap.get('id'))).contains('Business ') && objectMap.containsKey('customer')) //objectMap.put('id','Business Account'+String.valueOf(objectMap.get('id'))); ShopifyIdvsAccountId.put('Business '+String.valueof(accMap.get('id')),objectMap.get('customer')); } if (objectMap != null && !objectMap.isEmpty() && objectMap.containsKey('presentment_currency') && objectMap.get('presentment_currency') != null) { system.debug('check presentment value: ' + objectMap.get('presentment_currency')); presentmentCurrency = (String) objectMap.get('presentment_currency'); system.debug('presentmentCurrency===>'+presentmentCurrency); } //ShopifyIdvsAccountId.put(String.valueof(accMap.get('id')),'false'); /*if(!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Person_Account__c== true){ HIC_ShopifyUtility.InitializeAccount(ShopifyIdvsAccountId, 'Account',true, configurationList); }else{ HIC_ShopifyUtility.InitializeAccount(ShopifyIdvsAccountId, 'Account',false, configurationList); }*/ if (!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Customer_Email_Domain__c == true) { // B2B: Reuse cached Account/Contact from processInsertOpportunity if available if (cachedB2BAccountId != null) { objectMap.put('b2bAccountId', cachedB2BAccountId); if (cachedB2BContactId != null) { objectMap.put('b2bContactId', cachedB2BContactId); } } else { // Fallback: resolve by domain (only if processOrder runs solo) String email = (accMap.containsKey('email') && accMap.get('email') != null) ? String.valueOf(accMap.get('email')) : (objectMap.containsKey('email') ? String.valueOf(objectMap.get('email')) : null); if (String.isNotBlank(email) && email.contains('@')) { String domain = email.substringAfter('@').substringBefore('.'); List existingAccounts = [SELECT Id, Name FROM Account WHERE Name = :domain LIMIT 1]; Id b2bAccountId; if (!existingAccounts.isEmpty()) { b2bAccountId = existingAccounts[0].Id; } else { Account acc = new Account(Name = domain); insert acc; b2bAccountId = acc.Id; } Id b2bContactId; String customerId = String.valueOf(accMap.get('id')); String shopifyIdToSearch = 'Business ' + customerId; List contacts = [SELECT Id FROM Contact WHERE HIC_ShopifySync__Shopify_Id__c = :shopifyIdToSearch LIMIT 1]; if (!contacts.isEmpty()) { b2bContactId = contacts[0].Id; } objectMap.put('b2bAccountId', b2bAccountId); if (b2bContactId != null) objectMap.put('b2bContactId', b2bContactId); } } } else { if(accMap != null && !accMap.isEmpty()){ HIC_ShopifyUtility.InitializeAccount(ShopifyIdvsAccountId, configurationList, store); } } // ── Auto-convert Lead after Account/Contact upsert (only when Lead feature is ON) ── if (!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Enable_Lead__c) { String customerEmail = (String) accMap.get('email'); String shopifyCustomerId = String.valueOf(accMap.get('id')); String shopifyIdPattern = '%' + shopifyCustomerId; if (String.isNotBlank(customerEmail) && String.isNotBlank(shopifyCustomerId)) { List existingLead = [ SELECT Id, Email, HIC_ShopifySync__Shopify_Id__c FROM Lead WHERE HIC_ShopifySync__Shopify_Id__c = :shopifyCustomerId AND IsConverted = false LIMIT 1 ]; if (!existingLead.isEmpty()) { List matchingContact = new List(); // B2B: search Contact by 'Business ' + customerId (exact match) if (!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Customer_Email_Domain__c == true) { String b2bShopifyId = 'Business ' + shopifyCustomerId; matchingContact = [ SELECT Id, AccountId, HIC_ShopifySync__Shopify_Id__c FROM Contact WHERE HIC_ShopifySync__Shopify_Id__c = :b2bShopifyId LIMIT 1 ]; } else { matchingContact = [ SELECT Id, AccountId, HIC_ShopifySync__Shopify_Id__c FROM Contact WHERE Email = :customerEmail AND HIC_ShopifySync__Shopify_Id__c LIKE :shopifyIdPattern ORDER BY CreatedDate DESC LIMIT 1 ]; } if (!matchingContact.isEmpty()) { try { Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(existingLead[0].Id); lc.setAccountId(matchingContact[0].AccountId); lc.setContactId(matchingContact[0].Id); lc.setDoNotCreateOpportunity(true); lc.setConvertedStatus('Closed - Converted'); Database.convertLead(lc); } catch (Exception ex) { System.debug('Auto-convert exception: ' + ex.getMessage()); } } } } } String listType = 'List<' + 'Order' + '>'; list sobjListToUpsert = (List)Type.forName(listType).newInstance(); if(!OrderfieldMapping.isEmpty() && !objectMap.isEmpty()){ sobjListToUpsert.add(HIC_ShopifyUtility.processFieldTypeAndMapping(OrderfieldMapping,'Order',objectMap,configurationList,companyTimeZone)); // B2B: Override Account and Contact on Order if (!configurationList.isEmpty() && configurationList[0].HIC_ShopifySync__Customer_Email_Domain__c == true && !sobjListToUpsert.isEmpty()) { SObject ord = sobjListToUpsert[0]; if (objectMap.containsKey('b2bAccountId') && objectMap.get('b2bAccountId') != null) { ord.putSObject('Account', null); ord.put('AccountId', (Id) objectMap.get('b2bAccountId')); if (objectMap.containsKey('b2bContactId') && objectMap.get('b2bContactId') != null) { ord.put('HIC_ShopifySync__Order_s_Contact__c', (Id) objectMap.get('b2bContactId')); } } } } String query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c, HIC_ShopifySync__IsSFID__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')+' = \'OrderItem\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; list OrderproductFieldsList = new list(); OrderproductFieldsList = database.query(query); Type typLstSObject = Type.ForName('List'); List orderproductListToupsert = (List)typLstSObject.NewInstance(); list Orderproductlist = new list(); list listOfexistingProductOpplineitem = new list(); map productVsPricebookEntryMap = new map(); map productVsUnitepriceMap = new map(); map UpdatedproductVsPricebookEntryMap = new map(); map shopifyOrderIdVsExistMap = new map(); shopifyOrderIdVsExistMap.put(String.valueof(objectMap.get('id')),false); if(Schema.sObjectType.Order.isAccessible() && Schema.sObjectType.Order.fields.HIC_ShopifySync__Shopify_Id__c.isAccessible()){ for(Order order : [Select HIC_ShopifySync__Shopify_Id__c from Order Where HIC_ShopifySync__Shopify_Id__c IN: shopifyOrderIdVsExistMap.keyset()]){ shopifyOrderIdVsExistMap.put(order.HIC_ShopifySync__Shopify_Id__c, true); } } system.debug('**objectMap.get'+objectMap.get('line_items')); system.debug('**shopifyOrderIdVsExistMap'+shopifyOrderIdVsExistMap); if(objectMap != null && !objectMap.isEmpty() && objectMap.containskey('line_items') ){ /* OrderItem Delete */ String OrderId = string.valueOf(objectMap.get('id')); list listtodeleteOrder = new list(); String Orderquery = 'Select '+CheckAccessibility.isAccessible('id,HIC_ShopifySync__Shopify_Id__c','Order') +' from Order Where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Shopify_Id__c','Order')+' = \''+String.escapeSingleQuotes(OrderId)+'\' limit 1' ; /*List orderList = [Select Pricebook2id from Order where HIC_ShopifySync__Shopify_Id__c =: OrderId]; Boolean tempFlag = false; for(Order ordr : orderList){ if(ordr.Pricebook2id != configurationList[0].HIC_ShopifySync__Salesforce_Pricebook__c){ tempFlag = true; } }*/ String query3 = ''; Boolean multiCurrencyEnabled = Schema.getGlobalDescribe().containsKey('CurrencyType'); if(multiCurrencyEnabled){ query3 = 'Select Pricebook2id, CurrencyIsoCode from Order where HIC_ShopifySync__Shopify_Id__c =: OrderId'; }else{ query3 = 'Select Pricebook2id from Order where HIC_ShopifySync__Shopify_Id__c =: OrderId'; } List currencyMappingList = new List(); String query2 = ''; if(multiCurrencyEnabled){ query2 = ' select '+CheckAccessibility.isAccessible('id,name,HIC_ShopifySync__Store_External_ID__c,HIC_ShopifySync__External_ID__c,HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c , HIC_ShopifySync__IsSFID__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' from HIC_ShopifySync__HIC_Field_Mapping__c where ' +CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c','HIC_ShopifySync__HIC_Field_Mapping__c') +'= \'CurrencyISOCODE\' AND ' +CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c', 'HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; currencyMappingList = database.query(query2); } List orderList = database.query(query3); /*Boolean tempFlag = false; for(Order ordr : orderList){ if(ordr.Pricebook2id != configurationList[0].HIC_ShopifySync__Salesforce_Pricebook__c || (multiCurrencyEnabled && !currencyMappingList.isEmpty() && ordr.get('CurrencyIsoCode') != currencyMappingList[0].HIC_ShopifySync__Shopify_Fields__c)){ tempFlag = true; } } if(tempFlag){ listtodeleteOrder= database.query(Orderquery); for(Order order : listtodeleteOrder){ order.Status = 'Draft'; } Database.Update(listtodeleteOrder, false); } list listtodeleteOrderitem = new list(); if(!listtodeleteOrder.isEmpty()){ String OrderSFid= listtodeleteOrder[0].id; String Orderitemquery = 'Select '+CheckAccessibility.isAccessible('id,HIC_ShopifySync__Shopify_Id__c','OrderItem') +' from OrderItem Where '+CheckAccessibility.isAccessible('orderId','OrderItem')+' = \''+OrderSFid+'\' limit 51231212' ; listtodeleteOrderitem = database.query(Orderitemquery); } /* try{ if(!listtodeleteOrderitem.isEmpty()){ if(CheckAccessibility.isDeletable('OrderItem')) // database.delete (listtodeleteOrderitem,false); } }catch(Exception e){ system.debug('Error: '+e.getMessage()); }*/ /* OrderItem Delete End*/ Orderproductlist = (list)objectMap.get('line_items'); for(Object oProdList : Orderproductlist){ Map oProdmap = (Map)oProdList; if(oProdmap.get('product_id') == null) oProdmap.put('product_id', 'N/A'); if(oProdmap.get('variant_id') == null) oProdmap.put('variant_id', 'N/A'); //updated After 1st release; //if( !oprodMap.isEmpty() && oprodMap.containsKey('product_exists') && oprodMap.get('product_exists') != null && String.valueof(oprodMap.get('product_exists')).equalsIgnoreCase('false')) continue; productVsPricebookEntryMap.put(String.valueof(oProdmap.get('product_id')),null); productVsUnitepriceMap.put(String.valueof(oProdmap.get('product_id')),String.valueof(oProdmap.get('price'))); oProdmap.put('orderId',string.valueOf(objectMap.get('id'))); /*if(oProdmap.get('product_id') == 'N/A' && oProdmap.get('variant_id') == 'N/A') oProdmap.put('id',String.valueOf(objectMap.get('id')) + Orderproductlist.indexOf(oProdList)); else //oProdmap.put('id',String.valueOf(objectMap.get('id')) + String.valueof(oProdmap.get('variant_id')) + String.valueOf(oProdmap.get('product_id'))); */ } if(!Test.isRunningTest()){ if(presentmentCurrency!=null && presentmentCurrency!=' '){ updatedProductVsPricebookEntryMap = new Map(HIC_ShopifyUtility.InitializePricebookEntryIdorder(productVsPricebookEntryMap,productVsUnitepriceMap,configurationList,presentmentCurrency,store)); }else{ updatedProductVsPricebookEntryMap = new Map(HIC_ShopifyUtility.InitializePricebookEntryId(productVsPricebookEntryMap,productVsUnitepriceMap,configurationList,store)); } // map UpdatedproductVsPricebookEntryMap = new map(HIC_ShopifyUtility.InitializePricebookEntryIdorder(productVsPricebookEntryMap,productVsUnitepriceMap, configurationList,presentmentCurrency,store)); for(Object oProdList : Orderproductlist){ Map oProdmap = (Map)oProdList; //updated After 1st release; //if( !oprodMap.isEmpty() && oprodMap.containsKey('product_exists') && oprodMap.get('product_exists') != null && String.valueof(oprodMap.get('product_exists')).equalsIgnoreCase('false')) continue; oProdmap.put('PricebookEntryId',string.valueOf(UpdatedproductVsPricebookEntryMap.get(string.valueOf(oProdmap.get('product_id'))))); oProdmap.put('UnitPrice',oProdmap.get('price')); if(oProdmap.get('product_id') == null) oProdmap.put('product_id', 'N/A'); if(oProdmap.get('variant_id') == null) oProdmap.put('variant_id', 'N/A'); oProdList = (Object)oProdmap; System.debug('oProdList@@'+ oProdList); if(oProdmap.containsKey('PricebookEntryId') && oProdmap.get('PricebookEntryId') != null && String.isNotBlank(String.valueof(oProdmap.get('PricebookEntryId')))) listOfexistingProductOpplineitem.add(oProdList); } HIC_ShopifyUtility.initializeProductandVaraint(listOfexistingProductOpplineitem); } } orderproductListToupsert.addAll(HIC_ShopifyUtility.fillObjectValues(OrderproductFieldsList,'OrderItem',listOfexistingProductOpplineitem, configurationList)); try{ if(!sobjListToUpsert.Isempty()){ system.debug('**orderListToupsert--2'+sobjListToUpsert); Schema.SObjectType objectType = Schema.getGlobalDescribe().get('Order'); Map fieldMap = objectType.getDescribe().fields.getMap(); Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c'); if (configurationList[0].HIC_ShopifySync__Draft_Order_Sync__c == true){ if (configurationList[0].HIC_ShopifySync__Enable_Draft_Order__c == true) { String shopifyOrderId = String.valueOf(objectMap.get('id')); List draftOrders = [ SELECT Id, HIC_ShopifySync__Shopify_Id__c, HIC_ShopifySync__Shopify_Order_ID__c FROM Order WHERE HIC_ShopifySync__Shopify_Order_ID__c = :shopifyOrderId AND IsDeleted = FALSE LIMIT 1 ]; if (!draftOrders.isEmpty()) { for (SObject ord : sobjListToUpsert) { ord.put('HIC_ShopifySync__Shopify_Draft_Order_ID__c', draftOrders[0].HIC_ShopifySync__Shopify_Id__c); } } } else if(configurationList[0].HIC_ShopifySync__Enable_Opportunity_for_SDO__c == true) { String shopifyOrderId = String.valueOf(objectMap.get('id')); List draftOpps = [ SELECT Id, HIC_ShopifySync__Shopify_Id__c, HIC_ShopifySync__Shopify_Order_ID__c FROM Opportunity WHERE HIC_ShopifySync__Shopify_Order_ID__c = :shopifyOrderId AND IsDeleted = FALSE LIMIT 1 ]; if (!draftOpps.isEmpty()) { for (SObject ord : sobjListToUpsert) { ord.put('HIC_ShopifySync__Shopify_Draft_Order_ID__c', draftOpps[0].HIC_ShopifySync__Shopify_Id__c); } } } } EmailErrorCSV.makeCsvAndSendInEmail(body,'Order',sobjListToUpsert,theField,true,store); } if(!orderproductListToupsert.isEmpty()){ system.debug('**orderproductListToupsert--2'+orderproductListToupsert); Schema.SObjectType objectType = Schema.getGlobalDescribe().get('OrderItem'); Map fieldMap = objectType.getDescribe().fields.getMap(); Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c'); System.debug('theField@'+theField); for(OrderItem orderItemObj : (list)orderproductListToupsert){ if(orderItemObj.HIC_ShopifySync__Shopify_Id__c == null || orderItemObj.HIC_ShopifySync__Shopify_Id__c == ''){ List lineItemsList = (List)objectMap.get('line_items'); for(Object obj : lineItemsList){ Map lineItemsMap = (Map)obj; system.debug('**Check'+ String.valueOf(objectMap.get('id'))); system.debug('**Check'+String.valueOf(lineItemsMap.get('variant_id'))); system.debug('**Check'+String.valueOf(lineItemsMap.get('product_id'))); orderItemObj.HIC_ShopifySync__Shopify_Id__c = String.valueOf(objectMap.get('id')) + String.valueOf(lineItemsMap.get('variant_id')) + String.valueOf(lineItemsMap.get('product_id')); } } } EmailErrorCSV.makeCsvAndSendInEmail(body,'OrderItem',orderproductListToupsert,theField,true,store); } Schema.SObjectType objectType = Schema.getGlobalDescribe().get('Order'); Map fieldMap = objectType.getDescribe().fields.getMap(); Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c'); if(!configurationList[0].HIC_ShopifySync__Order_Status_Activated_To_Draft__c){ for(SObject ord : sobjListToUpsert){ ord.put('Status', 'Activated'); } } Database.upsert(sobjListToUpsert, theField, false); }catch(Exception e){ system.debug('***Exception here'+e.getlinenumber()); system.debug('***Exception is'+ + e.getMessage()); if(CheckAccessibility.isInsertable('name,HIC_ShopifySync__Missing_Order_Info__c,HIC_ShopifySync__Shopify_id__c,', 'HIC_ShopifySync__Missing_Order__c')){ CheckAccessibility.isAccessible('name,HIC_ShopifySync__Missing_Order_Info__c,HIC_ShopifySync__Shopify_id__c,', 'HIC_ShopifySync__Missing_Order__c'); HIC_ShopifySync__Missing_Order__c missingOrder = new HIC_ShopifySync__Missing_Order__c(); missingOrder.name = string.valueof(objectMap.get('order_number')); //missingOrder.HIC_ShopifySync__Missing_Order_Info__c = body; String jSONRequestBody = body; if(jSONRequestBody!= null && jSONRequestBody != ''){ Integer lengthofjSONRequestBody = jSONRequestBody.length(); if(lengthofjSONRequestBody != null && lengthofjSONRequestBody != 0 ){ if(lengthofjSONRequestBody <= 131072 ){ missingOrder.Missing_Order_Info__c = jSONRequestBody; }else if(lengthofjSONRequestBody > 131072 && lengthofjSONRequestBody <= 262144){ missingOrder.Missing_Order_Info__c = jSONRequestBody.substring(0,131072); missingOrder.Missing_Order_Second_Info__c = jSONRequestBody.substring(131072,lengthofjSONRequestBody); }else if(lengthofjSONRequestBody > 262144){ missingOrder.Missing_Order_Info__c = jSONRequestBody.substring(0,131072); missingOrder.Missing_Order_Second_Info__c = jSONRequestBody.substring(131072,262144); missingOrder.Missing_Order_Third_Info__c = jSONRequestBody.substring(262144,lengthofjSONRequestBody); } } } missingOrder.HIC_ShopifySync__Shopify_id__c = String.valueof(objectMap.get('id')); database.insert(missingOrder); } } if(configurationList[0].HIC_ShopifySync__Order_Fulfillment__c== true && body != null && body != '') insertOrderFulfillments(body,store); } public static void insertOrderFulfillments(String body,String store){ Map orderproductmap = (Map) JSON.deserializeUntyped(body); Map oProdmap = new Map(); list listOfAllfullfillmentOrderitem = new list(); list Orderfulfillmentproductlist = new list(); list listOfAllfullfillmentOrder = new list(); list OrderFulfillmentslist = new list(); String query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c, HIC_ShopifySync__IsSFID__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__Order_Fulfillment__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; list OrderfulfillmentFieldsList = new list(); OrderfulfillmentFieldsList = database.query(query); query = ''; query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c, HIC_ShopifySync__IsSFID__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__Order_Fulfillment_Item__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999'; list OrderfulfillmentitemFieldsList = new list([Select HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c, HIC_ShopifySync__IsSFID__c from HIC_ShopifySync__HIC_Field_Mapping__c Where HIC_ShopifySync__Object_Name__c = 'HIC_ShopifySync__Order_Fulfillment_Item__c']); OrderfulfillmentitemFieldsList = database.query(query); if(orderproductmap != null && !orderproductmap.isEmpty() && orderproductmap.containskey('fulfillments') ){ OrderFulfillmentslist = (list)orderproductmap.get('fulfillments'); listOfAllfullfillmentOrder.addall(OrderFulfillmentslist); } try{ if(!listOfAllfullfillmentOrder.isEmpty() && !OrderfulfillmentFieldsList.isEmpty()){ HIC_ShopifyUtility.processSobjectList(OrderfulfillmentFieldsList,listOfAllfullfillmentOrder,'HIC_ShopifySync__Order_Fulfillment__c', 'HIC_ShopifySync__Shopify_Id__c',configurationList,true,store); } }catch(Exception e){ //System.debug('DML Error'); } for(Object Orderfulfillmentitem : OrderFulfillmentslist){ Map orderfulfillmentItemmap = (Map)Orderfulfillmentitem; if(orderfulfillmentItemmap != null && !orderfulfillmentItemmap.isEmpty() && orderfulfillmentItemmap.containskey('line_items')){ Orderfulfillmentproductlist = (list)orderfulfillmentItemmap.get('line_items'); for(Object orderfuilmentitem : Orderfulfillmentproductlist){ oProdmap = (Map)orderfuilmentitem; oProdmap.put('orderfulfillmentId',string.valueOf(orderfulfillmentItemmap.get('id'))); oProdmap.put('id', string.valueOf(orderfulfillmentItemmap.get('id'))+oProdmap.get('id')); orderfuilmentitem = (Object)oProdmap; } listOfAllfullfillmentOrderitem.addAll(Orderfulfillmentproductlist); } } try{ if(!listOfAllfullfillmentOrder.isEmpty() && !OrderfulfillmentFieldsList.isEmpty()){ HIC_ShopifyUtility.processSobjectList(OrderfulfillmentitemFieldsList,listOfAllfullfillmentOrderitem,'HIC_ShopifySync__Order_Fulfillment_Item__c', 'HIC_ShopifySync__Shopify_Id__c',configurationList,true,store); } }catch(Exception e){ //System.debug('DML Error'); } EmailErrorCSV.flushFeatureParameters(); System.debug('flushFeatureParameters method called-->>'); } }