• Application TestingUser
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Following code helps me to convert my RichText Images to Public URL by saving in Document Object

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 !​