function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew LikensAndrew Likens 

Batch future calls calling to a web service that accepts JSON

Hello!

I have a future method that I want to pass account IDs and batch process them into a Set instead of passing one record at a time. I'm also calling to a web service that accepts JSON so do I have to serialize the set of account IDs in order for my web service to accept my set of account IDs? My code is below of what I'm trying to do but I get a 500 error.

Future method:
 
global class AccountUpsert{
  @future (callout=true)
   public static void AccountUpdate(Set<Id> accountId) {
     List<Account> accountList = [SELECT Id FROM Account WHERE Id IN :accountId];
     String str = JSON.serialize(accountList);
     Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://qa-svc01.salesforce.signzoneinc.com:49832/v1/Account/Updated');
request.setMethod('POST');
request.setBody(str);
request.setHeader('Content-Type', 'application/json');
request.setHeader('Content-Length', request.getBody());
request.setTimeout(120000);
HttpResponse response = http.send(request);
}
}

Account Trigger:
 
trigger AccountInsert on Account (before update, after update) {
    
    Set<Id> ids = new Set<Id>();

    for (Account acc : Trigger.new) {
        
            if (acc.Account_Status__c == 'Customer'
                && (System.IsBatch() == false && System.IsFuture() == false)
               ) 
            {
                ids.add(acc.Id);
                AccountUpsert.AccountUpdate(ids);
            } 
    }
}

Web Service:

User-added image

Any help is greatly appreciated!!​
Best Answer chosen by Andrew Likens
PawanKumarPawanKumar
Hi Andrew,
The code level everything seems fine. Now only problem seems to your endpoint. In order to verify endpoint problem, please follow below steps.

Can you please configure below url and run your code? 
http://putsreq.com/P36MXwyi3uCvhCJDfzii

Please add the below one your remote site setting before testing.
http://putsreq.com

Please let me know your result. I hope this time it should be 200(Not 500).
Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
Hi ,
Everything seems fine. can you please change line#12 as below and try?

request.setHeader('Content-Length', str.length());

Regards,
Pawan Kumar
PawanKumarPawanKumar
Please change your trigger as below:

trigger AccountInsert on Account (before update, after update) {
    
    Set<Id> ids = new Set<Id>();

    for (Account acc : Trigger.new) {
        
            if (acc.Account_Status__c == 'Customer'
                && (System.IsBatch() == false && System.IsFuture() == false)
               ) 
            {
                ids.add(acc.Id);
             } 
    }
 
// Just removed outide loop to avoid future Governer limit.   
   AccountUpsert.AccountUpdate(ids);
}
Andrew LikensAndrew Likens
Hi Pawan,

Thank you for the response! I put in str.length() and I received an error: "Method does not exist or incorrect signature: void setHeader(String, Integer) from the type System.HttpRequest"

Then I put single quotes around str.length() and I was able to save, but I still got a 500 error. Any other ideas?
PawanKumarPawanKumar
Hi Andrew,
The code level everything seems fine. Now only problem seems to your endpoint. In order to verify endpoint problem, please follow below steps.

Can you please configure below url and run your code? 
http://putsreq.com/P36MXwyi3uCvhCJDfzii

Please add the below one your remote site setting before testing.
http://putsreq.com

Please let me know your result. I hope this time it should be 200(Not 500).
Regards,
Pawan Kumar
This was selected as the best answer
Andrew LikensAndrew Likens
Hi Pawan,

I did receive a 200 this time! I now know there's something wrong with my endpoint. Thanks for your help!