Skip to main content
Mohankumar B 님이 #Data Management에 질문했습니다
// Setting up the Request URL

 

@RestResource(urlMapping='/ShopifyProductsAPI/*')

 

global with sharing class ShopifyProductsResource {

 

    

 

    // Creating a response wrapper class

 

    global class ResponseWrapper {

 

        String success;

 

        String message;

 

    }

 

    

 

    //PUT Method

 

    @HTTPPut

 

    global static List<ResponseWrapper> upsertProducts(){

 

        // Initializing the request parameter with the incoming request

 

        RestRequest productRequest = RestContext.request;

 

        // Getting the request body from the request

 

        String requestBody = productRequest.requestBody.toString();

 

        // Deserializing the JSON response body

 

        List<product2> productList = new List<product2>();    

 

        List<ResponseWrapper> responseWrapperList = new List<ResponseWrapper>();

 

        

 

        ProductWrapper responseWrapper = ProductWrapper.parse(requestBody);

 

        if((responseWrapper.products!=null) && (!responseWrapper.products.isEmpty())) {

 

            system.debug('Product list size ' + responseWrapper.products.size());

 

            

 

            for(Integer i=0; i<responseWrapper.products.size(); i++) {

 

                ProductWrapper.products product = responseWrapper.products.get(i);

 

                

 

                if(product.variants.size()>0){

 

                    for(Integer j=0; j<product.variants.size(); j++){

 

                        

 

                        product2 pv = new product2();

 

                        

 

                        //Header

 

                        pv.Shopify_ProductId__c = string.valueOf(product.id);

 

                        pv.Tags__c= string.valueOf(product.tags);

 

                        pv.Shopify_Handle__c = string.valueOf(product.handle);

 

                        pv.Product_Vendor__c = string.valueOf(product.vendor);

 

                        pv.IsActive = true;

 

                        

 

                        //Variant Details

 

                        pv.Shopify_Variant_Id__c = string.valueOf(product.variants[j].id);

 

                        pv.Name = string.valueOf(product.title) + '/' + string.valueOf(product.variants[j].title);

 

                        pv.Title__c = string.valueOf(product.variants[j].title);

 

                        pv.ProductCode = string.valueOf(product.variants[j].sku);

 

                        pv.Shopify_Inventory_Qty__c = integer.valueOf(product.variants[j].inventory_quantity);

 

                        pv.Shopify_Variant_Position__c = string.valueOf(product.variants[j].position);

 

                        if(product.variants[j].compare_at_price==null){

 

                            pv.Shopify_Compare_at_price__c = 0;

 

                        }else{

 

                            pv.Shopify_Compare_at_price__c = Integer.valueOf(product.variants[j].compare_at_price);

 

                        }

 

                        

 

                        pv.Price__c = Integer.valueOf(product.variants[j].price);

 

                        

 

                        if(product.images.size()>0){

 

                            for(Integer k=0; k<product.images.size(); k++){

 

                                if(string.valueOf(product.images[k].position) == '1'){

 

                                    pv.DisplayUrl =string.valueOf(product.images[k].src);

 

                                }

 

                            }//end of for image size

 

                        }//end of if image size

 

                        productList.add(pv);

 

                        //system.debug('Product Variant List inside ' + productList);

 

                    } //end of for product Variants

 

                }//end of if product variants  

 

            }//end of for

 

        }//end of if product wrapper

 

        

 

        

 

        

 

        

 

        // Inserting productList

 

        // upsert productList;

 

        // Returning the productList in the response

 

        for(product2 prod: productList){

 

            system.debug('Product ' + prod);

 

        }

 

        

 

        

 

        Schema.SObjectField ProdutExternalId = product2.Fields.Shopify_Variant_Id__c;

 

        

 

        Database.UpsertResult[] upsertResults = Database.upsert(productList,ProdutExternalId, false);

 

        // Checking the result of update operation for each Product

 

        for(Database.UpsertResult upsertResult : upsertResults) {

 

            // Initializing the response wrapper

 

            ResponseWrapper wrapper = new ResponseWrapper();

 

            // Initializing the response wrapper

 

            if (upsertResult.isSuccess()) {

 

                // Update successful, setting up the appropriate message in response wrapper

 

                wrapper.success = '1';

 

                wrapper.message = 'Successfully updated Product with Id: ' + upsertResult.getId();

 

            }

 

            else {

 

                // Update failed, getting errors and setting up the error message in response wrapper

 

                wrapper.success = '0';

 

                for(Database.Error error : upsertResult.getErrors()) {

 

                    wrapper.message = 'The following error has occurred for Product with Id: ' + upsertResult.getId() + '. ';

 

                    wrapper.message += error.getStatusCode() + ': ' + error.getMessage() + '. ';

 

                    wrapper.message += 'Product fields that affected this error: ' + error.getFields();

 

                }

 

            }

 

            // Adding response wrapper instance to the response wrapper list

 

            responseWrapperList.add(wrapper);

 

        }

 

        // Returning the response wrapper list in the response

 

        return responseWrapperList;

 

    }

 

    

 

}
답변 1개
0/9000