0

What I want to do: I want to fetch my woocommerce data to my salesforce. And I don't want to use plugins.

So, I started this whole thing by using REST API.

What I am thinking: I will call the http request and fetch a JSON output using woocommerce REST API and then I convert it to my salesforce MAP objects and then store the data into my sObjects in salesforce.

I used this resource from salesforce link

I used this code to fetch woocommererce orders table data with ID but getting nothing. When I used the POSTMAN REST Client it is giving me the data. But when I use the below code no content is getting back.

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://mydomain.com/wp-json/wc/v2/orders/37?oauth_consumer_key=XXXXXXXXXXXXXX&oauth_signature_method=HMAC-SHA1&oauth_timestamp=XXXXXXXXXXX&oauth_nonce=XXXXXXXXX&oauth_version=1.0&oauth_signature=XXXXXXXXXXXXXXXXXXX='); 

request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');

    //String ss = (String)animals[0];

    //System.debug('Name one is: ' + ss);

    System.debug('Received the following animals:');
    for (Object animal: animals) {
        System.debug(animal);
    }
}else{
    System.debug('Error: unable to connect to endpoint.');
}

Sample JSON IS:

[
    {
        "id": 38,
        "parent_id": 0,
        "number": "38",
        "order_key": "wc_order_5b472b646db19",
        "created_via": "checkout",
        "version": "3.4.3",
        "status": "processing",
        "currency": "INR",
        "date_created": "2018-07-12T10:20:20",
        "date_created_gmt": "2018-07-12T10:20:20",
        "date_modified": "2018-07-12T10:20:20",
        "date_modified_gmt": "2018-07-12T10:20:20",
        "discount_total": "0.00",
        "discount_tax": "0.00",
        "shipping_total": "0.00",
        "shipping_tax": "0.00",
        "cart_tax": "0.00",
        "total": "80.00",
        "total_tax": "0.00",
        "prices_include_tax": false,
        "customer_id": 1,
        "customer_note": "",
        "billing": {
            "first_name": "Mohan",
            "last_name": "Raj",
            "company": "XYZ",
            "address_1": "Noida",
            "address_2": "Indirapuram",
            "city": "Noida",
            "state": "UP",
            "postcode": "201014",
            "country": "IN",
            "email": "[email protected]",
            "phone": "9991231223"
        },
        "shipping": {
            "first_name": "Mohan",
            "last_name": "Raj",
            "company": "XYZ",
            "address_1": "Noida",
            "address_2": "Indirapuram",
            "city": "Noida",
            "state": "UP",
            "postcode": "201014",
            "country": "IN"
        },
        "payment_method": "cod",
        "payment_method_title": "Cash on delivery",
        "transaction_id": "",
        "date_paid": null,
        "date_paid_gmt": null,
        "date_completed": null,
        "date_completed_gmt": null,
        "meta_data": [],
        "line_items": [
            {
                "id": 10,
                "name": "Coca Cola",
                "product_id": 24,
                "variation_id": 0,
                "quantity": 8,
                "tax_class": "",
                "subtotal": "80.00",
                "subtotal_tax": "0.00",
                "total": "80.00",
                "total_tax": "0.00",
                "taxes": [],
                "meta_data": [],
                "sku": "",
                "price": 10
            }
        ],
        "tax_lines": [],
        "shipping_lines": [
            {
                "id": 11,
                "method_title": "Free shipping",
                "method_id": "free_shipping",
                "instance_id": "1",
                "total": "0.00",
                "total_tax": "0.00",
                "taxes": [],
                "meta_data": [
                    {
                        "id": 84,
                        "key": "Items",
                        "value": "Coca Cola &times; 8"
                    }
                ]
            }
        ]
]

** This is the code of wrapper class **

public class OrdersWrapper {
    public class Billing{
        public String first_name {get;    
    }


    public class LineItems{
        public Integer id {get; set;}
        public String name {get; set;}

    }

    public class Orders{
        public Integer id {get; set;}
        public String status {get; set;}
        public List<Billing> billing {get; set;}

    }

    public List<Orders> orders {get; set;}
}
10
  • Have you tried debugging a response Body? System.debug(response.getBody());
    – Eduard
    Jul 12, 2018 at 11:23
  • Also check if response.getBodyAsBlob() returns anything, it might be that the webservice returns blob data.
    – Eduard
    Jul 12, 2018 at 11:25
  • Hey! Eduard. Thank you for your response. I tried the response.getBody() and it is giving me the data which I wanted. Now can I convert the data which I am getting into List or Maps Because it is a nested JSON data and the example in salesforce has simple JSON data. Jul 12, 2018 at 11:39
  • Can you provide a sample of your JSON?
    – Eduard
    Jul 12, 2018 at 11:53
  • 1
    Hey David Reed, Sorry for duplication. Next time I will be more careful. Jul 12, 2018 at 12:00

1 Answer 1

0

I would recommend creating simple DTO (Data Transfer Object)/Wrapper classes that follow data model of your JSON. Then use JSON.deserialize(<your_json_string>, <wrapper_class>.class); from JSON Class.

The example wrapper class structure can be:

public class OrderDataMain
{
    public List<OrderData> orders;  
}

public class OrderData
{
 public String id { get; set; }
 public String status  { get; set; }
 public Billing billing { get; set; }
 ...
 <other fields>
 ...
}

public class Billing 
{
 public String last_name { get; set; }
 ...
 <other fields>
 ...
}

I think you got the idea...

Then you use it to deserialize your JSON:

List<OrderData> results = (List<OrderData>)JSON.deserialize(jsonDataList, List<OrderData>.class);

After that you will have all your data in the list of typr OrderData and you can access its data using apex.

Test Examples:

String jsonDataMain = '{"orders":[{"id":38,"status":"processing"},{"id":12,"status":"hold"}]}';
OrderDataMain main = (OrderDataMain)JSON.deserialize(jsonDataMain, OrderDataMain.class);
System.debug('main=' + main);

String jsonDataList = '[{"id":38,"status":"processing"},{"id":12,"status":"hold"}]';
List<OrderData> results = (List<OrderData>)JSON.deserialize(jsonDataList, List<OrderData>.class);
System.debug('results=' + results);
10
  • Hey @Eduard I am getting the JSON data but when I use that data it is giving me the Error. malformed json: expected '{' at the beginning of object salesforce Jul 16, 2018 at 10:59
  • What data are you talking about? The data from my example above?
    – Eduard
    Jul 16, 2018 at 11:20
  • The JSON data which I get from my endpoint i.e from my woo commerce store (in form of JSON arrays ). Jul 16, 2018 at 11:50
  • I have pasted the wrapper class of my data. Jul 16, 2018 at 11:54
  • Then, you need to make sure that your data is a valid JSON string. Also make sure that your JSON matches the data model (wrapper class) you deserialize it to. Pay attention to Lists in JSON and how they are difined in your data model, e.g. "line_items": [{...},{...}} should be public List<LineItem> line_items {get;set;} in your wrapper class
    – Eduard
    Jul 16, 2018 at 11:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .