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
Salesforce Test 5Salesforce Test 5 

Update existing picklist value

Hi Friends

I have one issue facing a big challenge.i have two object Account and Object__c which have lookup relationship.i have one picklist in account object with value ABC and XYZ.my requirment is want two convert all existing picklist value XYZ to ABC in account object which have Object__c record .


Thanks
Pankaj_GanwaniPankaj_Ganwani
Hi,

You can achieve this using batch apex(one time script).

Thanks,
Pankaj
Salesforce Test 5Salesforce Test 5
Hi Pankaj Have you sample coding...
Pankaj_GanwaniPankaj_Ganwani
Hi,

Use below mentioned code:
 
global class SearchAndReplace implements Database.Batchable<sObject>{

   global SearchAndReplace(){

   
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('select Id, (select Id from Object__r) from Account where picklist__c = \'XYZ\');
   }

   global void execute(Database.BatchableContext BC, List<Account> lstAccount){
     
    List<Account> lstUpdatedAcc = new List<Account>();
    for(Account objAccount :   lstAccount)
    {
          if(!objAccount.Object__r.isEmpty())
              lstUpdatedAcc.add(new Account(Id = objAccount.Id, picklist__c = 'ABC'));
     }
    update lstUpdatedAcc;
}
   global void finish(Database.BatchableContext BC){
   }
}

 
Salesforce Test 5Salesforce Test 5
Hi Pankaj I got error this Error: Compile Error: line breaks not allowed in string literals at line 9 column -1
Salesforce Test 5Salesforce Test 5
I have  modified your code but getting error

Error: Compile Error: Invalid field purchase_order__r for SObject Account at line 17 column 15
global class SearchAndReplace implements Database.Batchable<sObject>{

   global SearchAndReplace(){

   
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('select Id, (select Id from  Purchase_Order__r) from Account where Type =\'Customer');
   }

   global void execute(Database.BatchableContext BC, List<Account> lstAccount){
     
    List<Account> lstUpdatedAcc = new List<Account>();
    for(Account objAccount :   lstAccount)
    {
          if(!objAccount.Purchase_Order__r.isEmpty())
              lstUpdatedAcc.add(new Account(Id = objAccount.Id, Type = 'Prospect'));
     }
    update lstUpdatedAcc;
}
   global void finish(Database.BatchableContext BC){
   }
}

 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Please use the correct relationship name in inner query. Try using objAccount.Purchase_Orders__r.isEmpty() instead.