0

I am trying to populate the two grids based on the ending part of the email address.

My if condition is not working and all the records are landing in the tblExternal.

varAttendeeId is a variable and the result is the list of campaign members.

There is a mapquery function that returns the list of record(s) selected.

Please suggest how I can use the email address from the mapquery in the if statements here.

function addAttendeetoCampaign(){
        varAttendeeId = '';
  jq( ".attendeeChk:checked" ).each( function() { varAttendeeId+=jq(this).val()+","; } );

        if(varAttendeeId != '') {
            var result= Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.EventPortal_Detail.addToCampaignMember}',
                                                                                                                            varAttendeeId,
                                                                                                                            CToken,
                                                                                                                            UToken,
                                                                                                                            EToken,
                                                                                                                            function handleResultresponse(result, event) {
                    if (event.status){
                            // Insert a row in the External Invitees table as the last row if type is External
                            for( i=0; i<result.length; i++) {
                                if (varAttendeeId.indexOf('xyz.com') != -1) {
                                    var tableRef = document.getElementById('tblInternal').getElementsByTagName('tbody')[0];
                                    result.forEach(function(record) {
                                        // Insert a row as the last row
                                        var newRow   = tableRef.insertRow(tableRef.rows.length);

                                        // Insert a cell in the row at index 0
                                        var newCell1  = newRow.insertCell(0);
                                        var newCell2  = newRow.insertCell(1);
                                        var newCell3  = newRow.insertCell(2);
                                        var newCell4  = newRow.insertCell(3);
                                        var newCell5  = newRow.insertCell(4);
                                        var newCell6  = newRow.insertCell(5);

                                        // Append a text node to the cell
                                        newCell1.innerHTML = mapAttendeeQuery[record.AttendeeId].strName;
                                        newCell2.innerHTML = mapAttendeeQuery[record.AttendeeId].CompanyName;
                                        newCell3.innerHTML = "";
                                        newCell4.innerHTML = "";
                                        newCell5.innerHTML = mapAttendeeQuery[record.AttendeeId].strEmail;
                                        newCell6.innerHTML = "";
                                    })
                                }
                                else
                                { //Else Insert row as the last row in External Invitees table
                                    var tableRef = document.getElementById('tblExternal').getElementsByTagName('tbody')[0];
                                    result.forEach(function(record) {
                                        // Insert a row in the table at the last row
                                        var newRow   = tableRef.insertRow(tableRef.rows.length);

                                        // Insert a cell in the row at index 0
                                        var newCell1  = newRow.insertCell(0);
                                        var newCell2  = newRow.insertCell(1);
                                        var newCell3  = newRow.insertCell(2);
                                        var newCell4  = newRow.insertCell(3);
                                        var newCell5  = newRow.insertCell(4);
                                        var newCell6  = newRow.insertCell(5);

                                        // Append a text node to the cell
                                        newCell1.innerHTML = mapAttendeeQuery[record.AttendeeId].strName;
                                        newCell2.innerHTML = mapAttendeeQuery[record.AttendeeId].CompanyName;
                                        newCell3.innerHTML = "";
                                        newCell4.innerHTML = "";
                                        newCell5.innerHTML = mapAttendeeQuery[record.AttendeeId].strEmail;
                                        newCell6.innerHTML = "";
                                    })
                                }
                            }
                        }
                    },
            {escape: true});
        }   
}
3
  • can you just print console.log(varAttendeeId) and tell us what exactly you are seeing there ? Commented Feb 10, 2016 at 10:44
  • Aside: you really need to learn how to 1- indent your code, 2- use functions (the only difference in your if / else is the name of the element you update). 3- assign functions to variables so you don' t have such a christmas tree. Doing the above will make your code so much more readable. Commented Feb 10, 2016 at 10:47
  • And two more feedback, 1. Use Regex instead of indexOf as its more specific var re = /^(([^<>()[]\\.,;:\s@"]+(\.[^<>()[]\\.,;:\s@"]+)*)|(".+"))@xyz.com$/; re.test(email); 2. Your code has lots of duplicate lines, except the var tableRef all the code in else block is same as in if block Commented Feb 10, 2016 at 10:49

1 Answer 1

0

I don't imagine that this fixes your problem, but re-formatted and with a new function added I think it's clearer to see what it's actually doing. I'm not convinced it makes any sense...

function createNewAttendeeRows( result, tableName, attendees ) {
    var tableRef = document.getElementById( tableName ).getElementsByTagName('tbody')[0];

    result.forEach( function( record ) {
        // Insert a row as the last row
        var newRow   = tableRef.insertRow(tableRef.rows.length);

        // Insert a cell in the row at index 0
        var newCell1  = newRow.insertCell(0);
        var newCell2  = newRow.insertCell(1);
        var newCell3  = newRow.insertCell(2);
        var newCell4  = newRow.insertCell(3);
        var newCell5  = newRow.insertCell(4);
        var newCell6  = newRow.insertCell(5);

        // Append a text node to the cell
        newCell1.innerHTML = attendees[record.AttendeeId].strName;
        newCell2.innerHTML = attendees[record.AttendeeId].CompanyName;
        newCell3.innerHTML = "";
        newCell4.innerHTML = "";
        newCell5.innerHTML = attendees[record.AttendeeId].strEmail;
        newCell6.innerHTML = "";        
    }
}

function addAttendeetoCampaign(){

    varAttendeeId = '';
    jq( ".attendeeChk:checked" ).each( function() { varAttendeeId+=jq(this).val()+","; } );

    if(varAttendeeId != '') {
        var result= Visualforce.remoting.Manager.invokeAction(
                                        ( '{!$RemoteAction.EventPortal_Detail.addToCampaignMember}',
                                            varAttendeeId,
                                            CToken,
                                            UToken,
                                            EToken,
                                            function handleResultresponse(result, event) {

                                                if (event.status){
                                                    for( i=0; i<result.length; i++) {
                                                        var tableToUpdate = 'tblExternal';
                                                            if (varAttendeeId.indexOf('xyz.com') != -1) {
                                                                tableToUpdate = 'tblInternal';
                                                            }
                                                            createNewAttendeeRows( result, tableToUpdate, mapAttendeeQuery )
                                                        }
                                                    }
                                                },
                                            {escape: true}
                                        );
    }   
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .