You need to sign in to do that
Don't have an account?
symantecAP
Change in child should should change parent in batch
Hi All
I am trying to write a class where change in child object field will change a field on Parent Object .
I will have to then schedule the same.
I am not sure if my logic is right. I am first querying all the Child records. and putting if statement in my database.execute to check for the field change.
Child object : BigMachines__Quote__c looks up to Opportunity
if BigMachines__Quote__c.status__c = *unison* then Opportunity.stagename = closed won.
I have written following so far. Kindly help .
global class updateOpportunityStage implements Database.Batchable<sObject>,Schedulable{ global string query = 'SELECT id,BigMachines__Status__c from BigMachines__Quote__c '; global database.querylocator start(Database.BatchableContext BC){ //List<BigMachines__Quote__c> quoteList= new List<BigMachines__Quote__c>(); //BigMachines__Quote__c bmq = [Select id,BigMachines__Status__c from BigMachines__Quote__c where BigMachines__Status__c = '*unison*']; return Database.getQueryLocator(query); } global void execute(SchedulableContext SC){ updateOpportunityStage stg = new updateOpportunityStage(); database.executebatch(stg); } global void execute(Database.BatchableContext BC, List<sObject> scope){ List <Opportunity> oppList = new List<Opportunity>() ; for(sObject s : scope){ BigMachines__Quote__c quote = (BigMachines__Quote__c)s; System.debug('Adil'+quote); for (List< BigMachines__Quote__c> bmq :[SELECT id,BigMachines__Status__c from BigMachines__Quote__c ]){ if(quote.BigMachines__Status__c.contains ('unison') && quote.BigMachines__Is_Primary__c == true ){ Opportunity opp = [select id, StageName from Opportunity where id=:quote.id]; opp.stageName = 'Closed Won' ; } update oppList; } } } global void finish(Database.BatchableContext BC){} }
Thanks in Advance
Try this
--yvk
All Answers
How are you determining the change. I mean what if the parent is modified instead of child then you still need to update the parent with the child. If the parent have different childs and different values how we know which is modified?
Thank yoy for the reply
usually only one child record goes in to status called unison. so if any 1 of the child records is unison then that updates opportunity.stagename field.
And if the parent gets edited then there is no change either to parent or child records.
Hope i am clear this time.
What is the need of the batch, why not trigger on child.
using trigger you can retirive only those parents which needs to be modified.
--yvk
yes right but the child object is a managed package Big_Machine_Quote__c and we dont want to use a trigger and we want to schedule it in a such a way that it runs every 15 mins and check the logic and update field on Opportunity .
Thanks
Adil
Hi
Here is my modified code
Now i get error in my Apex job that is
" First error: Attempt to de-reference a null object”
Thanks
Adil
Try this
--yvk
Thanks
I have scheduled the code its says status as Completed but total batches are only 1 and batch processed 1. failures zero.
why is it procesing only 1 record.
Thanks
Adil
Can you tell my why the total batch processed is only 1 ?
That we cannot say, it depends on the records and all.
Try to run the query you are passing from system log or some thing and verify.
--yvk
Thanks for the reply.. i am checking the data as to y only one record is being processed.
Now the real concern is it doesnot update the Opportunity..
Thanks
Adil