+ Start a Discussion
force developer 35force developer 35 

Create Batch apex class to create Opportunity records based on Contact Data

I am completely new to Apex and coding.I have a requirement where I need to create Opportunities for Contact objects when a Picklist field on contact object gets updated.
Which means when the picklist filed is having any of the following values then an opportunity should get created for that contact automatically...
My picklist field values are: Service every year, Service every 6 months, Phone call and Send mail.
Now I need some help in writing the batch apex class and test class for this requirement. Any help is much appreciated.
Thanks in advance.
Best Answer chosen by force developer 35
Soyab HussainSoyab Hussain
Hi ,
Use this batch class this will help you, and you can modify picklist values according to you.
global class CreateOpportunityForContacts implements Database.Batchable<sObject>{
    
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Set<String> picklists = new Set<String>{'Service every year', 'Service every 6 months', 'Phone call', 'Send mail'};
            String query ='Select id, Name, Contact_frequency__c from Contact where Contact_frequency__c IN :picklists limit 1000'; 
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Contact> scope){
        List<Opportunity> OpportunityList = new List<Opportunity>();
        for(Contact a : scope) { 
            //set your opportunity values here
            Opportunity opp = new Opportunity();
            opp.name = a.Name;
            opp.CloseDate=date.Today();
            opp.StageName='Quoted';
            opp.ContactId = a.id; 
            OpportunityList.add(opp); 
        } 
        if(OpportunityList.size() > 0)
            insert OpportunityList; 
    }
    
    global void finish(Database.BatchableContext BC)    {
    }
}
If you found it useful please appreciate my efforts and mark it as the best answer.

LinkedIn:  https://www.linkedin.com/in/soyab-hussain-b380b1194/

Regards 
Soyab
 

All Answers

Soyab HussainSoyab Hussain
Hi force developer,
What do you need batch class OR Contact.Trigger ??
force developer 35force developer 35
Hi Soyab, 
I need a batch class which runs every quarter.
Soyab HussainSoyab Hussain
Okay, give me name of your picklist field name.
force developer 35force developer 35
My Picklist field name is Contact frequency 
I also need to check one more picklist field value on contact object before creating the opportunity.
The second picklist field name is Contact Month
Soyab HussainSoyab Hussain
Hi ,
Use this batch class this will help you, and you can modify picklist values according to you.
global class CreateOpportunityForContacts implements Database.Batchable<sObject>{
    
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Set<String> picklists = new Set<String>{'Service every year', 'Service every 6 months', 'Phone call', 'Send mail'};
            String query ='Select id, Name, Contact_frequency__c from Contact where Contact_frequency__c IN :picklists limit 1000'; 
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Contact> scope){
        List<Opportunity> OpportunityList = new List<Opportunity>();
        for(Contact a : scope) { 
            //set your opportunity values here
            Opportunity opp = new Opportunity();
            opp.name = a.Name;
            opp.CloseDate=date.Today();
            opp.StageName='Quoted';
            opp.ContactId = a.id; 
            OpportunityList.add(opp); 
        } 
        if(OpportunityList.size() > 0)
            insert OpportunityList; 
    }
    
    global void finish(Database.BatchableContext BC)    {
    }
}
If you found it useful please appreciate my efforts and mark it as the best answer.

LinkedIn:  https://www.linkedin.com/in/soyab-hussain-b380b1194/

Regards 
Soyab
 
This was selected as the best answer