120 lines
8.7 KiB
OpenEdge ABL
120 lines
8.7 KiB
OpenEdge ABL
/**
|
|
* ─────────────────────────────────────────────────────────────────────────────────────────────────┐
|
|
* This web service class is use for handle Shopify's Order webhook response.
|
|
* Trigger on Order delete.
|
|
* 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 <sumit@hicShopify.com>
|
|
* ──────────────────────────────────────────────────────────────────────────────────────────────────
|
|
*/
|
|
@RestResource(UrlMapping='/DeleteOrderFromShopify/*')
|
|
global with sharing class DeleteOrderFromShopify
|
|
{
|
|
@HTTPPost
|
|
global static void DeleteOrder(){
|
|
Restrequest request = restContext.request;
|
|
String store = 'Default';
|
|
Map <String, String> params = request.params;
|
|
if(!params.isEmpty() && params.containsKey('store') && params.get('store') != null)
|
|
store = params.get('store');
|
|
Restresponse response = restContext.response;
|
|
String JSONResponseBody = request.requestBody.ToString().Trim();
|
|
System.debug('JSONResponseBodyCustomerDelete--->'+JSONResponseBody);
|
|
Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(JSONResponseBody);
|
|
String OrderShopifyidforDelete = String.valueof(meta.get('id'));
|
|
List<Order> listOfOrderForDelete = new List<Order>();
|
|
List<Opportunity> listOfOpportunityForDelete = new List<Opportunity>();
|
|
List<HIC_ShopifySync__Order_Fulfillment__c> listOfOrderfulfillmentForDelete = new List<HIC_ShopifySync__Order_Fulfillment__c>();
|
|
|
|
list<HIC_ShopifySync__HIC_Configuration__c > configurationList = new list<HIC_ShopifySync__HIC_Configuration__c >();
|
|
String query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__Account_Contact__c, HIC_ShopifySync__Default_Account__c,HIC_ShopifySync__Enable_Draft_Order__c, name,HIC_ShopifySync__Enable_Opportunity_for_SDO__c, 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__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__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);
|
|
System.debug('configurationList == '+ configurationList);
|
|
|
|
if(Schema.sObjectType.Order.isAccessible() &&
|
|
Schema.sObjectType.Order.fields.HIC_ShopifySync__Shopify_id__c.isAccessible()){
|
|
if(!configurationList.isempty() && (configurationList[0].HIC_ShopifySync__Order__c== true && configurationList[0].HIC_ShopifySync__Enable_Draft_Order__c== false)){
|
|
listOfOrderForDelete = [select id,status, HIC_ShopifySync__Shopify_id__c from Order where HIC_ShopifySync__Shopify_id__c =: OrderShopifyidforDelete and status= 'Activated' ];
|
|
}
|
|
if(!configurationList.isempty() && (configurationList[0].HIC_ShopifySync__Order__c== false && configurationList[0].HIC_ShopifySync__Enable_Draft_Order__c== true)){
|
|
listOfOrderForDelete = [select id,status, HIC_ShopifySync__Shopify_id__c from Order where HIC_ShopifySync__Shopify_id__c =: OrderShopifyidforDelete and status = 'Draft' ];
|
|
}
|
|
if(!configurationList.isempty() && (configurationList[0].HIC_ShopifySync__Order__c== true && configurationList[0].HIC_ShopifySync__Enable_Draft_Order__c== true)){
|
|
listOfOrderForDelete = [select id, HIC_ShopifySync__Shopify_id__c from Order where HIC_ShopifySync__Shopify_id__c =: OrderShopifyidforDelete ];
|
|
}
|
|
}
|
|
for(Order order : listOfOrderForDelete){
|
|
order.Status = 'Draft';
|
|
}
|
|
if (!listOfOrderForDelete.isEmpty()) {
|
|
update listOfOrderForDelete;
|
|
}
|
|
|
|
|
|
if(Schema.sObjectType.opportunity.isAccessible() &&
|
|
Schema.sObjectType.opportunity.fields.HIC_ShopifySync__Shopify_id__c.isAccessible()){
|
|
if(!configurationList.isempty()){
|
|
listOfOpportunityForDelete = [select id, HIC_ShopifySync__Shopify_id__c from opportunity where HIC_ShopifySync__Shopify_id__c =: OrderShopifyidforDelete ];
|
|
}
|
|
|
|
}
|
|
if (!listOfOpportunityForDelete.isEmpty()) {
|
|
update listOfOpportunityForDelete;
|
|
}
|
|
|
|
if(!listOfOrderForDelete.isEmpty()){
|
|
if(Schema.sObjectType.Order.isAccessible() &&
|
|
Schema.sObjectType.Order.fields.HIC_ShopifySync__Shopify_id__c.isAccessible()){
|
|
listOfOrderfulfillmentForDelete = [select id, HIC_ShopifySync__Shopify_id__c from HIC_ShopifySync__Order_Fulfillment__c where HIC_ShopifySync__Order__c =: listOfOrderForDelete[0].id ];
|
|
}
|
|
}else if(!listOfOpportunityForDelete.isEmpty()){
|
|
if(Schema.sObjectType.Order.isAccessible() &&
|
|
Schema.sObjectType.Order.fields.HIC_ShopifySync__Shopify_id__c.isAccessible()){
|
|
listOfOrderfulfillmentForDelete = [select id, HIC_ShopifySync__Shopify_id__c from HIC_ShopifySync__Order_Fulfillment__c where HIC_ShopifySync__Order__c =: listOfOpportunityForDelete[0].id ];
|
|
}
|
|
}
|
|
list<Database.DeleteResult> deleterecordslist = new list<Database.DeleteResult>();
|
|
try{
|
|
Boolean error = false;
|
|
if(!listOfOrderForDelete.isempty()){
|
|
if(CheckAccessibility.isDeletable('Order'))
|
|
deleterecordslist = Database.delete(listOfOrderForDelete,false);
|
|
if(!deleterecordslist.isEmpty()) {
|
|
EmailerrorCSV.DeleteRecords('Order',JSONResponseBody,deleterecordslist,listOfOrderForDelete,store);
|
|
}
|
|
for(Database.DeleteResult result : deleterecordslist){
|
|
if(!result.isSuccess()){
|
|
error = true;
|
|
}
|
|
}
|
|
|
|
deleterecordslist.clear();
|
|
}
|
|
if(!listOfOpportunityForDelete.isempty() && !error){
|
|
if(CheckAccessibility.isDeletable('opportunity'))
|
|
deleterecordslist = Database.delete(listOfOpportunityForDelete,false);
|
|
if(!deleterecordslist.isEmpty()) {
|
|
EmailerrorCSV.DeleteRecords('opportunity',JSONResponseBody,deleterecordslist,listOfOpportunityForDelete,store);
|
|
}
|
|
for(Database.DeleteResult result : deleterecordslist){
|
|
if(!result.isSuccess()){
|
|
error = true;
|
|
}
|
|
}
|
|
}
|
|
if(!listOfOrderfulfillmentForDelete.isempty() && !error ){
|
|
if(CheckAccessibility.isDeletable('HIC_ShopifySync__Order_Fulfillment__c'))
|
|
deleterecordslist = Database.delete(listOfOrderfulfillmentForDelete,false);
|
|
if(!deleterecordslist.isEmpty()) {
|
|
EmailerrorCSV.DeleteRecords('HIC_ShopifySync__Order_Fulfillment__c',JSONResponseBody,deleterecordslist,listOfOrderfulfillmentForDelete,store);
|
|
}
|
|
deleterecordslist.clear();
|
|
}
|
|
}Catch(Exception e){
|
|
System.debug('DML Error');
|
|
}
|
|
}
|
|
} |