Array problem

I’m attempting to attach a series of 6 files to an eMail. Each file has already been imported into a blob. I create a new array. I declare a variable for sequencing the array.

Works just fine when there’s just one file to be attached, but balks at the second or subsequent.

	var vTo = email_to;
	var vCC = email_cc;
	var vBCC = email_bcc;
	var vFrom = guseriddefault$to_email.email;
	var vSubject = cor_subject;
	var vText = letter;

	var vAttachArray = new Array();
	var vI = 0 ;
	if ( attach_file )
	{
		vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file, attach_blob);
		vI += 1;
	}
	if ( attach_file2 )
	{
		vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file2, attach_blob2);
		vI += 1;
	}		
	if ( attach_file3 )
	{
		vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file3, attach_blob3);
		vI += 1;
	}
	if ( attach_file4 )
	{
		vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file4, attach_blob4);
		vI += 1;
	}
	if ( attach_file5 )
	{
		vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file5, attach_blob5);
		vI += 1;
	}
	if ( attach_file6 )
	{
		vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file6, attach_blob6);
		vI += 1;
	}		
		
	// one more available argument -- [overridePreferenceSMTPHost/properties array]
	var vSuccess = plugins.mail.sendMail( vTo, vFrom, vSubject, vText, vCC, vBCC, vAttachArray )
		
	if ( vSuccess )
	{
		var vTitle = i18n.getI18NMessage('7office.dlg.correspondence');
		var vMsg = i18n.getI18NMessage('7office.dlg.emailsuccess');
		var vBtn = plugins.dialogs.showInfoDialog(vTitle, vMsg);
		// eMail sent	
		
		cor_date = new Date();
	}

I can’t really see what goes wrong here. What do you mean by “balk”?

A general question: what if I have seven attachments?

Why don’t you just create a table

email_attachments (
   email_id int,
   attachment_name varchar, 
   attachment blob)

make a relation and loop over that?

Also, a shorter way of doing vI += 1; is vI ++;

patrick:
I can’t really see what goes wrong here. What do you mean by “balk”?

When two or more attachments are supplied only the first one is attached to the eMail. No sign of the others.

patrick:
A general question: what if I have seven attachments?

Why don’t you just create a table

email_attachments (

email_id int,
attachment_name varchar,
attachment blob)



make a relation and loop over that?

Also, a shorter way of doing vI += 1; is vI ++;

I could have gone that way. Instead the UI for attachments is a FID with six Add buttons. The process of adding imports the file into a blob, displays the file name and its path. Also includes a Delete button. I’ve assumed six attachments is enough to get this solution rolling.

Obviously something is wrong with my code. You’re generally quite astute in these matters, so the bug isn’t as obvious as I assumed.

Why do you use a var “vI”?
If you have just 6 blob fields you should create an array of lenght 6:

var myArray = new Array(6);

and reference directly like:

myArray[0]
myArray[1]
...
myArray[5]

Your code will fail if the user has attached a file in blob2 but not in blob1.
Try to rewrite the methods without the var and then debug.

ngervasi:
Your code will fail if the user has attached a file in blob2 but not in blob1.
Try to rewrite the methods without the var and then debug.

I find the whole method pretty strange, too. But I don’t see where it should fail.

If only blob2 is filled, but not blob1, then it should look like this:

var vAttachArray = new Array();
   var vI = 0 ;
   if ( attach_file )  // we don't enter here
   {
      vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file, attach_blob);
      vI += 1;
   }
   if ( attach_file2 )  // we do enter here, vI is 0 at that point
   {
      vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file2, attach_blob2); // we add to the Array at index 0
      vI += 1;  // for whatever other blob we increase from 0 to 1
   }

So actually I don’t see how it can go wrong. My impression is more that the other blobs are not filled.

Morley, why don’t you change the thing to:

if ( attach_file2 )  // we do enter here, vI is 0 at that point
   {
      application.output("I have an attach_file2");
      vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file2, attach_blob2); // we add to the Array at index 0
      vI += 1;  // for whatever other blob we increase from 0 to 1
   }

and see what you get in the output. But in general, I recommend using the debugger to see what is going on…

Morley:

if ( attach_file )
{
	vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file, attach_blob);
	vI += 1;
}
if ( attach_file2 )
{
	vAttachArray[vI] = plugins.mail.createBinaryAttachment(attach_file2, attach_blob2);
	vI += 1;
}



Morley, you're not checking for the blob field (attach_blob) but for the file name (attach_file) in the if() statement.
Are you sure your blob fields contain data? And your file_name fields?

ngervasi:
Morley, you’re not checking for the blob field (attach_blob) but for the file name (attach_file) in the if() statement.
Are you sure your blob fields contain data? And your file_name fields?

Yes, I’d rather check the blob fields but haven’t found a means to do so.

if ( attach_blob )
```returns a JavaScript error. <a class="postlink" href="http://forum.servoy.com/viewtopic.php?t=5412">http://forum.servoy.com/viewtopic.php?t=5412</a> explains. Has had no response.

Success! Coding error in the attachment process. The blobs were indeed empty :oops:

Also discovered a workable test for whether a blob has content. ```
if (blob == null)

Morley,

don’t get me wrong: but that’s what you have a debugger for. In the debugger you can see exactly what variable has which value and so forth. You can use “evaluate”, simply type a statement and see what is going on. That saves you a lot of headache. I am one of the ex FM crowd here and I can tell you that a decent debugger like the one you have here is one of the most valuable things that you have…

patrick:
don’t get me wrong: but that’s what you have a debugger for. In the debugger you can see exactly what variable has which value and so forth. You can use “evaluate”, simply type a statement and see what is going on. That saves you a lot of headache. I am one of the ex FM crowd here and I can tell you that a decent debugger like the one you have here is one of the most valuable things that you have…

I use the debugger extensively, but not confidently. I find its user interface confusing and its documentation virtually non-existent.

come over, I give you a training :wink: