Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Fragment function "is not a function"

Avatar

Level 2

Hi All,

 

I am making a new fragment to help with some code a want to run often. I'm at the point where it should be working but whenever I attempt to reference it in the field I get an error saying "error: TypeError: form1.variables.employeeLookup.employeeLookupMain is not a function".

 

I have no idea what's wrong, I have a decent amount of javascript experience I can't see where I messed up the function. Can anyone help? Applicable code attached below:

 

Code that runs in the field: 

 form1.formPage.mainSub.initialUserData.empNo::exit - (JavaScript, client)
console.log("beggining of exit event");
//xfa.host.messageBox("1");
try{
	console.log("before the script call");
	var lookupReturn;
	//lookupReturn = form1.variables[0].employeeLookup.employeeLookupMain(this.rawValue);
	lookupReturn = form1.variables.employeeLookup.employeeLookupMain(this.rawValue);
	//lookupReturn = employeeLookup.employeeLookupMain(this.rawValue);
	console.log("after the script call");            
	if(lookupReturn == null){
		xfa.host.messageBox("Please enter a valid Employee number.");
		empNo.rawValue = null;
		initialUserData.name.rawValue = null;
	}//if
	else{
		//LINES BELOW MUST BE ALTERED TO REFERENCE THE FIELDS THAT THE LOOKUP NEEDS TO FILL
		//-----------------------
		name.rawValue = lookupReturn[fullName];
		//-----------------------
	}//else
}//try
catch(e){
	console.log('error: ' + e)
}//catch

 

employeeLookup fragment:

 form1.#variables[0].employeeLookup - (JavaScript, client)

//test build
//returns null if the lookup failed
//returns a tuple with all of the desired values if lookup succeded.
function employeeLookupMain(uNumber) {
	console.log("inside of lookup main");
	var funcReturn = null;
    
	var cURL = "/rest/services/SupportingApplication/GetUserByAssociateNo";
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("POST", cURL);
	try {
        xmlhttp.send(uNumber.rawValue);
        xmlhttp.onload = function () {
        	try{
        		xmlDoc = processHTTPReturn(xmlhttp.responseText);
        	}//try
        	catch(e){
        		console.log("error: " + e)
        	}//catch

            if (xmlDoc == null) {
                funcReturn = null;
            } //if
            else {
                funcReturn = {
                    //Set all responses to a return in the tuple
                    //each of these next statements is in this format
                    //var {variable name} = (xmlDoc.getElementsByTagName("{variable name}")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("{variable name}")[0].childNodes[0].nodeValue : null;
                    //{value stored} = {this side tries to get the variable specified from the xmlDoc. This returns true or false} ? {this value is stored if the statement returned true} : {this value is stored     if the statement returned false};
                    'fullName': ((xmlDoc.getElementsByTagName("fullName")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("fullName")[0].childNodes[0].nodeValue : null),
                    'kcpDivision': ((xmlDoc.getElementsByTagName("kcpDivision")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("kcpDivision")[0].childNodes[0].nodeValue : null),
                    'givenName': ((xmlDoc.getElementsByTagName("givenName")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("givenName")[0].childNodes[0].nodeValue : null),
                    'jobTitle': ((xmlDoc.getElementsByTagName("jobTitle")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("jobTitle")[0].childNodes[0].nodeValue : null),
                    'shift': ((xmlDoc.getElementsByTagName("shift")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("shift")[0].childNodes[0].nodeValue : null),
                    'currentDate': ((xmlDoc.getElementsByTagName("currentDate")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("currentDate")[0].childNodes[0].nodeValue : null),
                    'dropBay': ((xmlDoc.getElementsByTagName("dropBay")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("dropBay")[0].childNodes[0].nodeValue : null),
                    'kcpClearance': ((xmlDoc.getElementsByTagName("kcpClearance")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("kcpClearance")[0].childNodes[0].nodeValue : null),
                    'userLogin': ((xmlDoc.getElementsByTagName("userLogin")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("userLogin")[0].childNodes[0].nodeValue : null),
                    'payrollClass': ((xmlDoc.getElementsByTagName("payrollClass")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("payrollClass")[0].childNodes[0].nodeValue : null),
                    'pager': ((xmlDoc.getElementsByTagName("pager")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("pager")[0].childNodes[0].nodeValue : null),
                    'phone': ((xmlDoc.getElementsByTagName("phone")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("phone")[0].childNodes[0].nodeValue : null),
                    'surname': ((xmlDoc.getElementsByTagName("surname")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("surname")[0].childNodes[0].nodeValue : null),
                    'departmentNumber': ((xmlDoc.getElementsByTagName("departmentNumber")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("departmentNumber")[0].childNodes[0].nodeValue : null),
                    'lcuid': ((xmlDoc.getElementsByTagName("lcuid")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("lcuid")[0].childNodes[0].nodeValue : null),
                    'location': ((xmlDoc.getElementsByTagName("location")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("location")[0].childNodes[0].nodeValue : null),
                    'mailStop': ((xmlDoc.getElementsByTagName("mailStop")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("mailStop")[0].childNodes[0].nodeValue : null),
                    'mi': ((xmlDoc.getElementsByTagName("mi")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("mi")[0].childNodes[0].nodeValue : null),
                    'email': ((xmlDoc.getElementsByTagName("email")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("email")[0].childNodes[0].nodeValue) : null
                }; //return
            } //else


        } //onload
    } //try
    catch (e) {
        console.log("error: " + e);
    } //catch
    return funcReturn;
} //func

//functions to build the xmlDoc
//--------------------

function xmlEscape(xmlStr){
    try {
        console.log("xmlStr before replace: ");
        console.log(xmlStr);
        //THIS IS WHERE YOU PUT ANY REPLACES NEEDED
        try {
            xmlStr = xmlStr.replaceAll('&', '&');
        } //try
        catch (e) {
            try {
                xmlStr = xmlStr.replace(/&/g, '&');
            } //try
            catch (e) {
                console.log("error: " + e);
            } //catch
        } //catch
        console.log("xmlStr after replace: ");
        console.log(xmlStr);
        return xmlStr;
    } //try
    catch (e) {
        console.log("error: " + e);
    } //catch
} //func

function parseWithDOMParser(xmlStr) {
    return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
}; //func

function parseOnSuperOldBrowsers(xmlStr) {
    var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML(xmlStr);
    return xmlDoc;
}; //func

function parseReturnNull() {
    return null;
} //func
//--------------------

//processes the raw HttpReturn and returns a parsed xmlDoc. Takes care of logging related to the doc and confirming that the doc returned something useful
//if this returns null then clear all the related fields
function processHttpReturn(rawXmlString) {
    var replacedXmlString;
    var parseXml;
    try {
        replacedXmlString = xmlEscape(rawXmlString);
        
        if (window.DOMParser) {
            	try{
	                xmlDoc = parseWithDOMParser(replacedXmlString);
                }//try
                catch(e){
                	console.log("error: " + e);
                }//catch
            } //if
            else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
            	try{
	                xmlDoc = parseOnSuperOldBrowsers(replacedXmlString);
                }//try
                catch(e){
                	console.log("error: " + e);
                }//catch
            } //else if
            else {
                xmlDoc = parseReturnNull(replacedXmlString);
            } //else
        
    } //try
    catch (e) {
        console.log("error: " + e);
    } //catch

    //logging
    console.log("xmlhttp: ");
    console.log(xmlhttp);
    console.log("xmlDoc: ");
    console.log(xmlDoc);
    console.log("doc element name: ");
    console.log(xmlDoc.documentElement.nodeName);
    console.log("documentElement.getElementsByTagName(fullName)[0]");
    console.log(xmlDoc.documentElement.getElementsByTagName("fullName")[0]);
    console.log("documentElement.getElementsByTagName(fullName)[0].nodeName: ");
    console.log(xmlDoc.documentElement.getElementsByTagName("fullName")[0].nodeName);

    //error catch to see if the input value actually returned something
    try {
        xmlDoc.getElementsByTagName("fullName")[0].childNodes[0].nodeValue;
    } //try
    catch (e) {
        console.log("error: " + e);
        xmlDoc = null;
    } //catch

    return xmlDoc;
} //func

            

 

This fragment is attached to the form. Any help would be greatly appreciated. 

 

Side note: I know the code is littered with Try Catch's, which isn't great form, but it's really difficult to tell what's going on without them. I plan I taking most of them out after I'm finished developing. 

1 Accepted Solution

Avatar

Correct answer by
Level 5

I believe following is the correct way to call your function

lookupReturn = employeeLookup.employeeLookupMain(this.rawValue);

Also try changing the following line and see if that has any effect

'email': ((xmlDoc.getElementsByTagName("email")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("email")[0].childNodes[0].nodeValue) : null

 

to 

'email': ((xmlDoc.getElementsByTagName("email")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("email")[0].childNodes[0].nodeValue : null)

Basically move the last closing bracket.

View solution in original post

6 Replies

Avatar

Employee Advisor

@JohnSolomon0 form1.variables.employeeLookup.employeeLookupMain  does this even resolve correctly to the function?

Avatar

Level 2

No, that's the issue. That call just throws an error saying that it's "not a function". I don't understand why, I call a very similar function in a similar location and don't have this issue. 

Avatar

Level 2

Tried embedding per your suggestion, the behavior did not change. 

Avatar

Correct answer by
Level 5

I believe following is the correct way to call your function

lookupReturn = employeeLookup.employeeLookupMain(this.rawValue);

Also try changing the following line and see if that has any effect

'email': ((xmlDoc.getElementsByTagName("email")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("email")[0].childNodes[0].nodeValue) : null

 

to 

'email': ((xmlDoc.getElementsByTagName("email")[0].childNodes[0]) ? xmlDoc.getElementsByTagName("email")[0].childNodes[0].nodeValue : null)

Basically move the last closing bracket.

Avatar

Level 2

It looks like the function call worked either way, but that line change did it! Well... it threw other errors, but these ones were much more useful that the previous and I know how to fix them Thank you!