[SalesForce] dynamic json body as http post request

public class RequestWrapper {

    public static GetUrlRequest GetUrlRequest;

    public class OrderDetails {
        public String txnId;
        public String custOrder;

        public OrderDetails(String txnId, String custOrder){
            this.txnId = txnId;
            this.custOrder = custOrder;
        }
    }

    public class GetUrlRequest {
        public String custId;
        public String custCode;
        public String purpose;
        public OrderDetails orderDetails;
        public GetUrl getUrl;

        public GetUrlRequest(String custId, String custCode, String purpose, OrderDetails orderDetails, GetUrl getUrl){
            custId = custId;
            custCode = custCode;
            purpose = purpose;
            orderDetails = orderDetails;
            getUrl = getUrl;
        }
    }

    public class GetUrl {
        public String url1;
        public String url2;

        public cbl(String url1, String url2){
            this.url1 = url1;
            this.url2 = url2;
        }
    }


    public static RequestWrapper parse(String json) {
        return (RequestWrapper) System.JSON.deserialize(json, RequestWrapper.class);
    }
}

I formed this class for the json body that I need to pass in my http post request.
I created this class from json generator and added the constructors.
can someone please tell me how do I initialize this class, I will be setting dynamic values to this later, but right now I want to hardcode values to this class vars and send it as the request body.

I tried to as follows but this gives me error:

RequestWrapper.GetUrl getUrl= new RequestWrapper.GetUrl ('url1', 'url2');
RequestWrapper.OrderDetails orderDetails = new RequestWrapper.OrderDetails('txnId', 'custId');
String jsonRequestBody = JSON.serialize(RequestWrapper.GetUrlRequest);
req.setBody(jsonRequestBody);

Error is: Constructor not defined

Best Answer

You need to pass the parameters to GetUrlRequest by changing it to method:

public class RequestWrapper {

    public static GetUrlRequest GetUrlRequest(String cId, String sBC, String pur, abc abc, cbl cbl){
        return new GetUrlRequest(cId,sBC,pur,abc,cbl);
    }

    public class abc {
        public String ab;
        public String cd;

        public abc(String td, String cR){
            this.ab = td;
            this.cd = cR;
        }
    }

    public class GetUrlRequest {
        public String cd;
        public String sb;
        public String pp;
        public abc aO;
        public cbl cO;

        public GetUrlRequest(String cId, String sBC, String pur, abc abcInstance, cbl cblInstance){
            cd = cId;
            sb = sBC;
            pp = pur;
            aO = abcInstance;
            cO = cblInstance;
        }
    }

    public class cbl {
        public String ru;
        public String cu;

        public cbl(String rUrl, String cUrl){
            this.ru = rUrl;
            this.cu = cUrl;
        }
    }


    public static RequestWrapper parse(String json) {
        return (RequestWrapper) System.JSON.deserialize(json, RequestWrapper.class);
    }
}

and then you need to get the request like below by passing parameters:

public static String check(){
    RequestWrapper.cbl cblInstance = new RequestWrapper.cbl('stringcblValue', 'stringcuvalue');
    RequestWrapper.abc abclInstance = new RequestWrapper.abc('somestringvalue', 'somestringvalue');
    String jsonRequestBody = JSON.serialize(RequestWrapper.GetUrlRequest('cid','sbc','pur',abclInstance,cblInstance));
    System.debug(jsonRequestBody);
    return jsonRequestBody;
}

What will debug:

{
  "sb": "sbc",
  "pp": "pur",
  "cO": {
    "ru": "stringcblValue",
    "cu": "stringcuvalue"
  },
  "cd": "cid",
  "aO": {
    "cd": "somestringvalue",
    "ab": "somestringvalue"
  }
}
Related Topic