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
SV MSV M 

Add opportunity to a specific account

Hi,

I would like to add opportunities whose Stage and Type is New Opportunity and New Business. I have added a new account record and these opportunities should be linked to the newly created account record.

//Batch Class

global class BatchForAddingOpps implements Database.Batchable<sObject> {
    List<Opportunity> oppList {get;set;}
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator([SELECT Name, Type, StageName, CreatedDate 
                                         FROM Opportunity 
                                         WHERE Type = 'New Business' AND StageName = 'New Opportunity']);
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> oppList) {
        List<Opportunity> newList = new List<Opportunity>();
        for(Opportunity opp : oppList) {
            opp.StageName = 'Closed Lost';
        }
        update oppList;
        System.debug('newList'+oppList);
    }
    global void finish(Database.BatchableContext BC) {
        
    }
}

I've got the opportunities but unable to add these opportunities to newly created account record...Can someone help me achieve this...

Thanks in advance.
Gaurav Sharma 472Gaurav Sharma 472
but you are not updating accountid in execute method.


if you want to associate an account with opportunity.

update accountid on opportunity with id of account