With the introduction of the new external Id field names (View External Ids), Grid will automatically populate the Id field as new records are created. However, this will not work on existing data in your Org. For this existing data, we have provided a script to run in Developer Console depending on the available data in your Org. You can determine your need to run this script by the following cases: 

  • No Data in Sandbox or Production - If you have no data in either your Sandbox or Production org, you do not have to run the script. The Id will be generated when a record is created. 
  • Old data on Sandbox, not on Production - If you have old data in your Sandbox environment, you will need to run the script in the Sandbox org. After running the script, the External Id field value will be populated. If you wish to have this same view in your Production Org as well, you can export the data from your Sandbox and import it into Production. 
  • Old data on Production, but not on Sandbox - If you have old data in your Production environment, you will run the script in your Production Org. The External Id field value will be populated there once the run is complete. 
  • Old Data on both Sandbox and Production - If you have old data in both Production and Sandbox environments, you will run the script in your Production Org first. After running the script, your existing Production data will have the External Id field values populated. If you run the script in your Sandbox environment, you will end up with different External Id values there. To sync the Production and Sandbox data, you will need to run the script in Production, export your data from Production, Delete your Sandbox data and import your Production data into the Sandbox. 

Running the Script

To run the provided script, follow these steps: 

  1. Login as System Administrator. 
  2. Go to Developer Console > Debug, select 'Open Execute anonymous window'.
  3. Paste in the script below and click 'Execute'.
  4. Once the script has executed, check your debug logs to verify the status. 
Integer RANDOM_NUMBER_LENGTH = 10;

List<CRMC_PP__GridView__c> gridViews = [SELECT ID, Name, createdDate, CRMC_PP__View_External_Id__c FROM CRMC_PP__GridView__c];

List<CRMC_PP__GridView__c> viewsToBeUpdated = new List<CRMC_PP__GridView__c>();

for(CRMC_PP__GridView__c view : gridViews){

if(String.isBlank(view.CRMC_PP__View_External_Id__c)){

String randomNumberString = string.valueof(Math.abs(Crypto.getRandomLong()));

randomNumberString = randomNumberString.substring(0, RANDOM_NUMBER_LENGTH);

view.CRMC_PP__View_External_Id__c = view.Name + randomNumberString + String.join(String.valueOfGmt(view.createdDate).split('-'),'');

viewsToBeUpdated.add(view);

}

}

Database.SaveResult[] srList = new List<Database.SaveResult>();

if(!viewsToBeUpdated.isEmpty()){

System.debug('Total Number of view records' + viewsToBeUpdated.size());

srList = Database.update(viewsToBeUpdated, true);

}

for (Database.SaveResult sr : srList) {

if (sr.isSuccess()) {

System.debug('Successfully upserted View');

}else {

for(Database.Error objErr : sr.getErrors()) {

System.debug('The following error has occurred.');

System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage());

}

}

}
JS