function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Santanu Roy 18Santanu Roy 18 

All the custom settings update in entire org

Hi please help me some poc's or code snippet if possible. I need to 1st get all the custom setting from the entire org then need to search email field type to all those custom settings ..Find those email field and then update with the new value. Some of the custom settings are having 3 email type fields as toEmail, ccEmail and bccEmail my code should find all those 3 fields across all custom settings and then update with the new email id .Please provide some code snippet if possible Mag.. Thanks for the help...
Sangeet kaseraSangeet kasera
Hi Santanu,

There is no decent way to do it, but can be achieved using a describe call.

Describe call result has a method that indicates whether it is an SObjectType or a normal SObject or a Custom Setting. We can take advantage of that to query all custom settings values.

You can go through below link where you can find a relevant way to find all custom setting data-
https://salesforce.stackexchange.com/questions/212940/fetch-all-custom-settings

Please choose this as a best answer by which it help others.

Regards,
Sangeet
AnudeepAnudeep (Salesforce Developers) 
Hi Santanu, 

You need to leverage Data Type object here because it gives you the data type of a field 

I ran the following query in my org and it showed me the Email field type along with the API name of the field in my custom setting 
SELECT DataType, QualifiedApiName FROM EntityParticle WHERE EntityDefinition.QualifiedApiName = 'agopagoni123__Dreamhouse_Settings__c'

User-added image

By adding necessary filters to this query, you can retrieve all the custom settings in your org that have field type as email. You can update your records accordingly based on the results

If you find this information helpful. Please mark this answer Best. It may help others in the community. Thank You

Anudeep
Santanu Roy 18Santanu Roy 18
@Sangeet Hi Sangeet , Yes this one I already searched to some extent it is helpful but I need to update also those field with the new value how would I do it .
I need to 1st get all the custom settings from the org then iterating those i need to find the field which has field type as email or may be some field type which are text but stores email value .. I need to get all the email values from all the custom settings and then update it .....Please let me know how the update will happen......

--Santanu
Santanu Roy 18Santanu Roy 18
@Anudeep, No that is not the thing I want .. I know that how to extract the field name by using query .. Please find above I updated my requirement. It is something need to be handled using dynamic as Sangeet mentioned Describe and SObject type..
Sangeet kaseraSangeet kasera
Hi @Santanu,

Yes, For Email field directly you need to perform DML and for Text you need to find that which fields are containing email value.
Atleast you should know that which fields you want to update then perform DML out of iteration after adding value.

Or I am not sure about that if is there any way may if you will apply check as contains @ OR .com before adding the value then this will be helpful.

. Please mark above answer as Best If you find that this is helpful.

Regards,
Sangeet
Santanu Roy 18Santanu Roy 18
Yes Sangeet that's the reason I need to first of all find the field values then I need to compare this with regex if this value as email or not ..But I am unable to get the field values ... This is my code if you run this code in your org it will search all the custom setting and then find the nam also but I am not able to find the values of those field name ....  for(String s : objectFields.keySet()) { this s in the below code get all the named of all the custom settings in your org. but how to update it I don't know can you help me to complete it please ....

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
        for(String objectName :gd.keySet()){
            Schema.SObjectType result = gd.get(objectName);
            if(result.getDescribe().isCustomSetting()){
                //List<AGN_MIR_Setting__c> mirList = AGN_MIR_Setting__c.getall().values();
                Map<String, Schema.SObjectField> objectFields =result.getDescribe().fields.getMap();
                System.debug('Custom Setting field map -- '+ objectFields);
               // Map<String, Object> fieldsToValue = result.getDescribe().getPopulatedFieldsAsMap();
                for(String s : objectFields.keySet()) {
                 
                    SObjectType r = ((SObject)(Type.forName('Schema.'+objectName).newInstance())).getSObjectType();
                    DescribeSObjectResult d = r.getDescribe();
                    SObject objectRef = (SObject)(Type.forName('Schema.'+objectName).newInstance());
                    Map<String, Object> fieldsToValue = objectRef.getPopulatedFieldsAsMap();
                   //getPopulatedFieldsAsMap 
                    //System.debug('Fields to map '+objectRef);
                    if(s.contains('mail') && String.valueOf(result.getDescribe().fields.getMap().get(s).getDescribe().getType()).equals('EMAIL')){
                        System.debug('Object --'+ result + ', FieldName --'+s +' Type '+result.getDescribe().fields
                                    .getMap()
                                    .get(s)
                                    .getDescribe().getType()
                                    );
                        
                        //build update query 
                        
                    }
                    
                }  
                //System.debug('Custom Setting values -- '+ query);
                //count++;
            }
        }