0

I'm writing a batch process where the invoice status is DUE Send an Email based on the Configuration done in metadata.

 global   class InvoiceDueNotification implements Database.batchable<sObject>{

  set<decimal> varDueDays = new set<decimal>();
  string varInvoiceStage = 'Due';
  global Database.QueryLocator start(Database.BatchableContext bc){
   list<Invoice_Reminder__mdt> MetaData = [SELECT Id, Label, No_of_Days__c FROM 
                                            Invoice_Reminder__mdt WHERE Active__c = true];
    For(Invoice_Reminder__mdt a : MetaData)
    {
        // varDueDaysLabel.add(a.Label);
        varDueDays.add( Integer.valueOf(a.No_of_Days__c));

    }
    String query = 'SELECT  Id, Family_Member__r.Name, Amount__c, Due_Date__c , Month__c, Name, Program_Name__c, Program_Price__c, stage__c, Service_Center__r.Name,    Service_Center__r.D2_BEFORE_DUE_DATE__c Service_Center__r.On_Due_Date__c FROM SCInvoice__c where Stage__c =:varInvoiceStage and  Due_Days__c in: varDueDays' ;        
    system.debug('query' + query);
    return Database.getQueryLocator(query);

  }

 global void execute(Database.BatchableContext BC,  List<sObject> scope){

 list<Invoice_Reminder__mdt> MetaData = [SELECT Id, Label, No_of_Days__c FROM Invoice_Reminder__mdt WHERE Active__c = true];
    For(Invoice_Reminder__mdt a : MetaData)
    {            
for(SCInvoice__c inv :(List<SCInvoice__c>) scope){

// Like inv.a.label

     }
}

}

In metadata values will be D2_BEFORE_DUE_DATE__c, .....On_Due_Date__c with Status. In Account object Fields has been created with the same API name as metadata value.

In the above for loop For(Invoice_Reminder__mdt a : MetaData) a.label value will be D2_BEFORE_DUE_DATE__c, how do we dynamically fetch the column in nested for loop

Ex: If metadata value is D2_BEFORE_DUE_DATE__c then fetch the D2_BEFORE_DUE_DATE__c column in inner loop.

Can You please help me o this.

1 Answer 1

0

You can simply use SObject's get method:

for(SCInvoice__c inv :(List<SCInvoice__c>) scope){
        Object value = inv.get(a.label); //i'm assigning as object i dont know data type, you can typecast.

}

Also, if field is not coming directly from the same object then you will have to first get the value from parent lookup field then get that field like this:

inv.getSobject('Service_Center__c').get('D2_BEFORE_DUE_DATE__c');

Learn here all SObject methods to understand it: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm

Using sObject.get() to fetch a field value from a related parent object

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .