0

Object A has Lookup to Account. Object B has Lookup to Account.

I need to fetch details from Object A to Object B .

Is that possible?

P.S I am pretty new to SFDC. Please excuse if this is a simple question.

1
  • 2
    The nature of the lookup relationship means that if Object A has a lookup to Account then there may be zero, one or many of object A related to that account. So from a record of object B you can determine the account but you cannot know whether there will be any matching Object A or many. You can implement some logic in code. You need to consider what you are doing with this information. For example, do you just want to show a list of Object A on the page of Object B? Or do you want values in Object A to affect values on Object B?
    – Doug B
    May 4, 2016 at 14:40

3 Answers 3

1

There is no easy way to achieve what you described, one way of doing it is through apex in either an apex class, or a trigger.

In the example below I am getting all the contacts and opportunities related to an account. Then I loop through the records and I assign a field from the Opportunities to all the Contacts.

This is a naive implementation and should only be used as a guidance in understanding what logical steps you need to take.

Typically you have alot more than 1 Opportunity and 1 Contact attached to an Account, hence you need to be careful which fields you are fetching and how you assign them .

List<Account> accounts = new list<Account>([Select id, name,(Select id,name, FieldA__c from Contacts) ,(Select id, name,FieldB__c from Opportunities ) from Account];

List<Contact> contactstoupdate = new List<Contact>();

for(Account a: accounts)
{

  String field_from_Opp='';

  for(Opportunity o: a.Opportunities
  {
    if(o.FieldA__c!=null)
    field_from_Opp=o.FieldA__c;
  }

  for(Contact c: a.Contacts)
  {
   if(cFieldB__c!=null)
    {
      c.FieldB__c=field_from_Opp;
      contactstoupdate.add(c);
     }
  }


 update contactstoupdate;
}
2
  • Marc -- you might want to fix the typos Filed instead of Field ; Happens to me all the time :-)
    – cropredy
    May 4, 2016 at 15:29
  • @cropredy thanks for spotting that out, I guess I need more coffee today :¬) May 4, 2016 at 15:36
0

Why don't you add objectA lookup field n objectB, then you can access the fields from objectA on objectB very easily using formula fields.

2
  • Yes you could do that, but that would mean a manual assignment for each record of type A to a record of type B, and I don't think this is what he was looking for. May 4, 2016 at 15:40
  • Will try out and update what worked.
    – SFDCRookie
    May 5, 2016 at 10:21
0

If you're ok with a lookup relationship solution you can add a lookup to Object A on Object B. Have your end user select the value of the lookup. Create fields on Object B where you want to pull in details from the Object A record. Create a workflow or process builder that populates those fields when the Object A lookup is populated or edited. Would this solve your problem?

You must log in to answer this question.

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