+ Start a Discussion
Timmy AhluwaliaTimmy Ahluwalia 

Superbadge - Data Integration Specialist - challenge 3

Hi,
I am getting this error, i have check all the configrations of  challange
12:53:52:269 CALLOUT_RESPONSE [38]|System.HttpResponse[Status=Bad Request, StatusCode=400]
public class ProjectCalloutService {
    @InvocableMethod
    //Complete the implementation for business and callout logic
    public static void postOpportunityToPMS(list<Id> OpportunityId){
        list<opportunity> oppselected = [select Id, Name, Account.Name, CloseDate, Amount from opportunity where Id= :opportunityId ];
        system.debug('6 '+oppselected);
        system.enqueueJob(new QueueablePMSCall(oppselected));
    }
    
    public class QueueablePMSCall implements system.Queueable, Database.AllowsCallouts{
        List<opportunity> oppselected;
        public QueueablePMSCall(list<Opportunity>oppselected){
           this.oppselected = oppselected;
            system.debug('16 '+oppselected);
        }
        public void execute(QueueableContext context){
             
            String s2 = jsonoppselected(oppselected);
      //      system.debug(s2);
          InvokeCall(s2);
        }
    }
     
        public static Httpresponse InvokeCall(string s2){
        //    system.debug(s2);
            ServiceTokens__c serviceToken = ServiceTokens__c.getvalues('ProjectServiceToken');
         
            system.debug(serviceToken);
            Http http = new Http();
            HttpRequest request = new HttpRequest();
          //  request.setEndpoint('https://sb-integration-pms.herokuapp.com/projects');
            request.setEndpoint('callout:ProjectService');
            request.setMethod('POST');
            request.setHeader('Content-Type', 'application/json');
            request.setHeader('token', 'serviceToken');
            request.setBody(s2);
            
            HttpResponse response = http.send(request);
      
       //     system.debug(response.getStatusCode() + response.getStatus());
            if(response.getStatusCode()!=201){
                system.debug(response.getStatusCode() + response.getStatus());
            }else if(response.getStatusCode()==400){
                system.debug(response.getBody());
            }
            return response;
        }
      
        public static string jsonoppselected(list<opportunity> oppselected){
          system.debug(oppselected);
            JSONGenerator gen = JSON.createGenerator(true);
            gen.writeStartArray();
            for(Opportunity o : oppselected){
                gen.writeStartObject();
                gen.writeIdField('opportunityId', o.Id);
                gen.writeStringField('opportunityName', o.Name);
                 gen.writeStringField('accountName', o.Account.Name);
                gen.writeDateField('closeDate', o.CloseDate);
                gen.writeNumberField('amount', o.Amount);
               
                gen.writeEndObject();
            }
            gen.writeEndArray();
            system.debug(gen.getAsString());
            return gen.getAsString();
        }

    }
 
edanna kedanna k

Hello Timmy Ahluwalia,

Can you please post the JSON data which you are trying to POST?.
and one observation is that 

list<opportunity> oppselected = [select Id, Name, Account.Name, CloseDate, Amount from opportunity where Id= :opportunityId ];

//change the above one to one of the below samples
 list<opportunity> oppselected = [select Id, Name, Account.Name, CloseDate, Amount from opportunity where Id= :opportunityId[0] ];

OR
 list<opportunity> oppselected = [select Id, Name, Account.Name, CloseDate, Amount from opportunity where Id IN opportunityId ];
 

Thanks,
Edanna.
 

Aradhika Chawla 3Aradhika Chawla 3
public class ProjectCalloutService {
    
    //Complete the implementation for business through  Process builder and  callout logic  
    @InvocableMethod 
    public static void postOpportunityToPMS(List<ID> oppId ){
        System.debug('oppId = ' + 'oppId') ;
        Opportunity opp;
        try{
            opp = [Select id, Name , AccountId , CloseDate , amount from Opportunity where Id = :oppId[0]];    
            System.debug('opp = ' + opp) ;
        }
        catch(Exception e){
            System.debug('opp = ' +   e.getMessage()) ;
            
        }
        System.enqueueJob(new QueueablePMSCall(opp)) ; 
        
        System.debug('The status code returned was expected') ;
        
        
    }
    
    
    
    
    public class QueueablePMSCall implements Queueable, Database.AllowsCallouts {
        Opportunity opp;
        public  QueueablePMSCall(Opportunity opp){
            this.opp = opp;   
        }
        
        public void execute(QueueableContext queue) {          
            ServiceTokens__c serviceToken = new ServiceTokens__c();
            System.debug('token:='+ ServiceTokens__c.getInstance('ProjectServiceToken').Token__c); 
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint('callout:ProjectService');
            request.setMethod('POST');
            request.setHeader('token', ServiceTokens__c.getInstance('ProjectServiceToken').Token__c);
            request.setHeader('Content-Type', 'application/json');
            // Set the body as a JSON object
            String accName;
            Id oppId = opp.Id;
            String name = opp.Name;
            Decimal amount = opp.Amount;
            Date closeDate = opp.CloseDate;
            try{
                accName = [select id, name from Account where id =:opp.AccountId].name;
            }
            catch (Exception e){
                System.debug('no account');
            }
            request.setBody('{"opportunityId": "' + oppId + '","opportunityName": "' + name + '","accountName": "' + accName +  '", "closeDate": "'+closeDate+'","amount":' + amount + '}');
            //request.setBody('{"opportunityId": "00641000004EKmFAAW","opportunityName": "Super-Duper Big Deal","accountName": "ACME Corp, Inc.","closeDate": "2016-10-30","amount": 150000}');
            HttpResponse response = http.send(request);           
            System.debug('request '  + request );
            //Parse the JSON response
            if (response.getStatusCode() != 201) {
                System.debug('The status code returned was not expected: ' +
                             response.getStatusCode() + ' ' + response.getStatus());
                opp.StageName = 'Resubmit Project';
                
            } else {
                System.debug('response.getBody() success :=' + response.getBody());
                opp.StageName = 'Submitted Project';
                
                
            } 
            
            System.debug('opp.StageName ' + opp.StageName );
            update opp;
        }
        
        
    }
    
    
}
srilaxmi reddy 13srilaxmi reddy 13
// please try below code 
public class ProjectCalloutService {
      @InvocableMethod
    public static void postOpportunityToPMS(List<Id> oppoIds){
        Opportunity opp = [SELECT Id,Name,Account.Name,CloseDate,Amount FROM Opportunity WHERE Id = :oppoIds[0]];
        String serviceToken = ServiceTokens__c.getValues('ProjectServiceToken').Token__c;
        
        String jsonInput = '{\n' +
            ' "opportunityId" : "'+opp.Id+'",\n'+
            ' "opportunityName" : "'+opp.Name+'",\n'+
            ' "accountName" : "'+opp.Account.Name+'",\n'+
            ' "closeDate" : "'+String.ValueOf(opp.CloseDate).mid(0,10)+'",\n'+   
            ' "amount" : "'+opp.Amount+'"\n}';
            
        System.enqueueJob(new QueueablePMSCall(serviceToken, jsonInput, opp.Id));
    }    
    
    class QueueablePMSCall implements System.Queueable, Database.AllowsCallouts{
        private String serviceToken;
        private String jsonInput;
        private Id oppId;
        
        public QueueablePMSCall(String serviceToken, String jsonInput, Id oppId){
            this.serviceToken = serviceToken;
            this.jsonInput = jsonInput;
            this.oppId = oppId;
        }
        
        public void execute(QueueableContext qc){
            postToPMS(serviceToken, jsonInput, oppId);        
        }
    }
    
    @Future(callout=true)
    private static void postToPMS(String serviceToken, String jsonInput, Id oppoId){
        HTTPRequest req = new HTTPRequest();
        req.setEndPoint('callout:ProjectService');
        req.setMethod('POST');
        req.setHeader('token',serviceToken);
        req.setHeader('Content-Type', 'application/json;charset=UTF-8');
        req.setBody(jsonInput);
        
        HTTP http = new HTTP();
        HTTPResponse res = http.send(req);
        
        Opportunity opp = new Opportunity(Id=oppoId);
        if(res.getStatusCode() == 201){
            opp.StageName = 'Submitted Project';                
            System.debug('Success: ' + res.getStatus());
        }else{
            opp.StageName = 'Resubmit Project';
            System.debug('Failure: ' + res.getStatusCode() + ' ' + res.getStatus());
        }
        update opp;
    }
}
Akash Pandey 25Akash Pandey 25
Hi,

I have used JSONGenerator class (my favorite) for easy JSON generation. See snippet below
public class ProjectCalloutService {
 //outer class   
  class QueueablePMSCall implements Queueable, Database.AllowsCallouts{
 //inner class
    Opportunity o1;
    String body;
    public QueueablePMSCall(Opportunity opp, String bdy) {
        this.o1 = opp;
        this.body = bdy;
    }
    
    public void execute(QueueableContext qc) {
        
        Http http = new Http();
        String value = ServiceTokens__c.getValues('ProjectServiceToken').Token__c;
        HttpRequest request = new HttpRequest();
        request.setBody(body);
        request.setEndpoint('callout: ProjectService');
        request.setHeader('Content-Type', 'application/json');
        request.setHeader('token', value);
        request.setMethod('POST') ;   
        HttpResponse response = http.send(request);
    }
 }
    
    //Complete the implementation for business and callout logic
    @InvocableMethod 
    public static void PostOpportunityToPMS(List<ID> optyid){
         //invocable method will always have void response
        Opportunity opp1 = new Opportunity();
        opp1 = [select id, name, account.Name, closeDate, amount from Opportunity where ID = :optyid[0]];
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        JSONGenerator jsg =  JSON.createGenerator(true);
        jsg.writeStartObject();
        jsg.writeStringField('opportunityId', opp1.Id);
        jsg.writeStringField('opportunityName', opp1.Name);
        jsg.writeStringField('accountName ', opp1.account.name);
        jsg.writeStringField('closeDate ', opp1.CloseDate.format());
        jsg.writeNumberField('amount' , opp1.Amount);
        jsg.writeEndObject();
        String body = jsg.getAsString();
        System.enqueueJob(new QueueablePMSCall(opp1,body));       
    }
}

Please mark this as best answer in case it solves your doubts