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
arc_08arc_08 

Update custom setting values, when sandbox get refreshed from prod?

Hi Everyone,

Currently we are manually changing/updating custom field values every time we did refresh from production. But we have different values in Production and sandbox, when we refreshed sandbox values are getting Prod values and we are doing manual changes to them. 
To avoid this manual change is there any other way that can be auto update to old values whenever refresh is done??

Please provide a sample apex code and how to retrive the old values of custom setting
sfdcBahusfdcBahu
Do you mean Data loader as the manual route?

Also when you meant Custom Setting, if its actual 'custom settings' you can update through Apex easily You may create a Class with Sample method like below and call the Class in Post Refresh settings while you initiate the Refresh.

Sample class:

Public class Postrefreshsample {

Public void myinitmethod()
{
List<CSetting__c> Settings = [select id,Config__c,Value__c from Csetting__c];

  for(CSetting__c CSet: Settings)
  {    
    if(CSet.Config__c == 'SettingA')
         Cset.Value__c = 'myvalueASandbox';
    
    if(CSet.Config__c == 'SettingB')
         Cset.Value__c = 'myvalueBSandbox';
   }
 Update Settings ;
 }
}

by any chance if you meant Custom Metadata type, then you can call them from Apex as well but little more work. below reference might be useful.
https://www.sfdcpanther.com/create-update-custom-metadata-using-apex/

PS:
I deal it little differently though. For every Custom metadata or Custom Settings, we store 2 rows. one for Prod and one For Sandbox.

And in every reference to the Custom Setting in Apex or Formula, we use the Custom Setting record Conditionally.

Eg:

String labeltoquery;

if(UserInfo.getOrganizationId == 'a0L2M00000NZsample') // this would be Prod org id which never changes.
   labeltoquery = 'SettingA_Prod';    

else 
 labeltoquery = 'SettingA_Sandbox';

then while using querying the Custom setting,

CSetting__c Settings = [select id,Config__c,Value__c from Csetting__c where Config__c =: labeltoquery ];

This way you dont have to update the Custom Setting after every refresh.