Script to auto attach website_image

Hi All,

I am trying to write a script to auto attach a website_image file which has already been uploaded to /public/files/.

Using set_value(“website_image”,“/files/img.jpg”) does not work as field is of type ‘attach’.

So. I have found
frappe-bench/apps/frappe/frappe/public/js/frappe/form/control.js
which is the code which is run when attach button is pressed.

Hence, I have the following script. But I am not sure if i am on the right lines?

Comments much appreciated.

Thanks

Ben

cur_frm.cscript.auto_create_slideshow = function(doc, cdt, cdn) {

if(doc) {
	
	if (doc.__islocal) {
		frappe.msgprint(__("Please save the document before uploading."));
		return;
	}
}


    frappe.call({
   method:'erpnext_ebay.auto_slideshow.process_new_images',
       args: { item_code: item_code },
       callback:function(r)
   {
	if(r)
	{
		cur_frm.set_value("slideshow", "SS-" + item_code);

		// Cannot do this - as field is type "Attach"
		//cur_frm.set_value("website_image", "/files/" + item_code + "-0" + ".jpg");


                    // need to use attach controller instead
		if(doc) {
			doc.parse_validate_and_set_in_model("/files/" + item_code + "-0" + ".jpg");
			doc.refresh();
			doc.attachments.update_attachment(attachment);
			doc.save();
		} else {
			this.value = this.get_value();
			this.refresh();
		}

	}
	else {
		msgprint("Problem. Please manually set up slideshow and website image.");

	}
       }
    })

}

Hi there,

I have same requirement, did you find solution for this?

Hi Mohammed,

No, I’m sorry I haven’t had chance to do this.
It is on my todo list.

I’ll let you know if I make some progress in the future.

Thanks

Ben

Thank you for your fast respond,
just for your record I solve part of the problem bu this code

if you want to set image by script use this one :

make website_image field type as Image then

cur_frm.set_df_property(‘website_image’, ‘options’, “/private/files/website_image-150x150.jpg”);
cur_frm.refresh_field(‘line_name’);

@Mohammed_Redha,

Thanks for that, I will give it a go.

Incidentally, if you are interested in doing similar thing with Slideshow, then I did some code that speeds up creation of Slideshows.

and the corresponding script below.

Thanks

Ben

cur_frm.cscript.auto_create_slideshow = function(doc, cdt, cdn) {

var item_code = doc.name;
	if(doc) {
		
		if (doc.__islocal) {
			frappe.msgprint(__("Please save the document before uploading."));
			return;
		}
	}


        frappe.call({
	   method:'erpnext_ebay.auto_slideshow.process_new_images',
           args: { item_code: item_code },
           callback:function(r)
	   {
		if(r)
		{
			cur_frm.set_value("slideshow", "SS-" + item_code);

			doc.refresh_field("slideshow");
			frappe.msgprint("Hopefully slideshow created");

		}
		else {
			msgprint("Problem. Please manually set up slideshow and website image.");

		}
           }
        })

Hi @Mohammed_Redha

I have almost same requirements.

i want to upload my signature whenever Workflow_state is Approved by Manager. Image of signature already uploaded in files of erpnext. i want to set this image in Purchase order Custom Signature Field. Can i ?

Any help will be appropriated.

Thanks