Find your content:

Search form

You are here

How to replace the text in opportunity title

 
Share

I have formatted the opportunity title as below.

AA - ss001 - aaaaaaaa

So I want to remove AA - from the title for all the record.

Note: I have 20000 record. So, how to do using SOQL query.


Attribution to: SFDC LEARNER

Possible Suggestion/Solution #1

There are several options to do this task.

  • Use Apex Data Loader to update your records. If you are good in Excel this is a good option.
  • Create simple page with a commandButton and in the action of the button apply your logic

(below is a untested sample code, get the idea)

public void btnAction(){
  List<Opportunity> allOpps = [SELECT Id, Title__c FROM Opportunity]; // put appropriate field for your task
  for(Opportunity opp : allOpps){
    // apply whatever your string manipulations here to get what you want as the title
    opp.Title__c = opp.Title__c.substring(5);  // //put appropriate field for your task
  }
  upsert allOpps;
}


NOTE :
If you are going to the second option you have to limit the query result since DML statements can process maximum 10000 records at once.
Also before updating records in production you have to ensure that you know that this is the format of all your records(double check them or good to have backup may be with data loader again)


Attribution to: highfive

Possible Suggestion/Solution #2

SOQL is only to query data, you can not use it to update or change data. To do this you will need to either export your data and use an external tool (excel may work) or you can run apex on your data.

For data operations that need to happen repeatedly you can use scheduled apex, if it only has to be done once you can use anonymous apex. DO BACKUP YOUR DATA BEFORE YOU RUN ANONYMOUS APEX ON PRODUCTION.

If yo have a look at the Apex String methods you'll find that using substring this is easily done, if your opportunity names are always formated in the same way.

string t = 'AA - ss001 - aaaaaaaa';
t = t.substring(5);
system.debug(t); //39997459)|USER_DEBUG|[4]|DEBUG|ss001 - aaaaaaaa

Through the developer console, the eclipse ide or other tools using the salesforce API you can execute the following anonymous apex:

List<opportunity> list = [SELECT Id,name 
                          FROM opportunity 
                          WHERE   put something meaningful here
                          LIMIT 10000];
for(opportunity opt:lst){
      opt.name = opt.name.substring(5);
}
update list;

The update DML statement can only process 10.000 records, so you should limit your query to this amount and repeatedly execute. Using the Where clause you should select those records that have not been modified yet (by filtering on the name, or lastmodified date, .. what makes sense to your scenario)


Attribution to: Samuel De Rycke
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33814

My Block Status

My Block Content