You need to sign in to do that
Don't have an account?
Asakti
batch class error for count of custom object records
i have written the below trigger
trigger CountVistorOnCustomer on Visit__c (After insert) {
Id devRecordTypeId = Schema.SObjectType.Visit__c.getRecordTypeInfosByName().get('Existing Customer Visit').getRecordTypeId();
List<Account> ct = new List<Account>();
List<Account> ct1 = new List<Account>();
List<Account> ct2 = new List<Account>();
List<Account> ct3 = new List<Account>();
set<Id> customerId = new set<Id>();
Integer orgFiscalMonth = [SELECT FiscalYearStartMonth FROM Organization].FiscalYearStartMonth; //april
Date orgFiscalYearStart = Date.newinstance(system.today().year(), orgFiscalMonth, 1);//2020-04-01
Date orgFiscalYearEnd = Date.newinstance(system.today().year(), orgFiscalMonth + 11, 31);
Integer PreviousYear = system.today().year() - 1;
Integer NextYear = system.today().year() + 1;
Integer CurrentYear = system.today().year();
if(Trigger.isInsert) {
for(Visit__c v1:Trigger.New) {
customerId.add(v1.Customer__c);
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id), Customer__c FROM Visit__c
where Customer__c IN :customerId and
VisitDate__c>=:orgFiscalYearStart
and VisitDate__c<=:orgFiscalYearEnd
and RecordTypeId ='012O0000000KkAVIA0'
GROUP BY Customer__c ];
AggregateResult[] groupedResults1 = [SELECT COUNT(Id), Customer__c FROM Visit__c
where Customer__c IN :customerId
and VisitDate__c = LAST_FISCAL_QUARTER
and RecordTypeId ='012O0000000KkAVIA0'
GROUP BY Customer__c ];
AggregateResult[] groupedResults2 = [SELECT COUNT(Id), Customer__c FROM Visit__c
where Customer__c IN :customerId
and VisitDate__c = THIS_QUARTER
and RecordTypeId ='012O0000000KkAVIA0'
GROUP BY Customer__c ];
for(AggregateResult ar:groupedResults) {
Id custid = (ID)ar.get('Customer__c');
Integer count = (INTEGER)ar.get('expr0');
Account cust1 = new Account(Id=custid);
cust1.Current_FY_for_Report__c = count;
ct1.add(cust1);
}
for(AggregateResult ar:groupedResults1) {
system.debug('Trigger Fired groupedResults second one'+groupedResults1);
Id custid = (ID)ar.get('Customer__c');
Integer count1 = (INTEGER)ar.get('expr0');
Account cust1 = new Account(Id=custid);
cust1.Previous_Quater_For_Report__c = count1;
ct2.add(cust1);
}
for(AggregateResult ar:groupedResults2) {
system.debug('Trigger Fired groupedResults third one'+groupedResults1);
Id custid = (ID)ar.get('Customer__c');
Integer count2 = (INTEGER)ar.get('expr0');
Account cust1 = new Account(Id=custid);
cust1.Current_Quarter__c = count2;
ct3.add(cust1);
}
system.debug('Trigger Fired ct'+ct);
update ct1;
update ct2;
update ct3;
ct.addAll(ct1);
ct.addAll(ct2);
ct.addAll(ct3);
}
batch class for the same
global class CountVisitorBatch implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'Select ID,Previous_Quater_For_Report__c,(Select Id,Customer__c from Visit__r) from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext info, List<Account> Accounts){
list<account> updatedList = new list<account>();
if(Accounts.size() > 0){
for(account acc : Accounts){
acc.Previous_Quater_For_Report__c = acc.Visit__r.size();
updatedList.add(acc);
}
}
if(updatedList.size() > 0){
update updatedList;
}
}
global void finish(Database.BatchableContext info){
}
}
But getting error
Variable does not exist: Visit__r
Can someone please help me with the batch class
trigger CountVistorOnCustomer on Visit__c (After insert) {
Id devRecordTypeId = Schema.SObjectType.Visit__c.getRecordTypeInfosByName().get('Existing Customer Visit').getRecordTypeId();
List<Account> ct = new List<Account>();
List<Account> ct1 = new List<Account>();
List<Account> ct2 = new List<Account>();
List<Account> ct3 = new List<Account>();
set<Id> customerId = new set<Id>();
Integer orgFiscalMonth = [SELECT FiscalYearStartMonth FROM Organization].FiscalYearStartMonth; //april
Date orgFiscalYearStart = Date.newinstance(system.today().year(), orgFiscalMonth, 1);//2020-04-01
Date orgFiscalYearEnd = Date.newinstance(system.today().year(), orgFiscalMonth + 11, 31);
Integer PreviousYear = system.today().year() - 1;
Integer NextYear = system.today().year() + 1;
Integer CurrentYear = system.today().year();
if(Trigger.isInsert) {
for(Visit__c v1:Trigger.New) {
customerId.add(v1.Customer__c);
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id), Customer__c FROM Visit__c
where Customer__c IN :customerId and
VisitDate__c>=:orgFiscalYearStart
and VisitDate__c<=:orgFiscalYearEnd
and RecordTypeId ='012O0000000KkAVIA0'
GROUP BY Customer__c ];
AggregateResult[] groupedResults1 = [SELECT COUNT(Id), Customer__c FROM Visit__c
where Customer__c IN :customerId
and VisitDate__c = LAST_FISCAL_QUARTER
and RecordTypeId ='012O0000000KkAVIA0'
GROUP BY Customer__c ];
AggregateResult[] groupedResults2 = [SELECT COUNT(Id), Customer__c FROM Visit__c
where Customer__c IN :customerId
and VisitDate__c = THIS_QUARTER
and RecordTypeId ='012O0000000KkAVIA0'
GROUP BY Customer__c ];
for(AggregateResult ar:groupedResults) {
Id custid = (ID)ar.get('Customer__c');
Integer count = (INTEGER)ar.get('expr0');
Account cust1 = new Account(Id=custid);
cust1.Current_FY_for_Report__c = count;
ct1.add(cust1);
}
for(AggregateResult ar:groupedResults1) {
system.debug('Trigger Fired groupedResults second one'+groupedResults1);
Id custid = (ID)ar.get('Customer__c');
Integer count1 = (INTEGER)ar.get('expr0');
Account cust1 = new Account(Id=custid);
cust1.Previous_Quater_For_Report__c = count1;
ct2.add(cust1);
}
for(AggregateResult ar:groupedResults2) {
system.debug('Trigger Fired groupedResults third one'+groupedResults1);
Id custid = (ID)ar.get('Customer__c');
Integer count2 = (INTEGER)ar.get('expr0');
Account cust1 = new Account(Id=custid);
cust1.Current_Quarter__c = count2;
ct3.add(cust1);
}
system.debug('Trigger Fired ct'+ct);
update ct1;
update ct2;
update ct3;
ct.addAll(ct1);
ct.addAll(ct2);
ct.addAll(ct3);
}
batch class for the same
global class CountVisitorBatch implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'Select ID,Previous_Quater_For_Report__c,(Select Id,Customer__c from Visit__r) from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext info, List<Account> Accounts){
list<account> updatedList = new list<account>();
if(Accounts.size() > 0){
for(account acc : Accounts){
acc.Previous_Quater_For_Report__c = acc.Visit__r.size();
updatedList.add(acc);
}
}
if(updatedList.size() > 0){
update updatedList;
}
}
global void finish(Database.BatchableContext info){
}
}
But getting error
Variable does not exist: Visit__r
Can someone please help me with the batch class
Hi,
Please check the Child Relationship Name is correct or not on your Visit__c object from custom Account lookup field...
Setup --> Object --> Visit --> Account Lookup Field --> Open the field don't click on the edit -> Loookup Options section --> Child Relationship Name --> ChildRelationshipName__r you have to use.
Thanks,
Maharajan.C