+ Start a Discussion
Akash Choudhary 17Akash Choudhary 17 

Update a Field of child record

Hi All,

I am attemption to update a field of a child object of a master detail .

Here is the code:

public class SubmitApproval {

public static void createAccountManager(List<SellerAuctionItem__c> triggerNewList){
      
        List<string> AuctionSet = new List<String>();
        for (SellerAuctionItem__c item : triggerNewList) {
            if(item.Auction__c != null){
                AuctionSet.add(item.Auction__r.Name);
                system.debug('Name' +AuctionSet);
            }
        }
        
        Map<String, Auctions__c> mapAuction = new Map<String,Auctions__c>([Select Name , Account_Manager__c From Auctions__c  WHERE Name IN:AuctionSet ]);
        system.debug('Name and account manager' +mapAuction);
        for (SellerAuctionItem__c item : triggerNewList) {
            if(item.Account_Manager__c == Null){
            item.Account_Manager__c = mapAuction.get(item.Auction__c).Account_Manager__c;
            }    }

    }   
}

It is giving null pointer error : Attempt to de-reference a null object

Please help, Thanks
 
Best Answer chosen by Akash Choudhary 17
Nishant Prajapati 10Nishant Prajapati 10
Try this code
public class SubmitApproval {

public static void createAccountManager(List<SellerAuctionItem__c> triggerNewList){
      
        List<string> AuctionSet = new List<String>();
        for (SellerAuctionItem__c item : triggerNewList) {
            if(item.Auction__c != null){
                AuctionSet.add(item.Auction__c);
                system.debug('Name' +AuctionSet);
            }
        }
        
        Map<Id, Auctions__c> mapAuction = new Map<Id,Auctions__c>([Select Name , Account_Manager__c From Auctions__c  WHERE Id IN:AuctionSet ]);
        system.debug('Name and account manager' +mapAuction);
        for (SellerAuctionItem__c item : triggerNewList) {
            if(item.Account_Manager__c == Null){
            item.Account_Manager__c = mapAuction.get(item.Auction__c).Account_Manager__c;
            }    }

    }   
}

 

All Answers

Nishant Prajapati 10Nishant Prajapati 10
Hi Akash,
The problem is, You're trying to traverse on parent object and fetching its name field, 
 AuctionSet.add(item.Auction__r.Name);
If its a Handler class of trigger and you're passing Trigger.New to createAccountManager method as argument, you must need to query, you can't traverse like that.
Akash Choudhary 17Akash Choudhary 17
Hi Nishant, 
Then how according to yoy the code should look like .
thanks
Nishant Prajapati 10Nishant Prajapati 10
Try this code
public class SubmitApproval {

public static void createAccountManager(List<SellerAuctionItem__c> triggerNewList){
      
        List<string> AuctionSet = new List<String>();
        for (SellerAuctionItem__c item : triggerNewList) {
            if(item.Auction__c != null){
                AuctionSet.add(item.Auction__c);
                system.debug('Name' +AuctionSet);
            }
        }
        
        Map<Id, Auctions__c> mapAuction = new Map<Id,Auctions__c>([Select Name , Account_Manager__c From Auctions__c  WHERE Id IN:AuctionSet ]);
        system.debug('Name and account manager' +mapAuction);
        for (SellerAuctionItem__c item : triggerNewList) {
            if(item.Account_Manager__c == Null){
            item.Account_Manager__c = mapAuction.get(item.Auction__c).Account_Manager__c;
            }    }

    }   
}

 
This was selected as the best answer
Akash Choudhary 17Akash Choudhary 17
Hi Nishant,

The code worked, but instead of the name it fetched the ID.
 
Akash Choudhary 17Akash Choudhary 17
Its done Thanks for the help Nishant.