You need to sign in to do that
Don't have an account?
Mayank Sharma 21
avoid soql in for loop
Below is the code. Can someone help to get the count outside for loop
public static void generateInvoiceIds(Map<Id, Payment__c> newMap) {
List<Payment__c> updatednewPaymentRecords = new List<Payment__c>();
for(Payment__c record :newMap.values()) {
if(record.TransactionStatus__c == 'Credit note' && record.InvoiceNumberAuto__c == null){
Payment__c record1 = new Payment__c(id=record.id);
record1.InvoiceDate__c = System.today();
// system.debug('CLINIC ID - ' + newPaymentRecord.ClinicIdAuto__c + ' --- APPOINTMENT MONTH - ' + newPaymentRecord.OpportunityAppointmentMonthAuto__c);
Integer numberOfInvoices = [select count() from Payment__c where Opportunity__r.Clinic__c = :record.ClinicIdAuto__c and InvoiceNumberAuto__c != null and (TransactionStatus__c = 'Paid' OR TransactionStatus__c = 'Credit note') and OpportunityAppointmentMonthAuto__c = :record.OpportunityAppointmentMonthAuto__c];
if(record.TransactionStatus__c == 'Credit note')
record1.InvoiceNumberAuto__c = 'G-VU-' + record.OpportunityAppointmentDate__c.format('MMYY') + '-' + record.ClinicInvoiceCodeAuto__c + '-' + (numberOfInvoices + 1);
else
record1.InvoiceNumberAuto__c = 'TR-VU-' + record.OpportunityAppointmentDate__c.format('MMYY') + '-' + record.ClinicInvoiceCodeAuto__c + '-' + (numberOfInvoices + 1);
updatednewPaymentRecords.add(record1);
}
}
update updatednewPaymentRecords;
}
public static void generateInvoiceIds(Map<Id, Payment__c> newMap) {
List<Payment__c> updatednewPaymentRecords = new List<Payment__c>();
for(Payment__c record :newMap.values()) {
if(record.TransactionStatus__c == 'Credit note' && record.InvoiceNumberAuto__c == null){
Payment__c record1 = new Payment__c(id=record.id);
record1.InvoiceDate__c = System.today();
// system.debug('CLINIC ID - ' + newPaymentRecord.ClinicIdAuto__c + ' --- APPOINTMENT MONTH - ' + newPaymentRecord.OpportunityAppointmentMonthAuto__c);
Integer numberOfInvoices = [select count() from Payment__c where Opportunity__r.Clinic__c = :record.ClinicIdAuto__c and InvoiceNumberAuto__c != null and (TransactionStatus__c = 'Paid' OR TransactionStatus__c = 'Credit note') and OpportunityAppointmentMonthAuto__c = :record.OpportunityAppointmentMonthAuto__c];
if(record.TransactionStatus__c == 'Credit note')
record1.InvoiceNumberAuto__c = 'G-VU-' + record.OpportunityAppointmentDate__c.format('MMYY') + '-' + record.ClinicInvoiceCodeAuto__c + '-' + (numberOfInvoices + 1);
else
record1.InvoiceNumberAuto__c = 'TR-VU-' + record.OpportunityAppointmentDate__c.format('MMYY') + '-' + record.ClinicInvoiceCodeAuto__c + '-' + (numberOfInvoices + 1);
updatednewPaymentRecords.add(record1);
}
}
update updatednewPaymentRecords;
}
Below code should help you.
Dont forget to give a thumbsup if this helps you.
Regards,
Zaja
All Answers
Let us know if it helps.
Let us know if this will help you
Below code should help you.
Dont forget to give a thumbsup if this helps you.
Regards,
Zaja