0

I'm trying to serialize to IP to a POST statement but it's giving me this error: message: Json Deserialization failed on token 'null' and has left off in the middle of parsing a row. Will go to end of row to begin parsing the next row errorCode: INVALID_FIELD

This is the body of the JSON:

[{"Start":"172.104.17.37","End":"172.104.17.37"},{"Start":"172.104.5.7","End":"172.104.17.37"}]

this is the code:

public class ipWrapper{
    String Start;
    String EndIP;
}

ipWrapper ip1 = new ipWrapper();
ip1.Start = '172.104.17.37';
ip1.EndIP = ip1.Start;

ipWrapper ip2 = new ipWrapper();
ip2.Start = '172.104.5.7';
ip2.EndIP = ip1.Start;

List<ipWrapper> listIP = new List<ipWrapper>();
listIP.add(ip1);
listIP.add(ip2);

String bodyToFix = JSON.serialize(listIP);
system.debug('bodyToFix: ' + bodyToFix);
String body = bodyToFix.replace('EndIP', 'End');
system.debug('body: ' + body);
HttpRequest req = new HttpRequest();
req.setBody(body);
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');      
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v41.0/tooling/sobjects/IpRange');
req.setMethod('POST');
Http httpreq = new Http();

HttpResponse res;

enter image description here

1 Answer 1

0

This endpoint (and most other endpoints in Tooling API) can only insert one entity in a POST request. So you will have to only send in one object instead of an array.

{"Start":"172.104.17.37","End":"172.104.17.37"}

However, if you wish to create multiple entities/records in the IPRange object or other objects in a single request, consider using the Composite API.

Hit this endpoint /services/data/v51.0/tooling/composite, with the below composite request - which has 2 sub requests, one for each IPRange.

{
  "allOrNone":false,
  "compositeRequest":
  [
    {
     "method":"POST",
     "body": {"Start":"172.104.17.37","End":"172.104.17.37"},
     "url":"/services/data/v40.0/tooling/sobjects/IPRange/",
     "referenceId":"iprange_reference_id1"
    },
   {
     "method":"POST",
     "body": {"Start":"172.104.17.39","End":"172.104.17.39"},
     "url":"/services/data/v40.0/tooling/sobjects/IPRange/",
     "referenceId":"iprange_reference_id2"
    }
  ]
}

So your Apex class could look something like this

public class CompositeRequestWrapper{
    Boolean allOrNone;
    List<CompositeRequest> compositeRequest ;
}

public class CompositeRequest{
    public String method;
    public Map<String, String> body;
    public String url;
    public String referenceId;
}

public static void createRequest(){
    CompositeRequestWrapper crw = new CompositeRequestWrapper();
    crw.allOrNone = true;
    
    List<CompositeRequest> requests = new List<CompositeRequest>();
    
    CompositeRequest cr = new CompositeRequest();
    cr.method = 'POST';
    cr.url = '/services/data/v40.0/tooling/sobjects/IPRange/';
    cr.referenceId = 'ref1';
    Map<String, String> o = new Map<String, String>();
    o.put('start', '1.1.1.1');
    o.put('end', '2.2.2.2');
    cr.body = o;
    requests.add(cr);

    CompositeRequest cr2 = new CompositeRequest();
    cr2.method = 'POST';
    cr2.url = '/services/data/v40.0/tooling/sobjects/IPRange/';
    cr2.referenceId = 'ref2';
    o = new Map<String, String>();
    o.put('start', '3.3.3.3');
    o.put('end', '4.4.4.4');
    cr2.body = o;
    requests.add(cr2);

    crw.compositeRequest = requests;
    
    String bodyToSend = JSON.serialize(crw);
    
    HttpRequest req = new HttpRequest();
    req.setBody(bodyToSend);
    req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
    req.setHeader('Content-Type', 'application/json');      
    req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v51.0/tooling/composite');
    req.setMethod('POST');
    Http h = new Http();

    HttpResponse res = h.send(req);
    
}
6
  • thank you very much for your answer. Ive tried to find the continuation of your code but I cannot implement it Aug 12, 2021 at 10:08
  • @JulioDidierMaciel I just updated my answer to include the full code. Aug 12, 2021 at 10:14
  • tried to execute your code on a anonymous block but didnt create the IPs. Aug 12, 2021 at 10:19
  • Strange - I tried this code in a new scratch org, and it created the Trusted IPs under Network Access. Are you seeing any errors in the HTTP response? Oh by the way - The code above goes into an Apex class, and then you need to call the createRequest() method. If you're running it as anonymous, you will need to move all the code outside the createRequest() method. Aug 12, 2021 at 10:48
  • or just call the createRequest() at the end. Aug 12, 2021 at 10:55

You must log in to answer this question.

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