0

I'm bulkifying my current code to handle repetitive tasks.Previously the code was like below:

 Http httpCall = new Http();
        HttpRequest request = new HttpRequest();
        if(!Test.isRunningTest()) {
            request.setEndpoint('callout:ISAM');   
        }else {
            request.setEndpoint(endpointsUrl);
        }
        request.setMethod('POST');
        request.setTimeout(120000);
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody(requestBody);
        HTTPResponse res;
        if(Test.isRunningTest()) {
            res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody('{"success":"true"}');
            res.setStatusCode(200);
        }else {
            res = httpCall.send(request);
        }

Now I modified the code and it looks like this :

Http httpCall = new Http();

        for(String oldEmail:key){
        IntegrationEndpoints__c registrationEndpoint = IntegrationEndpoints__c.getValues('Registration');
        String endpointsUrl = registrationEndpoint.EndpointUrl__c;

        HttpRequest request = new HttpRequest();
        String newEmail =  validEmailIDs.get(oldEmail);         
        Map<String, Object> requestPost = new Map<String, Object>();
        requestPost.put('newuserid', newEmail);
        requestPost.put('userid', oldEmail);
        requestPost.put('requesttype', 'changeusername');
        String jsonRequestBody = JSON.serialize(requestPost);

        if(!Test.isRunningTest()) {
            request.setEndpoint('callout:ISAM');   
        }else {
            request.setEndpoint(endpointsUrl);
        }
        request.setMethod('POST');
        request.setTimeout(120000);
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody(jsonRequestBody);
               system.debug('requestBody'+jsonRequestBody);

        httpReqList.add(request);    
        }

        HTTPResponse res;
        if(Test.isRunningTest()) {
            res = new HttpResponse();
          res.setHeader('Content-Type', 'application/json');
          res.setBody('{"success":"true"}');
          res.setStatusCode(200);
            system.debug('call to webservice1 test'+httpReqList.size());

        }else {
            system.debug('call to webservice1'+httpReqList.size());
           // res = httpCall.send(httpReqList);
        }

And I'm getting the below error.

Method does not exist or incorrect signature: [System.Http].send(List)

Any help to solve this?

2 Answers 2

2

The Http send method must be passed a single HttpRequest object, but the body set on that can be a string that represents a JSON array (AKA a list).

Assuming requestPost is a list of objects, you could use:

HttpRequest request = new HttpRequest();
...
String jsonRequestBody = JSON.serialize(requestPost);
request.setBody(jsonRequestBody);
...
res = httpCall.send(request);

But you need to check what the third part end point will accept: often such end points won't accept multiple objects at once and you are stuck with having to make individual requests.

0

you can try using the JSONGenerator

Something like:

JSONGenerator generator = JSON.createGenerator(true);
generator.writeStartObject();
generator.writeObjectField('requestPost',requestPost);
generator.writeEndObject();
String JSONString = generator.getAsString();
System.debug('#JSONString: '+JSONString);

Hope it helps

You must log in to answer this question.

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