+ Start a Discussion
Richa_LearningRicha_Learning 

Retrieve cusotm setting list field value

 Hi,

I am using custom settings (Cloneaccounts__c ) to inlcude field API name  so they shoud not be cloned. there are few fields where api name is more than 40 characters and i am not able to save them. I created a field "Fieldname" on custom settings (Cloneaccounts__c ) so i can save fields api name in it. But how do i modify this controller to read the value of CloneAccounts__c field "Fieldname"
public with sharing class CloneaccController {

    public account objaccount {get;set;}        
    public string accountID;                        
   
    private account objNewaccount;
    private string queryString = '';
    public string strPrevCurrency {get;set;}
    Map<String, Schema.SObjectField> mapaccountFields;
    set<String> setexfields = new set<String>();

    public CloneaccController(ApexPages.StandardController controller) 
    {
                      
        accountID = ApexPages.currentPage().getParameters().get('id');
        
        
        if(accountID != null)
        { 
                mapaccountFields = Schema.SObjectType.account.fields.getMap() ;
                 List<Cloneaccounts__c > lstexfields = Cloneaccounts__c.getall().values();
                 
            for( Cloneaccounts__c exfield: lstexfields){
                setexfields.add(exfield.Name.toLoweraccount());
            }
          
            for(String s: mapaccountFields.keyset()){
                if(!setexfields.contains(s)){
                    if(queryString == ''){
                        queryString += s;
                    }else{
                        queryString += ',' + s;
                    }
                }
            }
                
                
       objnewaccount = Database.Query('Select ' + queryString + ' From account where id= \'' + String.escapeSingleQuotes(accountID) + '\'');   
                  
       objaccount = objNewaccount.clone(false,true,false,false);
             
        } 
 
         }
        
         public PageReference save()
    {
         insert objaccount;
        return new PageReference('/'+objaccount.id);
        
    }  
           
    }

Please help
Best Answer chosen by Richa_Learning
R Z KhanR Z Khan
mapaccountFields = Schema.SObjectType.account.fields.getMap() ;
                 List<Cloneaccounts__c > lstexfields = Cloneaccounts__c.getall().values();
                 
            for( Cloneaccounts__c exfield: lstexfields){
if(exfield.fieldname__c!= null){
                setexfields.add(exfield.fieldname__c.toLowerCase());
}
            }

Try this

All Answers

BM 5BM 5
Hi, 

Can you try the below code in your controller.
  private static Cloneaccounts__c sfParams = Cloneaccounts__c .getOrgDefaults(); 

Thanks,
BM
Richa_LearningRicha_Learning
i am not that expert in apex. could you lease where i need to put this? how do i update this so it should read field value
 
mapaccountFields = Schema.SObjectType.account.fields.getMap() ;
                 List<Cloneaccounts__c > lstexfields = Cloneaccounts__c.getall().values();
                 
            for( Cloneaccounts__c exfield: lstexfields){
                setexfields.add(exfield.Name.toLoweraccount());
            }

 
R Z KhanR Z Khan
is this a hierarchy or a lsit custom setting?
TO fit field names that are mroe than 40 characters long you can modify your Fieldname field type to text area
Richa_LearningRicha_Learning
This is a list.

Initially i created a list with no field and enter the value directly using manage--> new .but there are some api names that i was not able to fit.

Now i created a field  "fieldname__C" text area and i am entering the api names in it.
 
Issues is my controller is not reading the value of fieldname__c
Richa_LearningRicha_Learning
Example: how i update the controller so it shoud  the value of test 

User-added image
R Z KhanR Z Khan
String fields = '';

List<Cloneaccounts__c > lstexfields = Cloneaccounts__c.getall().values();

for(Cloneaccounts__c acc : lstexfields){
fields += acc.FieldName__c + ',';
}

then add that fields string to your query
Richa_LearningRicha_Learning
this is how i updated it....but when i click on the the button i get this error:
Attempt to de-reference a null object 
An unexpected error has occurred. Your development organization has been notified.
 
mapaccountFields = Schema.SObjectType.account.fields.getMap() ;
                 List<Cloneaccounts__c > lstexfields = Cloneaccounts__c.getall().values();
                 
            for( Cloneaccounts__c exfield: lstexfields){
                setexfields.add(exfield.fieldname__c.toLoweraccount());
            }

 
R Z KhanR Z Khan
what does toLoweraccount() do?
issue is because you don't have a FieldName__c filled. so when you call exfield.FieldName__c its null and then you call toLoweraccount on a null object that throws an exception. Are you trying to do toLowerCase()
Richa_LearningRicha_Learning
Yes to lowercase. I added a field in fieldname__c. It should read customer__C as value

User-added image
R Z KhanR Z Khan
mapaccountFields = Schema.SObjectType.account.fields.getMap() ;
                 List<Cloneaccounts__c > lstexfields = Cloneaccounts__c.getall().values();
                 
            for( Cloneaccounts__c exfield: lstexfields){
if(exfield.fieldname__c!= null){
                setexfields.add(exfield.fieldname__c.toLowerCase());
}
            }

Try this
This was selected as the best answer
Richa_LearningRicha_Learning
It worked thanks. But why it was reading it blank  when i have field populated?
R Z KhanR Z Khan
You were calling toLoweraccount() which is not a method