• Mayank Sharma 21
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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;
    }