You need to sign in to do that
Don't have an account?
Application TestingUser
Converting Richtext Inline image to Public URL Unknow Issue : You have uncommitted work pending. Please commit or rollback before calling out
Following code helps me to convert my RichText Images to Public URL by saving in Document Object
Trigger:
Batch:
Small size images are working perfect by larger size images fails.
ANY HELP WILL BE APPRECIATED !
Trigger:
trigger CaseComment on case (After insert, After update) { list<case > caselist = trigger.new; for(case cse:caselist) { if(cse.Steps_to_Reproduce__c .contains('/rtaImage')) { system.debug('cse[0].Steps_to_Reproduce__c '+cse.Steps_to_Reproduce__c); batchNotesInsert shn = new batchNotesInsert(cse.Id); database.executeBatch(shn); } } }
Batch:
global class batchNotesInsert implements Database.Batchable<sObject>,Database.AllowsCallouts, Database.Stateful { private String strParameter; public batchNotesInsert(String strParam) { strParameter = strParam; } global Database.QueryLocator start(Database.BatchableContext BC) { String query = 'select id from case where Id = :strParameter'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<case> scope) { String imgTagPattern; List<Case> cas = new List<Case>(); cas = [Select Id,Steps_to_Reproduce__c from Case where Id = :strParameter]; imgTagPattern = cas[0].Steps_to_Reproduce__c; List<String> parts = imgTagPattern.split('<img'); String mystring = ''; for (string imgvar : parts) { if(String.isBlank(imgvar)) { } else { String imageURL = imgvar.substringBetween('src="', '"'); String FinalUrl =imageURL.remove('amp;'); String fullFileURL=''; if(FinalUrl.contains('/rtaImage')) { list<Document> lstDocuments = new list<Document>(); PageReference pageRef = new PageReference(FinalUrl); Document docvar = New Document(); docvar.FOLDERID = [select id from folder where name = 'TFS'].id; docvar.type = 'jpg'; docvar.body = pageRef.getContent(); docvar.Name = 'TFS Inline Image'; docvar.IsPublic = true; Insert docvar; fullFileURL =+URL.getSalesforceBaseUrl().toExternalForm() + '/servlet/servlet.ImageServer?id=' + docvar.id + '&oid=' + UserInfo.getOrganizationId() ; } else { fullFileURL = FinalUrl; } if(!String.isBlank(imageURL)) { mystring =imgTagPattern.replace(imageURL , fullFileURL); imgTagPattern = mystring; cas[0].Steps_to_Reproduce__c = mystring ; } } } Update cas[0]; } global void finish(Database.BatchableContext BC) { } }
Small size images are working perfect by larger size images fails.
ANY HELP WILL BE APPRECIATED !
getContent is treated as a callout in API version 34.0 and later.
So I reckon what is happening here is that you have inserted a document first time through the loop, then the second time when you call getContent this is blocked as you have uncommitted work - the document that you inserted. You have two options as I see it:
1. Reduce the API version so that getContent isn't treated as a callout
2. Store all of your documents in a collection and insert them once you have completed processing of 'parts'