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));
req.setHeader('Content-Type','application/x-www-form-urlencoded');
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.writeStartObject();
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);
gen.writeEndObject();
req.setBody('arg0='+ EncodingUtil.urlEncode(gen.getAsString(),'UTF-8.'));
// req.setEndpoint(ENDPOINT+'createRecord'+ EncodingUtil.urlEncode(gen.getAsString(),'UTF-8.'));
req.setEndpoint(ENDPOINT+'createRecord');
Http http = new Http();
HTTPResponse res = http.send(req);
return res.getBody();
}
Attribution to: NHL
Possible Suggestion/Solution #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.
Attribution to: pjcarly
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3607