Skip to main content
Mayank Sharma a posé une question dans #Apex
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;

    }
4 réponses
  1. 16 mars 2017, 19:13
    Hi Mayank,

    Below code should help you.

     

    Map<String,Set<String>> clinicApptMonthMap = new Map<String,Set<String>>();

    Set<String> clinicSet = new Set<String>();

    Map<string,Integer> clinicApptMonthCounterMap = new Map<String,Ineger>();

    List<Payment__c> updatednewPaymentRecords = new List<Payment__c>();

    for(Payment__c payRec : newMap.values()){

    clinicSet.add(payRec.ClinicIdAuto__c);

    if(clinicApptMonthMap.get(payRec.ClinicIdAuto__c)!=null){

    Set<String> newSet = clinicApptMonthMap.get(payRec.ClinicIdAuto__c);

    newSet.add(payRec.OpportunityAppointmentMonthAuto__c);

    clinicApptMonthMap.put(payRec.ClinicIdAuto__c,newSet);

    }

    else

    clinicApptMonthMap.put(payRec.ClinicIdAuto__c, new Set<String>{payRec.OpportunityAppointmentMonthAuto__c});

    }

    for(Payment__c iterator : [select id, Opportunity__r.Clinic__c, OpportunityAppointmentMonthAuto__c from Payment__c where Opportunity__r.Clinic__c in :clinicSet and InvoiceNumberAuto__c != null

    and (TransactionStatus__c = 'Paid' OR TransactionStatus__c = 'Credit note')]){

    if(clinicApptMonthMap.get(iterator.ClinicIdAuto__c)!=null){

    if(clinicApptMonthMap.get(iterator.ClinicIdAuto__c).contains(iterator.OpportunityAppointmentMonthAuto__c)){

    if(clinicApptMonthCounterMap.get(iterator.ClinicIdAuto__c+iterator.OpportunityAppointmentMonthAuto__c)!= null){

    clinicApptMonthCounterMap.put(iterator.ClinicIdAuto__c+iterator.OpportunityAppointmentMonthAuto__c,clinicApptMonthCounterMap.get(iterator.ClinicIdAuto__c+iterator.OpportunityAppointmentMonthAuto__c)+1)

    else

    clinicApptMonthCounterMap.put(iterator.ClinicIdAuto__c+iterator.OpportunityAppointmentMonthAuto__c,1);

    }

    }

    }

    for(Payment__c payRec : newMap.values()){

    payRec.InvoiceDate__c = System.today();

    payRec.InvoiceNumberAuto__c = (payRec.TransactionStatus__c == 'Credit note')?'G-VU-':'TR-VU-'+payRec.OpportunityAppointmentDate__c.format('MMYY') + '-' + payRec.ClinicInvoiceCodeAuto__c + '-' + (clinicApptMonthCounterMap.get(payRec.ClinicIdAuto__c+payRec.OpportunityAppointmentMonthAuto__c) + 1);

    updatednewPaymentRecords.add(payRec);

    }

    if(updatednewPaymentRecords.size() > 0)

    update updatednewPaymentRecords;

    Dont forget to give a thumbsup if this helps you.

    Regards,

    Zaja