You need to sign in to do that
Don't have an account?
Aidel Bruck
working with soql queries and dataloader
I have a custom object X I need to updated through the dataloader. When updated I need the related account to be updated for each custom object.
Obviously, my trigger uses a Soql query on each instance of the custom object to find the related account.
When I run the dataloader update I reach the limit and I am stuck.
Any ideas?
Obviously, my trigger uses a Soql query on each instance of the custom object to find the related account.
When I run the dataloader update I reach the limit and I am stuck.
Any ideas?
http://www.sfdc99.com/2014/01/20/soql-queries-inside-loops/
All Answers
This is not the ideal situation to update Related objects while inserting/updating mass # of Custom object, Please follow below approach:
- Bypass Account update for mass DML operation.
- Create a batch job, that will identify all the custom object records that has been updated recently.
- Pull related Account records and make necessary changes.
- Update account object.
Hope this helps!!!Thanks,
Gaurav
Skype: gaurav62990
Yes, anyway we need to do that uptil records get updated properly.
But, first complete below steps:
Thanks,
Gaurav
Skype: gaurav62990
trigger PaymentPaid on Payment__c (after insert, after update)
{
List<account> UpdatedAccounts= new list<account>();
If(trigger.isinsert)
for(payment__c pay :trigger.new )
If(pay.status__C=='paid')
{
system.debug('pay new'+pay.Check_Amount_G__c);
updatedAccounts=paymentpaidhelper.CreateUpdatedAccounts(pay, updatedaccounts);
}
If(trigger.isupdate)
for(payment__c pay :trigger.new )
for (payment__c payold: trigger.old)
if(pay.id== payold.id)
If(pay.status__C== 'paid' && payold.status__c != 'paid')
{
system.debug('pay update'+ pay.Check_Amount_G__c);
updatedAccounts=paymentpaidhelper.createupdatedaccounts(pay, updatedaccounts);
}
if(!updatedAccounts.isempty())
update updatedaccounts;
}
public class PaymentPaidHelper
{
public static List<Account> CreateUpdatedAccounts(Payment__C Pay, list<account> updatedAccounts )
{
integer i= 0;
account a= new account();
boolean remove= false;
integer stop= 0;
If(!updatedaccounts.isempty())
{
for(account acc: updatedaccounts)
{
a= [select Patient_Id__c, Last_Payment_Policy_ID__c, Last_Payment_Check_Amount__c, Last_payment_check_date__c, Last_Payment_Check_Number__c,
Last_payment_covering_month__c, Last_payment_Payee__c From Account Where Patient_ID__c =: pay.Patient_ID__c][0];
if(acc.patient_Id__C== a.patient_id__c)
{
a.Last_Payment_Policy_ID__c= acc.Last_Payment_Policy_ID__c+'; '+pay.approved_policy_name__C;
a.Last_Payment_Check_Amount__c= acc.Last_Payment_Check_Amount__c+ '; '+pay.Check_Amount_G__c;
a.Last_payment_check_date__c= pay.check_date__C;
a.Last_Payment_Check_Number__c=acc.Last_Payment_Check_Number__c+'; '+ pay.Check__c;
a.last_payment_covering_month__C= acc.last_payment_covering_month__C+'; '+pay.Covering_Period__c;
a.Last_payment_Payee__c= acc.Last_payment_Payee__c+'; '+pay.payee__c;
//system.debug('ids are equal: last payment policy: '+a.Last_Payment_Policy_ID__c+'/n last payment check amount '+ a.Last_Payment_Check_Amount__c+
//'/n last payment check date '+ a.Last_payment_check_date__c+'/n last payment check number '+ a.Last_Payment_Check_Number__c+
//'/n last payment covering month '+ a.Last_payment_covering_month__c+'/n last payment payee '+a.Last_payment_Payee__c);
remove= true;
stop= i;
}
else
{
a.Last_Payment_Policy_ID__c= pay.approved_policy_name__c;
a.Last_Payment_Check_Amount__c= pay.Check_Amount_G__c+'';
a.Last_payment_check_date__c= pay.check_date__C;
a.Last_Payment_Check_Number__c= pay.Check__c;
a.last_payment_covering_month__C= pay.Covering_Period__c;
a.Last_payment_Payee__c= pay.payee__c;
// system.debug('Ids are not equal: last payment policy: '+a.Last_Payment_Policy_ID__c+'/n last payment check amount '+ a.Last_Payment_Check_Amount__c+
//'/n last payment check date '+ a.Last_payment_check_date__c+'/n last payment check number '+ a.Last_Payment_Check_Number__c+
//'/n last payment covering month '+ a.Last_payment_covering_month__c+'/n last payment payee '+a.Last_payment_Payee__c);
}
i++;
}
if(remove)
updatedaccounts.remove(stop);
updatedAccounts.add(a);
}
else
{
a= [select Patient_Id__c, Last_Payment_Policy_ID__c, Last_Payment_Check_Amount__c, Last_payment_check_date__c, Last_Payment_Check_Number__c,
Last_payment_covering_month__c, Last_payment_Payee__c From Account Where Patient_ID__c =: pay.Patient_ID__c][0];
a.Last_Payment_Policy_ID__c= pay.approved_policy_name__C;
a.Last_Payment_Check_Amount__c= pay.Check_Amount_G__c+'';
a.Last_payment_check_date__c= pay.check_date__C;
a.Last_Payment_Check_Number__c= pay.Check__c;
a.last_payment_covering_month__C= pay.Covering_Period__c;
a.Last_payment_Payee__c= pay.payee__c;
updatedaccounts.add(a);
}
return updatedaccounts;
}
}
Hi Aidel,
Can you please contact me on skype, this need to be analyzed and add batch job for this scenario.
Thanks,
Gaurav
Skype: gaurav62990
http://www.sfdc99.com/2014/01/20/soql-queries-inside-loops/
is the problem solved?