function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
trigger AccountTrigger on Account( after update ) { if( Trigger.isAfter && Trigger.isUpdate ) { List<Account> accountsToProcess = new List<Account>(); for( Account acc : Trigger.new ) { if( acc.Stage__c == 'Hot' && Trigger.oldMap( acc.Id ).Stage__c == 'Warm' ) { accountsToProcess.add( acc ); } } if( accountsToProcess.size() > 0 ) { Database.executeBatch( new NewContactsBach( accountsToProcess ) ); } } }
global class NewContactBatch implements Database.Batchable<sObject> { String query; private List<Account> accountsToProcess; global NewContactBatch(List<Account> accountsToProcess) { this.accountsToProcess = accountsToProcess; } global Database.QueryLocator start(Database.BatchableContext BC) { query = 'Select Id, Stage__c, Name from Account where Id in :accountsToProcess'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<sObject> scope) { List<Account> accs = (List<Account>) scope; List<Contact> contactsToInsert = new List<Contact>(); for( Account acc : accs ) { Contact newContact = new Contact(); newContact.AccountId = acc.Id; // Add aditional fields here contactsToInsert.add( newContact ); } if( contactsToInsert.size() > 0 ) insert contactsToInsert; } global void finish(Database.BatchableContext BC) { } }
I think the best approach is to create a trigger on Account, because you would be able to check Trigger.new values against Trigger.oldValue and then you can call a batch. Try the following code:
AccountTrigger.trigger
NewContactsBach.cls
I hope you find this solution useful. If it does please mark as Best Answer to help others too.
Regards.