When I upload a file from the directory (and save it as an attachment) and send it with REST webservices, it works great, but when I get a file from Salesforce (from chatter for example) it doesn't work...

I tried to change the type of the file, but it still doesn't work.

I also tried once to get a file from attachment and attach this one to an object and tried to send it after but it didn't work.

I think it's a problem of type (blob, base 64..)

What can cause this problem?

I have this error:

System.CalloutException: You have uncommitted work pending. Please commit or rollback 
before calling out Erreur dans l'expression '{!createFolder}' du composant <apex:page> dans
page signeco_creatasigneco Class.Signeco_Connector.createFolder: line 125, column 1 
Class.CreateSignecoController.createFolder: line 350, column 1 the firstg concern this    one:
HTTPResponse res = http.send(req); The second: cf Signeco_Connector.createFolder( folder,
fileToSignEnv, destinataires); 

// the webservice call I use REST webservices.

Here is my code the problem is that th id of the attachment is null, i use to store information , so i cant put an id, do i have to inser it and update after ???

// Visualforce page

// controlleur methode

public PageReference createFolder() { fileToSign = ([SELECT Id, Type, CreatedDate, Body, Title, LinkUrl, ContentData, ContentSize, CreatedById, CreatedBy.FirstName, CreatedBy.LastName, ParentId, Parent.Name, ContentFileName, ContentType From FeedItem where Type = 'ContentPost' and ParentId=: folder.id order by createddate, ID DESC Limit 1]);

fileToSignEnv.body = fileToSign.ContentData;
fileToSignEnv.ParentId = folder.id;
fileToSignEnv.name= 'Test';

fileToSign= null;

//fileToSignEnv.body = Encodingutil.base64Decode(String.valueOf(fileToSignEnv.body));
insert fileToSignEnv;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Le body de chatter!'+fileToSignEnv.body));
   if (folder.Id == null ||  fileToSignEnv.Id == null ||destinataires.get(0) == null) {  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Veuillez renseigner les champs obligatoires'+fileToSignEnv.Id ));}
   else { System.debug('voici le doc'+fileToSign.ContentData);
          cf = Signeco_Connector.createFolder(folder,  fileToSignEnv, destinataires);
          fileToSign= null;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Your folder has been send to be signed!'));
        System.debug('voici l\'affichage'+cf);
        Map<String, Object> resultJSON = (Map<String, Object>)JSON.deserializeUntyped(cf);
        folder.SignecoId__c = (String) resultJSON.get('reference');
       // cf = '###  '+resultJSON;
        update folder; 
   return null;

// REST webservice

public static String createFolder(Signeco__c folder, Attachment docToSign, List dest) { HttpRequest req = new HttpRequest(); //req.setEndpoint(ENDPOINT+'createRecord?arg0=1'); req.setMethod('POST');

    Blob userNameValue = Blob.valueOf(USERNAME);
    Blob passwordValue = Blob.valueOf(PASSWORD);
    req.setHeader('X-OTC-Auth-Ident', EncodingUtil.base64Encode(userNameValue));
    req.setHeader('X-OTC-Auth-Password', EncodingUtil.base64Encode(passwordValue));

System.debug('On est dans la classe conencteurs'+docToSign.body); List docs = new List(); docs.add(new Document(docToSign));

    List<Recipient> recps = new List<Recipient>();

    for (Destinataires_Signeco__c d : dest) {
        recps.add(new Recipient('PERSONAL',d.Email__c));

    JSONGenerator gen = JSON.createGenerator(true);
    gen.writeStringField('service', 'Keynectis SIGNECO Service');
    gen.writeStringField('subject', folder.Name);
    gen.writeStringField('comment ', folder.description__c);
    gen.writeObjectField('documents', docs);
    gen.writeObjectField('recipients', recps);
    req.setBody('arg0='+ EncodingUtil.urlEncode(gen.getAsString(),'UTF-8.'));

   // req.setEndpoint(ENDPOINT+'createRecord'+ EncodingUtil.urlEncode(gen.getAsString(),'UTF-8.'));

    Http http = new Http();
    HTTPResponse res = http.send(req);

   return res.getBody();

  • 1
    Can you provide the error you are getting?Also the service you are using? Oct 23, 2012 at 8:03
  • added your error and code sample to the question.
    – Saariko
    Oct 23, 2012 at 9:03

1 Answer 1


If I look at your error message:

You have uncommitted work pending. Please commit or rollback before calling out

this means that you make a (rest) webservice call, after you started a database transaction, either you do your webservice call before doing DML statements, or you do your webservice call in an @future method.

  • Thank you, I deleted "insert fileToSignEnv;" i don't need it. The problem is thate fileToSignEnv.id is null and it's normal because i only use it to put my information in it so i did public Attachment fileToSignEnv { get { if (fileToSignEnv == null) fileToSignEnv = new Attachment(); return fileToSignEnv ; } set; } How to give it an id (id is not writable)
    – NHL
    Oct 23, 2012 at 8:43
  • You can instantiate a new object with its id in the constructor, like so Attachment a = new Attachment(Id = xxxxx);
    – pjcarly
    Oct 23, 2012 at 8:49
  • then you can do dml statements on the sobject, and the corresponding sobject in the database will be affected
    – pjcarly
    Oct 23, 2012 at 8:50
  • One last question please! It show this error System.TypeException: Invalid id value for this SObject type: a01E000000DkhWgIAJ what i should put ???
    – NHL
    Oct 23, 2012 at 9:10
  • this means that the id provided isn't an id from an Attachment.
    – pjcarly
    Oct 23, 2012 at 9:39

You must log in to answer this question.

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