0

I need to display the account name with a slash between account name and the parent account name , if the parent account field is filled. I tried to do that using SObjectType methods. But couldn't do it. I am facing:

Invalid relationship fromo account to account.

Also if I removed getSobject('Account') I am facing:

null pointer exception.

Here is my code.

if(wrp.mySObject.getSObject('Account').getSObject('Parent').get('Name')!=null){
    wrp.mySObject.put('Name',wrp.mySObject.get('Name')+'/'+wrp.mySObject.getSObject('Account').getSObject('Parent').get('Name'));
}

Here wrp is the instance of my wrapper class and mySObject is the SObject variable used in that class. Please just tell me the right way to do it generally. I will implement the same according to my code.

5
  • What's the problem exactly?
    – Christoph
    Commented Jun 5, 2017 at 11:14
  • Please find my updated question. Forgot to add the exception i am facing
    – nonerd
    Commented Jun 5, 2017 at 11:18
  • Have you gone through developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Reshma
    Commented Jun 5, 2017 at 11:24
  • yes but couldn't sort out the problem unfortunately
    – nonerd
    Commented Jun 5, 2017 at 11:27
  • does your query select parent account ? can you provide your SOQL Commented Jun 5, 2017 at 12:36

2 Answers 2

0

You could take much clear approach using SOQL query. Given a sample approach.

Set<Id> AccountIds = new Set<Id>();
//capture account Ids

Map<Id, Account> mapAccount = Map<Id, Account>([SELECT Id, Name, ParentId, Parent.Name From Account WHERE Id IN:AccountIds]);

for(Id accountId:mapAccount.keySet())
{
    Account acct = mapAccount.get(accountId);

    //here loop through the wrapper and put names
    wrp.mySObject.put('Name',acct.Name+'/'+acct.Parent.Name);

}
0

I would create a Process that updates the Account. The process would run when the account is created. It would then check if the Parent Account is not null. If that field is not null it will update the Account Name with the following formula.

[Account].Name & '/' & [Account].Parent.Name 

This way you avoid unneeded code.

You must log in to answer this question.

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