0

Getting the error when I iterate my data variable in for:each.Below is the snip of the values that is fetched in the data variable.Please help!

enter image description here

HTML

<table class="slds-table slds-table_cell-buffer slds-table_bordered">
                        <template for:each={data} for:item="con">
                            <tr key={con.key}>
                                <th style="height: 25px;">
                                    <div>{con}</div>
                                </th>
                            </tr>
                        </template>
                    </table>

JS

@track data;
getMergeLeads({Indexing:selectedIdsArray})
            .then(result => {
                var selectedIdRecords = result;
                this.data = selectedIdRecords;
                console.log('Get Merge Leads : '+JSON.stringify(this.data));
                
            })
            .catch(error => {
                this.error = error;
                console.log('Get Merge Leads error ===> '+error.body.message);
            });

Controller

@AuraEnabled
    public static ListMapWrapper getMergeLeadRecord ( list<string> Indexing ) {
        wrapmap = new Map<string, List<dataWrapperClass>>();
        system.debug('Indexing : '+Indexing); 
        list<string> tempstringID = new list<string>();
        for(integer i = 0 ;i < Indexing.size(); i++){
            tempstringID.add(Indexing[i]);
        }
        system.debug('tempstringID : '+tempstringID); 
        if(tempstringID.size()<=0){
            return null;
        }
        
        String objName ='Account';
        
        if (Schema.sObjectType.Account.isAccessible()){
            
            Map<String, Schema.SObjectField> objectFields = new Map<String, Schema.SObjectField>();
            
            Schema.SObjectType convertType = Schema.getGlobalDescribe().get(objName);
            Schema.DescribeSObjectResult r = convertType.getDescribe();    
            objectFields = Schema.getGlobalDescribe().get(objName).getDescribe().fields.getMap();
            
            List<String> fieldlist = new List<String>();
            Set<String> aPINamesSet = objectFields.keyset();
            fieldlist.addAll(aPINamesSet);
            system.debug('fieldlist : '+fieldlist); 
            string soqlQueryCurrent = string.join(fieldlist,',');
            
            if(soqlQueryCurrent.endsWithIgnoreCase(',')){
                soqlQueryCurrent = soqlQueryCurrent.removeEndIgnoreCase(',');                  
            }
            String queryStr ='SELECT '+soqlQueryCurrent+' FROM '+objName+' where Id =:tempstringID';  
            system.debug('queryStr : '+queryStr);            
            List <Sobject> finalLeadListRecord = Database.query(String.escapeSingleQuotes(queryStr));
            system.debug('finalLeadListRecord : '+finalLeadListRecord);       
            Set<SObjectField> accessibleFields = new Set<SObjectField>();
            
            SObjectType sObjectType = Schema.getGlobalDescribe().get('Account');
            for (SObjectField field : sObjectType.getDescribe().fields.getMap().values()){
                if (field.getDescribe().isAccessible() && field.getDescribe().isUpdateable() ){
                    system.debug('Object accessible '); 
                    accessibleFields.add(field);
                }
            }
            
            for(Sobject ld : finalLeadListRecord){
                String jsonInput = System.Json.serialize(ld);
                system.debug('jsonInput '+jsonInput); 
                Map<String, Object> soqlresMap = (Map<String, Object>)JSON.deserializeUntyped(jsonInput);
                
                List<dataWrapperClass> tempStrings = new List<dataWrapperClass>();
                String ssmap = 'Id';
                if(ssmap == 'Id' || ssmap == 'id'){
                    String tempVal = null;
                    String tempValID = null;
                    ssmap = 'Id (Master Record)';
                    tempVal = String.valueOf(soqlresMap.get('Name'));
                    tempValID  = String.valueOf(soqlresMap.get('Id'));
                    if(wrapmap.containskey(ssmap)){
                        tempStrings = wrapmap.get(ssmap);                        
                        dataWrapperClass dcpw = new dataWrapperClass();
                        dcpw.FieldValue = tempVal;
                        dcpw.FieldAPIName = ssmap;
                        dcpw.FieldAPINameOrValue = tempValID;
                        tempStrings.add(dcpw);
                        
                    }
                    else{
                        dataWrapperClass dcpw = new dataWrapperClass();
                        if(ssmap == 'Id (Master Record)'){
                            dcpw.FieldAPIName = tempVal;
                            dcpw.FieldValue = tempVal;
                            dcpw.RadioButton = true;
                            dcpw.FieldAPINameOrValue = tempValID;
                        }
                        tempStrings.add(dcpw);
                        wrapmap.put(ssmap,tempStrings); 
                    }
                }
                
                for(SObjectField accField : accessibleFields){
                    String accFields = String.valueOf(accField);
                    
                    if(accFields != 'Attributes' && accFields != 'PhotoURL' && accFields != 'Lastreferencedate' && accFields != 'IsDeleted' && accFields != 'Name' && accFields != 'Address'
                       && accFields != 'OwnerId' && accFields != 'CreatedDate' && accFields != 'CreatedById' && accFields != 'Lastmodifieddate' && accFields != 'LastmodifiedbyId' && accFields != 'SystemModstamp'
                       && accFields != 'Lastvieweddate' && accFields != 'LastReferencedDate' && accFields != 'lastTransferDate'  && accFields != 'IsConverted'){
                           List<dataWrapperClass> tempString = new List<dataWrapperClass>();
                           system.debug('Inside restricted field condition');
                           String tempVal = null;
                           String tempValID = null;
                           
                           if(accFields == 'Id'){
                               accFields = 'Id (Master Record)';
                               tempVal = String.valueOf(soqlresMap.get('Id'));
                               tempValID  = String.valueOf(soqlresMap.get('Id'));
                           }
                           else{
                               tempVal = String.valueOf(soqlresMap.get(accFields));
                               tempValID  = String.valueOf(soqlresMap.get('Id'));
                           }
                           
                           if(wrapmap.containskey(accFields)){
                               tempString = wrapmap.get(accFields);                        
                               dataWrapperClass dcpw = new dataWrapperClass();
                               dcpw.FieldValue = tempVal;
                               dcpw.FieldAPIName = accFields;
                               dcpw.FieldAPINameOrValue = tempValID;
                               tempString.add(dcpw);
                               
                           }
                           else{
                               dataWrapperClass dcpw = new dataWrapperClass();
                               if(accFields == 'Id (Master Record)'){
                                   dcpw.FieldAPIName = tempVal;
                                   dcpw.FieldValue = tempVal;
                                   dcpw.RadioButton = true;
                                   dcpw.FieldAPINameOrValue = tempValID;
                               }
                               else{
                                   dcpw.FieldAPIName = accFields;
                                   dcpw.FieldValue = tempVal;
                                   dcpw.RadioButton = false;
                                   dcpw.FieldAPINameOrValue = tempValID;
                               }
                               tempString.add(dcpw);
                               wrapmap.put(accFields,tempString); 
                           }
                       }
                }
            }
            
            try{
                if(wrapmap.size()>0){
                    wrapmap = updateMapFieldsValue(wrapmap);
                }
            }
            catch(Exception e){
                system.debug(' : '+e.getCause());
                system.debug(' : '+e.getLineNumber());
                system.debug(' : '+e.getMessage());
                system.debug(' : '+e.getTypeName());
            }            
                        
            List<ParentWrapper> results = new List<ParentWrapper>();
            for(String key : wrapmap.keySet()){
                ParentWrapper pw = new ParentWrapper(key, wrapmap.get(key));
                results.add(pw);
            }
            
            ListMapWrapper lmw = new ListMapWrapper();
            lmw.tableTemp = results;
            lmw.mapTemp = wrapmap; 
            system.debug('lmw : '+lmw);       
            return lmw;
        } 
        else{
            ListMapWrapper lmw = new ListMapWrapper();
            return lmw;
        }
    }
5
  • The this.data does not seems to be an array. The iteration can happen on array types only.
    – manjit5190
    Commented Nov 21, 2020 at 8:55
  • @manjit5190 I changed it to this.data = []; still getting the same error. Commented Nov 21, 2020 at 9:13
  • Please add the signature of the imperative apex method yu are calling (edit the question to add it).
    – Phil W
    Commented Nov 21, 2020 at 9:27
  • @PhilW I have added the controller method that I'm calling. Commented Nov 21, 2020 at 9:34
  • Enable debug mode and then check this might be you get better idea what's wrong here. Commented Nov 21, 2020 at 12:03

1 Answer 1

5

The issue is you are trying to return an Object and you are trying to iterate over it.

I was able to replicate this via this code:

connectedCallback() {
    const testData = [];
    const testMe = {
        value : '123Value'
    }
    const testMe2 = {
        value : '123Value2'
    }
    //testData.push(testMe);
    //testData.push(testMe2);
    this.data = testMe2;
}

If you try to iterate over object rather than a list it would give this error.

Try to change you Apex code to return a list instead of a wrapper, orelse on JS side try to drill down to the list and then assign the list to this.data orelse an empty list to this.data.

Here is a simple way by which you can debug these issues from chrome developer console in future.

When you get an exception like this, always remember there will always a stack trace attached to it, from that you can easily identify the triggering point.

My triggering point was t.data(minified), which means the data which is being feed to the attribute is not in expected format. You can keep a breakpoint and check this.

enter image description here

4
  • Once you have the JS access the wrapper's properties, also make sure that those properties are aura enabled.
    – Phil W
    Commented Nov 21, 2020 at 9:56
  • 1
    @PhilW Yes they are aura enabled. Commented Nov 22, 2020 at 7:36
  • In that case the above should work fine, if you assign list this.data instead of Object. Commented Nov 22, 2020 at 8:02
  • @NagendraSingh Can you please suggest how to do that? I'm really confused. Commented Nov 23, 2020 at 7:07

You must log in to answer this question.

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