function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ravi G 9Ravi G 9 

I am facing challenging while maping some fields from batch

I have custom object Invoice__c and which is having lookup of account on Invoice_Number__c.
  • Account recordtype is payment and requirement is like whenever this Payment recordtype account is not having any parent account then this Invoice_Number__c ParentId will be blank too.
  • If this Payment recordtype account have parent record then this should be map into this Invoice_Number__c parentId.

I am trying to implement like this and  It is failing and getting null exception.

if(accRec.ParentId != null ){
inv.Invoice_Number__r.parentId = accRec.ParentId;
}
Pls check this and help me on this. Thanks!!
Sunil Kumar 841Sunil Kumar 841
Can you add the above code? 
Prateek Prasoon 25Prateek Prasoon 25
Based on your requirement, you need to check if the Account record's record type is "Payment" and it does not have a parent account, then the Invoice_Number__c's ParentId should be blank. If the Payment record type account has a parent account, then you need to map the Invoice_Number__c's ParentId to the parent account's Id.
You can modify your code as follows to achieve this functionality:
//Check if the account's record type is "Payment" and it has no parent account
if(accRec.RecordType.DeveloperName == 'Payment' && accRec.ParentId == null ){
    inv.Invoice_Number__c = null; //Set the parent Id of the Invoice_Number__c to null
} else if (accRec.ParentId != null) {
    inv.Invoice_Number__c = accRec.ParentId; //Map the parent account's Id to the Invoice_Number__c's ParentId
}

This code first checks if the Account's record type is "Payment" and it has no parent account. If that's the case, then it sets the parent Id of the Invoice_Number__c to null.
If the Payment record type account has a parent account, then it maps the parent account's Id to the Invoice_Number__c's ParentId.
Make sure to replace "DeveloperName" with the actual Developer Name of your Record Type. Also, ensure that the API names of your custom objects and fields are correct.

If you find this answer helpful, Please mark it as the best answer.
Ravi G 9Ravi G 9

Hi Sunil, check this code:

public class UpdateInvoiceNumber implements Database.Batchable<sObject>,Database.AllowsCallouts,Database.stateful {

    public Database.QueryLocator start(Database.BatchableContext bcx){
        String query = 'Select Id,RecordTypeId,ParentId,isActive__c,snum__c From Account where isActive__c = true';
        return Database.getQueryLocator(query);
    }
    public void execute(Database.BatchableContext bcx, List<Account> records){
        
        system.debug('batch Account record--'+records);
        system.debug('batch Account record--'+records.size());
        List<Invoice__c> updatedList = new List<Invoice__c>();
        Id RecType = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('taxable').getRecordTypeId();
        map<id,Invoice__c> accmap = new map<id,Invoice__c>();

        try {
            List<Invoice__c> invList = [Select id,recordtypeId,freight__c,Invoice_Number__c,Invoice_Number__r.parentId
                                                         from Invoice__c Where RecordType.DeveloperName = 'payment' AND freight__c <> null AND ( Status__c = 'New' OR Status__c = 'Working')];
            if(invList != null){
                for(Account accRec : records){
                    for(Invoice__c inv :invList){
                        if(accRec.RecordTypeId == RecType && accRec.isActive__c == true && 
                           accRec.snum__c == inv.freight__c){
                               inv.Invoice_Number__c = accRec.Id;
                               
                              if(accRec.ParentId == null){
                                  system.debug('inv 1'+inv.Invoice_Number__r.parentId);
                                   inv.Invoice_Number__r.parentId = '';
                               }
                               if(accRec.ParentId != null ){
                                   inv.Invoice_Number__r.parentId = accRec.ParentId;
                                   system.debug('inv 2'+inv.Invoice_Number__r.parentId);
                               }
                               updatedList.add(inv);
                               accmap.putAll(updatedList);
                               System.debug('accmap'+accmap);
                           }
                    }
                }
            }
            if(accmap.size()>0){
                update accmap.values();
            }
            system.debug('update value####'+accmap);
            system.debug('size of map'+accmap.Size());
            
        }catch (Exception exp) {
            System.debug('TESTING'+ exp.getCause()+exp.getLineNumber()+exp.getMessage());
        }   
    }
    
    public void finish(Database.BatchableContext bcx){
        
    }
}