0

I have written one logic in that when we select an object and its related fields on VF page and click on a Add Mapping Button,it will redirect to an other page where selected object and its fields being added. Issue is that when I click for the first time on the Add mapping button,controller method is not calling up(where I have written my logic).On second click it is working fine.Help will be appreciated.Thanks I have put system.debug,so on first click it will enter in for loop,but not inside the (w.ischecked) if condition. my Vf page logic for button:

<div class="slds-col slds-no-flex slds-grid slds-align-top">
                <div class="slds-button-group" role="group">
                    <apex:commandButton value="Add Mappings" styleClass="slds-button slds-button--neutral" action="{!doSave}"/>
                    <!--<apex:commandLink action="{!doSave}" value="add" styleClass="slds-button slds-button--neutral" />-->
                    <apex:commandButton value="Cancel" styleClass="slds-button slds-button--neutral" action="{!doCancel}"/>
                    <!--<button class="slds-button slds-button--neutral">Save Mappings</button>
                    <button class="slds-button slds-button--neutral">Cancel</button> -->
                </div>
            </div>

controller save method: I am pasting the whole code for better understanding:

global with sharing class JNMappings{  public string fieName{get;set;}
    public list<mappingWrap> fieldLst{get;set;}
    public list<mappingWrap> fieldLstToShow{get;set;}
    public string selectedValue{get;set;}
    public string objLabel{get;set;}
    transient map<String, Schema.SObjectType> schemaMap;
    transient map<String, Schema.SObjectField> fieldMap;
    public List<SelectOption> objvalues{get;set;}
    public Mapping__c mapObj{get;set;}
    public string objName{get;set;}
    public boolean fieldFlag{get;set;}
    //Constructor
    public JNMappings(){
        fieldLstToShow = new list<mappingWrap>();
        fieldLst = new list<mappingWrap>();
        JNGenericUtilClass gen = new JNGenericUtilClass();
        dispFlag = true;
        fieldFlag = true;
        FLSCreatecheck = true; 
        userTheme = gen.getContextUserUiThemeDisplayed();
        mapObj = new Mapping__c();
        dupCheck = new list<Mapping__c>();
        dupChMap = new map<string, string>();
        populateObjects();
        selectedValue = objvalues[0].getValue();
        objLabel = objvalues[0].getLabel();
        doFLSCheck();
        dupCheck = [select Unique_Key__c from Mapping__c where Field_API_Name__c != ''];
        for(Mapping__c m:dupCheck){
            dupChMap.put(m.Unique_Key__c,m.Unique_Key__c);
            if(m.Unique_Key__c == 'Accountid')
                accId = m.Id;
            if(m.Unique_Key__c == 'Opportunityid')
                optyId = m.Id;
            //Ankur 11 Nov 2016 - Assign the Lead master record ID which can be used to tag the field mappings related to Lead
            if(m.Unique_Key__c == 'Leadid')
                leadId = m.Id;            
        }
        populateFields(); 
        objName = '';
        fieName = '';
    }
    //Method to fetch all the Objects
    public void populateObjects(){
        objvalues = new List<SelectOption>();        
        Pattern regEx = Pattern.compile('^\\w*?__\\B\\w*__');         
      //for(Schema.PickListentry picklistEntry:statusFieldDescription.getPicklistValues()){         
        for(Schema.SObjectType item : ProcessInstance.TargetObjectId.getDescribe().getReferenceTo()) { 
            if(objvalues.size() == 12)
                break;
            Matcher filter = regEx.matcher(item.getDescribe().getLocalName().toLowerCase());
            if(!item.getDescribe().CustomSetting && (filter.find() == false)) 
            {   
                if(objName != null && objName != ''){
                    if(item.getDescribe().getLabel().toLowerCase().containsIgnoreCase(objName))
                        objvalues.add(new SelectOption(item.getDescribe().getLocalName(), item.getDescribe().getLabel()));                    
                    }
                else{                    
                    objvalues.add(new SelectOption(item.getDescribe().getLocalName(), item.getDescribe().getLabel()));

            }                   
          }        
        }       
        if(objvalues.size() == 0){
            dispFlag = false;
            selectedValue = '';
        }else{            
            dispFlag = true;           
            selectedValue = objvalues[0].getValue();          
        }
        populateFields();
      }
    //Method to fetch all the fields
    public void populateFields(){
        fieldLst = new list<mappingWrap>();
        fieldLstToShow = new list<mappingWrap>();
        system.debug('XXXXXX'+ fieName);
        counter = 0;
        schemaMap = Schema.getGlobalDescribe();
        if(selectedValue != null && selectedValue != ''){
            if(selectedValue == 'Account' || selectedValue == 'Opportunity'){
                mapObj.Related_to_Object__c = selectedValue ;
            }
            else{
                  mapObj.Related_to_Object__c = null;
            }
            Schema.SObjectType ObjectSchema = schemaMap.get(selectedValue);
            objLabel = ObjectSchema.getDescribe().getLabel();
            fieldMap = ObjectSchema.getDescribe().fields.getMap();
            for (String fieldName: fieldMap.keySet()){
                if(fieldLst.size() == 500)
                    break;
               if(fieldMap.get(fieldName).getDescribe().isAccessible() && !dupChMap.containsKey(selectedValue+fieldName)){               
                    if(fieName != null && fieName != ''){
                        if(fieldMap.get(fieldName).getDescribe().getLabel().toLowerCase().containsIgnoreCase(fieName))
                            fieldLst.add(new mappingWrap(fieldName, fieldMap.get(fieldName).getDescribe().getLabel(), string.valueOf(fieldMap.get(fieldName).getDescribe().getType())));
                    }
                    else{
                        fieldLst.add(new mappingWrap(fieldName, fieldMap.get(fieldName).getDescribe().getLabel(), string.valueOf(fieldMap.get(fieldName).getDescribe().getType())));
                    }
                }
            }
            totalSize = fieldLst.size();
            fieldLst.sort();
            if((counter+limitSize) <= totalSize){
                for(Integer i=0;i<limitSize;i++){
                    fieldLstToShow.add(fieldLst.get(i));
                }
            }else{
                for(Integer i=0;i<totalSize;i++){
                    fieldLstToShow.add(fieldLst.get(i));
                }
            }
        }
        if(fieldLstToShow.size()== 0){
            fieldFlag = false;
        }else{
            fieldFlag = true;
        }

    }
    //Method to fetch related objects for mapObj.Related_to_Object__c
    public void populateRelatedObjects(){
        string seletcedObj = mapObj.Related_to_Object__c;
        if(seletcedObj == 'Master'){
            system.debug('@@' +seletcedObj);
            populateObjects();
        }
        else{
            objvalues = new List<SelectOption>();
            objvalues.add(new SelectOption(seletcedObj, seletcedObj));
            schemaMap = Schema.getGlobalDescribe();
            List<Schema.ChildRelationship> sc = schemaMap.get(seletcedObj).getDescribe().getChildRelationships();
            Pattern regEx = Pattern.compile('^\\w*?__\\B\\w*__'); 
            for(Schema.ChildRelationship item : sc) { 
                //if(objvalues.size() == 12)
                //    break;
                Matcher filter = regEx.matcher(item.getChildSObject().getDescribe().getLocalName().toLowerCase());
                if(!item.getChildSObject().getDescribe().CustomSetting && (filter.find() == false)) 
                {   
                    if(seletcedObj != null && seletcedObj != ''){
                        //if(!item.getChildSObject().getDescribe().getLabel().toLowerCase().containsIgnoreCase(seletcedObj))
                            objvalues.add(new SelectOption(item.getChildSObject().getDescribe().getLocalName(), item.getChildSObject().getDescribe().getLabel()));
                    }
                    else{
                        objvalues.add(new SelectOption(item.getChildSObject().getDescribe().getLocalName(), item.getChildSObject().getDescribe().getLabel()));
                    } 
                } 
            }
            if(objvalues.size() == 0){
                dispFlag = false;
                selectedValue = '';
            }else{
                dispFlag = true;
                selectedValue = seletcedObj;
            }
            populateFields();
            mapObj.Related_to_Object__c = selectedValue;
        }
    }

    //Method to save the selected records
    public Pagereference doSave(){
      if(!dupChMap.containsKey('Leadid')){
          Mapping__c mapObj = new Mapping__c();
          mapObj.Object_API_Name__c = 'Lead';
          mapObj.Object_Label__c = 'Lead';
          mapObj.Field_API_Name__c = 'id';
          mapObj.Field_Label__c = 'Lead ID';
          mapObj.Field_Type__c = 'ID';
          mapObj.Related_to_Object__c = 'Lead';
          mapObj.Enabled__c = true;
          mapObj.Unique_Key__c = 'Leadid';
          if(Schema.Sobjecttype.Mapping__c.isCreateable()){
              doFLSCheck();
              if(FLSCreatecheck)
                  Database.insert(mapObj);
          }
          leadId = mapObj.Id;
        }
        Mapping__c mappingobj = new Mapping__c();
        list<Mapping__c> maplst = new list<Mapping__c>();
        string errormsg = '';
        schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType ObjectSchema = schemaMap.get(selectedValue);
        system.debug('sel val here' +ObjectSchema);
        for(mappingWrap w:fieldLst){            
            mappingobj = new Mapping__c();  
            system.debug('come inside for' +mappingobj);            
            if(w.isChecked){  
                system.debug('inside if' +w.isChecked);
                mappingobj.Object_Label__c = string.valueOf(ObjectSchema.getDescribe().getLabel());
                mappingobj.Object_API_Name__c = selectedValue;
                mappingobj.Field_API_Name__c = w.fName;
                mappingobj.Field_Label__c = w.fLabel;
                mappingobj.Field_Type__c = w.fType;
                mappingobj.Unique_Key__c = mappingobj.Object_API_Name__c + mappingobj.Field_API_Name__c;
                mappingobj.Enabled__c = true;
                mappingobj.Pick_values_from_SFDC__c = w.SFDC;
                if(mapObj.Related_to_Object__c == null){
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING,'Please select the related to object!'));
                    return null;
                }
                mappingobj.Related_to_Object__c = mapObj.Related_to_Object__c;
                if(mappingobj.Related_to_Object__c == 'Account'){
                    mappingobj.Mapping__c = accId;
                }
                else if(mappingobj.Related_to_Object__c == 'Opportunity' || mappingobj.Related_to_Object__c == 'Both'){
                    mappingobj.Mapping__c = optyId; 
                }
                //Ankur 11 Nov 2016 - Populate the Parent Object(Mapping__c) field with master Lead record ID
                else if(mappingobj.Related_to_Object__c == 'Lead')
                    mappingobj.Mapping__c = leadId;
                    maplst.add(mappingobj);

            }
        }
        if(maplst.size()==0){
            system.debug('IAMHERE!!');          
          //  ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a field!'));
            return null;
        }
        if(maplst != null && maplst.size()>0){
            doFLSCheck();
            if(Schema.Sobjecttype.Mapping__c.createable && FLSCreatecheck){
                insert maplst;
            }else{

            }
        }
        Pagereference pg;
        if(userTheme == 'Theme4d')
            pg = new Pagereference('/apex/JNMappingList_LEX');
        else
            pg = new Pagereference('/apex/JNMappingList');
        pg.setRedirect(true);
        return pg;
    }

wrapper:

global class mappingWrap implements Comparable {
        global string fName{get;set;}
        global string fLabel{get;set;}
        global string fType{get;set;}
        global boolean isChecked{get;set;}
        global boolean SFDC{get;set;}
        global mappingWrap(string fN, string fL, string fT){
            fName = fN;
            fLabel = fL;
            isChecked = false;
            fType = fT;
            SFDC = true;
        }
         global Integer compareTo(Object ObjToCompare) {
            return fLabel.CompareTo(((mappingWrap)ObjToCompare).fLabel);
        }
    }
  • Code is incomplete to understand what your passing inside the "for loop" for fieldLst. – NITHESH K Aug 10 '17 at 9:19
  • I have only written one method for which that button is not working. FieldLst is a list of wrapper : public list<mappingWrap> fieldLst{get;set;} fieldLst=new list<mappingWrap>(); – Gaurav singh Aug 11 '17 at 11:14
  • Where your passing value for fieldLst ? – NITHESH K Aug 11 '17 at 11:31
  • That is in separate method. I am pasting the whole code for better understanding: – Gaurav singh Aug 14 '17 at 10:36
0

I got the solution,i was using html tag i.e. html tag="options" .I have removed this tag.We should not prefer to use html tags as salesforce has its own designed tags and attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.