Skip to main content
Participating Frequently
June 18, 2020
Answered

(If statement) to insert image based on a value.

  • June 18, 2020
  • 1 reply
  • 1318 views

I am using AutoMailMerge and I am trying to add a JavaScript to check if a field value is "1"  then insert image in the image field. It's working with this code, but it doesn't do the else if conditions, all exported/processed PDFs contains the first image only (Circle1.png).

 

This is the code:

var f = this.getField("Image1_af_image");
if (f.value = 1)
{
   f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle1.png");
} else if (f.value = 2) 
{
  f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle2.png");
} else if (f.value = 3) 
{
  f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle3.png");
}

 

This topic has been closed for replies.
Correct answer BarlaeDC

Okay I chagned the code to this: But it deosn't insert any image to the filed.
For your queastion I get the filed number from a excel sheet using AutoMailMerge 

var f = this.getField("Image1_af_image");
if (f.value == "1")
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle1.png");
} else if (f.value == "2") 
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle2.png");
}


This link contains 3 processed files with this above code with 3 diffrent values for "Image1_af_image".
https://gofile.io/d/qMvnfu 


Hi,

 

I am confused as to how f which points to a field which contains the numbers 1,2, or 3, and then f is also being used for the buttonImportIcon.

 

In the forms you have posted the number is contianed in a field called "Number", therefore shouldn't the code be something like :

 

var f = this.getField("Image1_af_image");
var n = this.getField("Number");
if (n.value == "1")
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle1.png");
} else if (n.value == "2") 
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle2.png");
}

 

Hope this helps

 

Malcolm

1 reply

BarlaeDC
Community Expert
Community Expert
June 18, 2020

Hi,

 

You need to add a second '=' to your if statements

= - means set the value to

== - means compare

 

so the if becomes

var f = this.getField("Image1_af_image");
if (f.value == 1)
{
   f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle1.png");
} else if (f.value == 2) 
{
  f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle2.png");
} else if (f.value == 3) 
{
  f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle3.png");
}

 

Hope this helps

 

Malcolm

Participating Frequently
June 18, 2020

I am sorry! but it didn't work, also it didn't change the first image like before.

 
BarlaeDC
Community Expert
Community Expert
June 18, 2020

Hi,

 

I won't change anything unless the value of "Image1_af_image" is the number 1, 2, or 3.  Is that field a drop down or list which has these values specified, or is the user typing them in?

 

Have you checked the console for an error?

 

If you can share the document or post it so we can see then we can help more, all I did was correct you JavaScript syntax.

 

Regards

 

Malcolm