email resource name as string

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

email resource name as string

#1

Postby Segi » Wed Jan 31, 2018 9:18 pm

I have a globally defined function called sendEmailReport that I want to use to trigger an email resource to be sent with an optional file attachment.

In the server side script, I create a JS object to hold the properties which looks something like this:

Code: Select all

    var reportObj = {
          emailGroup: "CalibrationLogsDueSoon",
          emailResource: "CALIBRATIONLOGS_DUESOON_REPORT",
          callingForm: form.callingFormName,
          callingScript: "CALIBRATIONLOGS_DUESOON_REPORT_BEFOREFORM",
          fileAttachment: fileName,
     };
     
     sendEmailReport(reportObj);
sendEmailReport() is defined like this:

Code: Select all

function sendEmailReport(reportObj) {
	   // The properties must be defined!!!!
	   if (reportObj.emailGroup == null) {
	        sendAdminEmail("No email group was provided in call to sendEmailReport()","No email group was provided in call to sendEmailReport() which was called by the script " + reportObj.callingScript + (reportObj.callingForm != null ? " that was called by the form " + reportObj.callingForm : ""));	
	        return null;
	   }

	   if (reportObj.emailResource == null) {
	        sendAdminEmail("No email resource was provided in call to sendEmailReport()","No email resource was provided in call to sendEmailReport() which was called by the script " + reportObj.callingScript + (reportObj.callingForm != null ? " that was called by the form " + reportObj.callingForm : ""));	
	        return null;
	   }

	    // If the email resource doesn't exist, send an email to the admin because this shouldn't ever happen
           if (resources.getResource(reportObj.emailResource) == null) {
               sendAdminEmail("Invalid resource name","The email resource " + reportObj.emailResource + " is not valid. It was called by the script " + reportObj.callingScript + (reportObj.callingForm != null ? " that was called by the form " + reportObj.callingForm : ""));

               return null;
          }

	   if (reportObj.callingScript == null) {
	        sendAdminEmail("No calling script was provided in call to sendEmailReport()","No calling script was provided in call to sendEmailReport() which was called by the script " + reportObj.callingScript + (reportObj.callingForm != null ? " that was called by the form " + reportObj.callingForm : ""));	
	        return null;
	   }

	   // TO field must always exist. CC may not exist in a lot of cases where its not used
	   if (fields.getField("TO_RECIPIENTS") == null) {
	        sendAdminEmail("TO_RECIPIENTS field does not exist in call to sendEmailReport()","TO_RECIPIENTS field does not exist in call to sendEmailReport() which was called by the script " + reportObj.callingScript + (reportObj.callingForm != null ? " that was called by the form " + reportObj.callingForm : ""));	
	        return null;
	   }
	   
     var recipients=getEmailGroupMembers(reportObj.emailGroup);

     // getEmailGroupMembers() returns null if emailGroupName is not a valid Intranet email group
     if (recipients != null) {
          fields.TO_RECIPIENTS.value=recipients[0];

          if (fields.getField("CC_RECIPIENTS") != null) {
               fields.CC_RECIPIENTS.value=recipients[1];
          }

          // Include additional To recipients if provided
          if (reportObj.additionalToRecipients != null) {
               fields.TO_RECIPIENTS.value+=(fields.TO_RECIPIENTS.value != "" ? ";" : "") + reportObj.additionalToRecipients;
          }

          // Include additional CC recipients if provided
          if (reportObj.additionalCCRecipients != null && fields.getField("CC_RECIPIENTS") != null) {
               fields.CC_RECIPIENTS.value+=(fields.CC_RECIPIENTS.value != "" ? ";" : "") + reportObj.additionalCCRecipients;
          }
                    
          // Trigger email with attachment if one was specified
          if (reportObj.fileAttachment==null) {
              resources.getResource(reportObj.emailResource).sendmail();
          } else {
              resources.getResource(reportObj.emailResource).sendmail(reportObj.fileAttachment);
          }
     } else {
     	    // Send an email to the admin if an email group was specified that doesn't exist
          sendAdminEmail("Invalid email group","The email group " + reportObj.emailGroupName + " is not valid. It was called by the script " + reportObj.callingScript + (reportObj.callingForm != null ? " that was called by the form " + reportObj.callingForm : ""));

          return null;
     }
}
I am getting an error on the line resources.getResource(reportObj.emailResource).sendmail(reportObj.fileAttachment); (In my example, reportObj does provide a fileAttachment property) but i'm sure the line resources.getResource(reportObj.emailResource).sendmail(); would also generate an error.

The full error is:

Code: Select all

Can't find method com.ebasetech.ufs.runtime.external.api.impl.EmailResourceWrapper.sendmail(org.mozilla.javascript.ConsString). (GLOBAL_FUNCTIONS#830) 
You can do fields.getfield(string value").value="something" to reference a field based on a string why doesn't this work as well in the resources object ?

Update: I looked back at some of my other code where I call sendMail and realized that sendmail takes an object of attachments not a string. Wrapping the file name with [] so it reads resources.getResource(reportObj.emailResource).sendmail([reportObj.fileAttachment]); fixed the issue for me.
0 x

Who is online

Users browsing this forum: No registered users and 10 guests