I have a trigger on task when new task is created then trigger will fire and if condition is true it 'completed the status of task. My requirement is that before completing the status of task , if condition is true I need to attach a pdf into the contract, which is related to that task. Below is my code, it's throwing me an error [HttpResponse[Status=Bad Request, StatusCode=400].Please help , I am new to salesforce.

 for(Task t: supportCode){
        if(String.isNotBlank(t.type) && t.type.toLowerCase() == System.Label.SWPKeyUpdateType.toLowerCase() 
                && String.isNotBlank(t.Status) && t.Status.toLowerCase() == System.Label.SWPKeyUpdateStatus.toLowerCase() 
                && String.isNotBlank(t.Subject) && (t.Subject.toLowerCase() == System.Label.SWPKeyCreateSubject.toLowerCase() || t.Subject.toLowerCase() == System.Label.SWPKeyUpdateSubject.toLowerCase())) {
            contIDMap.put(t.WhatID, t); 
         system.debug('task  chk'+ t);
         system.debug('contract chk111'+ contIDMap.size());
    if(contIDMap.size() > 0){ 
        Set<ID> taskIDtoUpdate = new Set<ID>();
        for(Contract ct : [SELECT Name,Id FROM Contract WHERE ID IN : contIDMap.keyset() AND Account.Support_Code__c != null]){

            GenerateContractLicensePdf(ct.Id , UserInfo.getSessionId()); 
        List<Task> taskLstUpdate = new List<Task>();
        for(Task tk : [Select Status From Task Where ID IN : taskIDtoUpdate]){
            tk.Status = 'Completed';

        update taskLstUpdate;

This is My future method , which is called from GenerateContractLicensePdf method.

public static void GenerateContractLicensePdf(Id ContractId, String userSessionId)
     system.debug('checking '+ContractId);
     system.debug('checking '+userSessionId);
    //Replace below URL with your Salesforce instance host
    String addr = 'https://' + Label.Org_Instance + '.salesforce.com/services/apexrest/attachContractLicensePdf'; 

    HttpRequest req = new HttpRequest();

    req.setHeader('Authorization', 'OAuth ' + userSessionId);

    Map<String, String> postBody = new Map<String,String>();
    system.debug('checking123 '+ContractId);
    postBody.put('ContractID', ContractId);
    String reqBody = JSON.serialize(postBody);

    system.debug('checking123 body '+reqBody);
    req.setEndpoint( addr );  
    Http http = new Http();
    HttpResponse response = http.send(req); 

} Here I am getting the ContractId and UserSessionId. Below is my rest Resource class.

Global without sharing class AttachContractLicense{
    global static void attachPDF(ID ContractId) { 
        system.debug('find value here'+ContractId );
        //Contract cont = [Select id from Contract where Id =: ContractId];

        system.debug('find value here'+ContractId);

        PageReference PDfref = Page.ContractLicensePdf;

        system.debug('find value here'+ContractId);

        Blob b = PDfref.getContentAsPDF(); 
        Attachment attach  = new Attachment();
        attach.ContentType = 'application/pdf';
        attach.Body = b ;
        attach.ParentId = ContractId;
        attach.Name = 'Parallels Inc  Contract License';
        insert attach;

        system.debug('Attach' + attach);  
        catch(Exception e){ 
  • What line is throwing the error? FYI a HTTP 400 error means that you sent a bad request.
    – BarCotter
    Commented Jun 9, 2015 at 9:29
  • Rest Resource class not sending any response.
    – SFDC 44
    Commented Jun 9, 2015 at 10:11
  • What line is throwing the error?
    – BarCotter
    Commented Jun 9, 2015 at 10:28
  • @ BarCotter : HttpResponse response = http.send(req); , This line throws the error. Please Help HttpResponse[Status=Bad Request, StatusCode=400]
    – SFDC 44
    Commented Jun 9, 2015 at 11:19
  • Your Map may have to be Map<String, Id> as the endpoint is expecting an Id
    – BarCotter
    Commented Jun 9, 2015 at 11:42

1 Answer 1


I recently had to do something very similar so maybe my working implementation can help you.

For this line:

  • String addr = 'https://' + Label.Org_Instance + '.salesforce.com/services/apexrest/attachContractLicensePdf';

I used this way:

  • https:// + URL.getSalesforceBaseUrl().getHost() + '/services/apexrest/attachContractLicensePdf/'

For this line:

  • req.setHeader('Authorization', 'OAuth ' + userSessionId);

I used Bearer instead of OAuth:

  • req.setHeader('Authorization', 'Bearer ' + userSessionId);

For setting the req.Body, I used this one line:

  • req.setBody('{"ContractId":' + JSON.serialize(ContractId)+'}');

Also, I noticed that when you are setting the req body, you are using 'ContractID', but on your Rest Resource, it is expecting 'ContractId'. I had this exact same spelling discrepancy and was receiving the error message you are. This is most likely the problem.

You must log in to answer this question.

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