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
yogendra Aragula 8yogendra Aragula 8 

batch apex sosl

Hi All,

I am looking for a batch apex where i can add some string to the end of the email ID to make it inactive. ex: abc_123@gmai.com+007
some vaule at the end of the email ID. I have a free text where users enters multiple email addresses in this ( Format: salesforce@yahoo.com;sdfc@hotmail.com;developer_sdfc@aol.com). Now i have a batch apex which adds a dummy string at the end of the value in the that field. However i want to add a dummy string after every .com. So how should i find that .com from that field and add some sting to it using batchapex.

Thank you
Best Answer chosen by yogendra Aragula 8
John Pipkin 14John Pipkin 14
You would need to use the split() method to break it out and then join() to put them back together. Here is the code that should work:
 
global class addstring implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,emailID__c FROM Testemail__c';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, list<Testemail__c> reference){
        List<Testemail__c> asdf = new List<Testemail__c>();
        for(Testemail__c abc : reference){
            if(abc.EmailID__c != null){
                List<String> emailList = new List<String>();
                for(String s :abc.emailID__c.split(';')){
                    emailList.add(s + 'hi');
                }
                abc.emailID__c = String.join(emailList,';');
                asdf.add(abc);
            }
        }
        if(!asdf.isEmpty()){
            Update asdf;
        } 
    }
    global void finish(Database.BatchableContext BC) {
    }
}

Hope that helps

All Answers

John Pipkin 14John Pipkin 14
Yogendra, 

Check out this stackexchange thread. http://salesforce.stackexchange.com/questions/53312/sosl-query-in-batch-apex

Hope that helps
Matthew CokeMatthew Coke
you can use a regex statement to do this
yogendra Aragula 8yogendra Aragula 8
global class addstring implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,emailID__c FROM Testemail__c';
        return Database.getQueryLocator(query);
    }
global void execute(Database.BatchableContext BC, list<Testemail__c> reference)  
            {
            List<Testemail__c> asdf = new List<Testemail__c>();
            for(Testemail__c abc : reference)
         {
                        abc.emailID__c = abc.emailID__c + 'hi';
                        asdf.add(abc);
                        }
            if(!asdf.isEmpty()){
                        Update asdf;
            }
    }
    global void finish(Database.BatchableContext BC) {
    }
}


This is my code 
yogendra Aragula 8yogendra Aragula 8
global class addstring implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,emailID__c FROM Testemail__c';
        return Database.getQueryLocator(query);
    }
global void execute(Database.BatchableContext BC, list<Testemail__c> reference)   
            {
            List<Testemail__c> asdf = new List<Testemail__c>();
            for(Testemail__c abc : reference) 
         {
                        abc.emailID__c = abc.emailID__c + 'hi';
                        asdf.add(abc);
                        }
            if(!asdf.isEmpty()){
                        Update asdf;
            } 
    }
    global void finish(Database.BatchableContext BC) {
    }



This is my code where i want to add some thing to every thing which ends with .com in that field. How to achive it
 
John Pipkin 14John Pipkin 14
You would need to use the split() method to break it out and then join() to put them back together. Here is the code that should work:
 
global class addstring implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,emailID__c FROM Testemail__c';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, list<Testemail__c> reference){
        List<Testemail__c> asdf = new List<Testemail__c>();
        for(Testemail__c abc : reference){
            if(abc.EmailID__c != null){
                List<String> emailList = new List<String>();
                for(String s :abc.emailID__c.split(';')){
                    emailList.add(s + 'hi');
                }
                abc.emailID__c = String.join(emailList,';');
                asdf.add(abc);
            }
        }
        if(!asdf.isEmpty()){
            Update asdf;
        } 
    }
    global void finish(Database.BatchableContext BC) {
    }
}

Hope that helps
This was selected as the best answer
yogendra Aragula 8yogendra Aragula 8
It works prefect for ( ; )
But can you tell me if i can use two parameter in slipt method. I Also wanted to split it with ( , ) and ( . ) these two values.