+ 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;
        }
        
        
    }
    
    
}