+ Start a Discussion
sumit dsumit d 

Batch to change Opportunity name in this format - ABC : 1234

Hi All,
      i want to develope a batch for my requirement is to Fetch All opportunities which  has recordtype =time rack , 
       and check if their name STARTS with  thier Related account's name then update name of opportunity  Should be in this format - {!Account.Name} : Opportunity Name and populate Opportunity's field Customer Type = 'End User' on opportunity.

Ex:  Opportunity name is ABC 1234 and account name is ABC  Than after running the batch OppName would become- ABC: 1234 and its customer type should become -'End User'
Best Answer chosen by sumit d
Hi Sumit,

Try something like this:
public class DFBatchExample implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {    
        Id timeRackID = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('time_rack').getRecordTypeId();
        return Database.getquerylocator([SELECT Id,Name,Account.Name
                                         FROM Opportunity WHERE RecordTypeId =: timeRackID AND AccountId != NULL ]);
    public void execute(Database.BatchableContext bc, List<Opportunity> records){
        List<Opportunity> finalOpp = new List<Opportunity>();
        for(Opportunity opp : records ) {
            if( opp.Name.startsWith(opp.Account.Name))	
        for(Opportunity op: finalOpp) {
            op.Name = op.Account.Name + ':'+ op.Name.remove(op.Account.Name);
            //op.Customer_Type__c = 'End User';
        update finalOpp;
    public void finish(Database.BatchableContext bc){ 
Hope this helps!