0

I am using a remote action for getting a result of list of information from a sObject. On my page load I am getting populating a map with field api name and label. But after the page load I click for remote action. The value of the map is getting reset. Now i changed the code and calling the method which populate the map from remote action. But still the the map value is null. Can anyone help me hoe to fix it.

public class ConfigurationCtrl {
   public static Map<String, String> fieldNameLabel{get; set;}
public static Map<String, String> getfieldNameLabel(){

    Schema.SObjectType objects = Configuration_Item__c.SObjectType;
    Map<String, String> fieldNameLabel1 = new Map<String, String>();
    for(Schema.SObjectField fld:  objects.getDescribe().fields.getMap().values()){
        System.debug('Ran describe ob '+fld.getDescribe().getlabel());
        fieldNameLabel1.put(fld.getDescribe().getname(), fld.getDescribe().getlabel());
    }
    System.debug('fieldNameLabel: ' + fieldNameLabel1); 
    return fieldNameLabel1;
  }
  @RemoteAction
public static List<Configuration_Item__c> fetchFieldsetValues(string idList){
    fieldNameLabel = new Map<String, String>();
    getfieldNameLabel();
    system.debug('fetchFieldsetValues idList '+idList);
    List<String> ids = new List<String>();
    ids = idList.split(';');
    String recordtypeName = '';
    integer i=0;
    for(String idResult : ids ){
        if(idResult.contains('RecTypeName')){
            recordtypeName = idResult.remove(':RecTypeName-'); 
            break;
        }
        i++;
   } 
   ids.remove(i); 
   System.debug('Record type Name : ' + RecordtypeName );    
   List<Configuration_Item__c> configurationItemSOQLResult = ConfigurationItemsService.getConfigurationItemList(ids, recordtypeName );        

   System.debug('configuration Item SOQL Result '+ configurationItemSOQLResult);
   //return JSON.serialize(configurationItemSOQLResult);
   //Map<String, String> fieldNameLabel2 = new Map<String, String>();
   //fieldNameLabel2 = getfieldNameLabel();
   for(Configuration_Item__c ci: configurationItemSOQLResult)
   System.debug('configuration Item  '+ ci);
   System.debug('field Name Label information : ' + fieldNameLabel );
   return configurationItemSOQLResult;
  }
}

VF page :

Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.ConfigurationCtrl.fetchFieldsetValues}',
      idList,function(result, event) {            
          console.log(result+' :result ');
          //$('.selectionPanel_sec2').html(result);
          //var obj = JSON.parse(result);
          //var string = result.toString();
          //var str = '{"val1": 1, "val2": 2, "val3": 3}';
            //var obj = jQuery.parseJSON(string);
            //alert(obj.val1);
         // console.log(obj);
          var resultarray = []; 
          var i = 0;
         var string = '<table>';
           $.each( result, function( key, value ) {
           if (value.Name != 'PO2')
           string +='<tr><td><input type="radio" name="configurationRadio" ></td>';

                  $.each( value , function( key1, value1 ) {
                   console.log( key1+'key1' + typeof key1);
                   key1=key1.replace("__c", " ");
                   key1=key1.replace("_", " ");
                   console.log( key1 + ": " + value1 );
                  // if (typeof value1 != 'undefined') 
                  if (key1 != 'Id' && key1 != 'RecordTypeId' && value1 != 'PO2' ) string +='<td>'+key1 +' :'+value1+'</td>';

                   //else 
                   //string +='<td>undefnd</td>';
                   //resultarray[i][key1] = value1;
                   //string +='<td>'+value1+'</td>';
               });
               string +='</tr>';
               i++;
           });
           string +='</table>'
           console.log('resultarray'+string); 
           $('.selectionPanel_sec2').html(string);      
          //obj =  JSON.parse(text);    
          //console.log(obj +' :obj info');    
     });
  }

Can anyone help me what is the reason for this issue. How to fix it? Is their other way by which we can hold the value of the map with out getting reset.

2 Answers 2

0

You are not using return value from this method getfieldNameLabel(); You need to use it like below code if you want to assing return value to a variable. (In this case Map)

fieldNameLabel = getfieldNameLabel();
2
  • That's fine, I was testing something so made some change. But do you have any idea why the value is getting reset in after calling remote action method. As in my case the map have value onload, but when I call a remote action method the value of map get reset.
    – Anu
    Commented Aug 31, 2015 at 7:02
  • Do you have fieldNameLabel = new Map<String, String>(); in your code? Or is it for testing purposes also?
    – tugce
    Commented Aug 31, 2015 at 7:16
0

Unlike traditional VF ViewState model, Remote actions do not hold any sort of context. So every time you call a remote action method it acts as a complete brand new request. In ViewState the values of the controller are serialised to client page and deserialized once an action is performed. But in RemoteAction model there is no state stored anywhere. Every request will be absolutely independent of the previous one. It's purely stateless , unless you store the state in database.

You must log in to answer this question.

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