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
lauraecilauraeci 

Trying to get Account email from an Object with a master detail relationship with the account

I have an object that has a master detail relationship with the account.  I query the object and get the Account Id from it for an existing Account associated with myObject, myObject.Account_ID__c.

 

Trying to create a new account out of that Id so I can get the email from it but when I do the assignment to a new account, the email is null.  

 

Any help is appreciated!

 

Thanks!

-Laura

 

Account a = new Account(Id=myObject.Account_ID__c);

 

System.debug(a.PersonEmail);  // PersonEmail is null

 

 

HariDineshHariDinesh

Hi,

 

“Trying to create a new account out of that Id”.

 

What do you mean by this? You cannot create one more account with same id.

ID is the unique field in salesforce and you can’t have same id for any of the SF Components.

You can’t create account by your statement.

 

If you want to create a new account you need code like this.

 

Account a = new Account(name='hhhh12new');
 insert a;

 

And one more is there is no “PersonEmail” field in Account object.

If you want to create your custom object record with same account id you can do like this

 

// query account here.
account acc= [select id, name from account where name like '%%'];
myObject__c my = new myObject__c(name='mynew',myObject.Account_ID__c=acc.id);

 

 

sf_evolutionsf_evolution

Hi there,

 

Just taking a stab at it - hoping I understand your question correctly.

 

List <Account> a = [SELECT Id, Name, PersonEmail from Account aWHERE Id = :myObject.Account_ID__c LIMIT 1];

System.debug(a[0].PersonEmail);  


..There's much better ways to write this and I haven't tested it, but you should be able to improve on this

 

lauraecilauraeci

My issue was that I am doing this inside a loop which seemed inefficient.  

 

I was hoping Master-detail provided a way to create an Account by copying an existing account, i.e. not creating two accounts with the same id but assigning an account to an existing Id so I can access that accounts records without explicitly having to query for it.   The  statement I used created an Account but it had all null fields and I'm pretty sure it had a different id but I didn't verify that.

 

HariDinesh, I am using a Person Account (B-to-C) model for Accounts so there is a PersonEmail field on the Account but that detail doesn't really matter for this problem, I could be trying to access any field that is set on the existing account.  

 

This solution looks like something that I'm looking for by for an Account:

 

myObject__c my = new myObject__c(name='mynew',myObject.Account_ID__c=acc.id);