Html field images are not attaching in sys attachment

jobin1
Tera Expert

Hi All,

 

HTML field images are not attaching in sys attachment record and it is working only when we  copy past or addition of images from the HTML filed header.

We have some custom UI actions called replay, replay all, and forward while initiating these UI actions all the images available in the HTML field have to be attached to the sys attachment of the particular record and this is happening only when we only when we copy past or addition of images from the HTML filed header. how to modify or add this logic to the existing OOB functionality? so that whenever we click on "replay all" the images in the HTML field will be added to sys attachment.
current Behavior.

jobin1_1-1695193509086.png

 

 

 

Expected behavior

jobin1_2-1695193616744.png

 



5 REPLIES 5

Peter Bodelier
Giga Sage

Hi Jobin,

Please share the script you tried with.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

jobin1
Tera Expert

 below is after update BR  when HTML filed changes.

(function executeRule(current, previous /*null when async*/) {

gs.log("htmlstart");

    // Get the HTML content from the u_html_message field

    var htmlContent = current.u_html_message;

gs.log("html1htmlContent"+htmlContent);

 

    // Create a regular expression to find image tags 

   

 

//<p class="MsoNormal"><img id="Picture_x0020_1" style="width: 3.4375in; height: 1.8333in;" width="330" height="176" /></p>

var imageRegex = /<img[^>]+>/g;

 

    var match;

//is not null. it checks if a match was found. If a match was found, the loop continues to execute; if no more matches are found, the loop exits.

    while ((match = imageRegex.exec(htmlContent)) !== null) {

        // Get the image URL from the src attribute

var imageUrl = match;

gs.log("html2imageUrl"+imageUrl);

 

        // Create a new attachment record

//var attachment = new GlideRecord('sys_attachment');

       var attachment = new GlideSysAttachment();

        // Set the parent record (u_email_client)

        attachment.setValue('table_name', 'u_email_client');

        attachment.setValue('table_sys_id', current.sys_id);

        

        // Set the file name 

        attachment.setValue('file_name', 'image.png');

 

        // Set the content type (you may want to customize this)

        attachment.setValue('content_type', 'image/png');

        

        // Set the content from the image URL

        attachment.writeBase64(gs.base64Encode(gs.httpStream(imageUrl)));

        

        // Insert the attachment record

        attachment.insert();

gs.log("html3insert");

    }

})(current, previous);

Thank you @jobin1,

 

I see you already added some debugging lines. Until where can you see your script is working. Are you finding the images in the html, and only not able to add as attachment, or does it fail at an earlier stage?


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

i am getting till the image url (gs.log("html2imageUrl"+imageUrl); is below

html2imageUrl<img id="Picture_x0020_1" style="width: 3.4375in; height: 1.8333in;" width="330" height="176" />

Now only not able to add as attachment