0
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

3
  • I really highly recommend you try to use more informative variable names. Those you selected above communicate virtually nothing, making your code basically write-only. It makes a huge difference to the future developer, and that person may be you!
    – Adrian Larson
    Commented Aug 7, 2019 at 3:43
  • @AdrianLarson noted, will edit this soon, I just didnt wanna give away the real variable names so had made it like initials only, will definitely edit it soon. thank you
    – user905
    Commented Aug 7, 2019 at 3:46
  • I figured you might be changing them for this site, since they don't all match. ;)
    – Adrian Larson
    Commented Aug 7, 2019 at 3:50

1 Answer 1

3

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"
  }
}
1
  • Thank you. @salesforce-sas.
    – user905
    Commented Aug 7, 2019 at 10:21

You must log in to answer this question.

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