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
rv90rv90 

Get a value from account object in custom object trigger

Hi there, 

I have financial Account as a custom object and account as a standard object, 

In financial object  i have a  look up relation field (Client_Account__r) with account. 

Now, i am coding in FinancialAccount trigger handler class - I need to fetch "Name" value from Account. 

By using formula field i successfully got the value from account object by using - - Client_Account__r.name 

But when coding in apex trigger class, i am not able to fetch the value- below is my code- 
 
 for (Financial_Account__c fa:triggernew)
 {
string finalstr='';
string totalstr='';
string cName = fa.Client_Account__r.Name;
           
   IF(cName!= Null){
             IF( cName.contains ('NDT/')) {
       	     	totalstr= cName.removeStart('NDT/');
             }else{
                 totalstr = cName;
            }
         }
}
I am getting null in cName variable .
Client_Account__c is a custom field in financial account object and is a lookup with account object.



 
Best Answer chosen by rv90
Amit Chaudhary 8Amit Chaudhary 8
try like below. Try to query the Client_Account__r.Name  field
Set<Id> setId = new set<ID>();
for(Financial_Account__c fa:trigger.new)
{
	setId.add(fa.id);
}

List<Financial_Account__c> lstFA = [select id,fa.Client_Account__r.Name from Financial_Account__c where id in:setId ];

for(Financial_Account__c fa:lstFA)
{
	string finalstr='';
	string totalstr='';
	string cName = fa.Client_Account__r.Name;

	IF(cName!= Null)
	{
		IF( cName.contains ('NDT/')) 
		{
			totalstr= cName.removeStart('NDT/');
		}
		else
		{
			totalstr = cName;
		}
	}
}

Let us know if this will help you
 

All Answers

Raj VakatiRaj Vakati
Please try like this  
for (Financial_Account__c fa:triggernew)
 {
string finalstr='';
string totalstr='';
string cName = fa.Client_Account__r.Name; 
           
   IF(cName!= Null && cName!=''){
             IF( cName.contains ('NDT/')) {
       	     	totalstr= cName.removeStart('NDT/');
             }else{
                 totalstr = cName;
            }
         }
}

 
Amit Chaudhary 8Amit Chaudhary 8
try like below. Try to query the Client_Account__r.Name  field
Set<Id> setId = new set<ID>();
for(Financial_Account__c fa:trigger.new)
{
	setId.add(fa.id);
}

List<Financial_Account__c> lstFA = [select id,fa.Client_Account__r.Name from Financial_Account__c where id in:setId ];

for(Financial_Account__c fa:lstFA)
{
	string finalstr='';
	string totalstr='';
	string cName = fa.Client_Account__r.Name;

	IF(cName!= Null)
	{
		IF( cName.contains ('NDT/')) 
		{
			totalstr= cName.removeStart('NDT/');
		}
		else
		{
			totalstr = cName;
		}
	}
}

Let us know if this will help you
 
This was selected as the best answer
rv90rv90
Thanks Amit- It worked. :)