Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list

Javascript not executing in right order


Level 4

Hey my great javascript buddies!!!  Okay So I have the following script on a click event:

var oDoc =

Initial.presence = "hidden"

Printinfo.presence = "visible"

oDoc.mailDoc ({


                         cTo: Name.rawvalue

                         cSubject: Hello


Why when I click the button does the email send first and then the field hide and become visible.  I need them to hide/visible before the form is emailed. 

What is even more weird is that sometimes it works find and other times it doesn't.  And I can't for the life of me figured out what is wrong!

Any help is always extremely appreciated!


6 Replies


Level 4

i am still looking for an answer to this question if anyone has any ideas



Level 7

Hi Jodi,

Some lines of your code are missing semicolons at the end.


Level 4

This is my actual script. (the above was just an example for ease) even when adding semi colons It still emails first and then hides the fields.  I even added the emailme, thinking that it would have to go through those steps first to get to the email me, but still no luck .

if (errorcount == 0)

                {Main.Type.presence = "hidden";

{var tables = Main.GIKDetails.Donor.all;

for (j = 0; j < tables.length; j++)

{if (tables.item(j).NewAdd.rawValue == "0")

{tables.item(j).NewAdd.presence = "hidden"};

if (tables.item(j).ForeignAddress.rawValue == "0")

{tables.item(j).ForeignAddress.presence = "hidden"};

tables.item(j).Spacer6.presence = "hidden";


                {var tables = Main.GIKDetails.Donor.all;

                    for (j = 0; j < tables.length; j++)

                        {var table = tables.item(j);

                         var fund = table.FundInfo.all;

                         for (i = 0; i < fund.length; i++)

                         {var row = fund.item(i)


                if (fund.item(i).NewFund.rawValue == "0")

                {fund.item(i).NewFund.presence = "hidden";

                 fund.item(i).NewFundContact.presence = "hidden";

                 fund.item(i).NewFundEmail.presence = "hidden";}



                fund.item(i).NonGift.presence = "hidden";


                if (fund.item(i).Opportunity.Opportunity2.No.rawValue == "2")

                {fund.item(i).Opportunity.presence = "hidden"};


                fund.item(i).Tribute.TributeTxt.presence = "hidden";

                fund.item(i).Tribute.Tribute2.presence = "hidden";


                fund.item(i).AddDesig.presence = "hidden";

                fund.item(i).Spacer3.presence = "hidden";

                fund.item(i).RemoveDesig.presence = "hidden";

                emailme = emailme + 1;









{if (emailme == 1)

{var oDoc =;


            bUI : true,

            cTo : "",

            cCc : "",

            cSubject : "Gift In Kind - Gift Transmittal",

            cMsg : "",



Level 7

Hi Jodi,

I have sometimes also had problems with things not executing in the order i like, so I go about it another way.

What you can try is this:

  • Add a new button that will be Hidden and add the email code to the click event
  • In the button you are clicking above add: newButton.ExecEvent("click"); //this will click the new hidden button programmatically

Because your original button is conditionally sending the email if the emailme variable = 1, you might need to add another condition that will end the code.

For example

if (emailme == 1)


newButton.ExecEvent("click"); //this will click the new hidden send email button programmatically


if (emailme == 0)


end; //end the execution of the code



Level 4

Still doesnt work. the only reason for the condition was so that maybe it would prevent the email from happening before hiding the other fields. 

So I removed the conditions and put the newButton.ExecEvent("click') at the end of all the hiding conditions and it still fires before it hides the fields.


Level 7

What about if you just had a separate email button that shows after the 'processing' has been done for hiding the fields? Just means the user will have to click another button, but at least it will do the email when you want.