0

So I have the following scenario: I upload some data of object A, and within object A there is a field (field_b__c) which I want to use to lookup another object (B) and get a specific field (target__c) from it

The issue that I'm having is that field_b isn't an Id of B, but a field called Name which I have to use to get B.

What I have is the following:

List<A__c> b_names = [SELECT field_b__c FROM A where Id IN :Trigger.newMap.keySet()];

How do I then use b_names to get all of the relevant B objects without iterating through it and adding them one by one?

List<B__c> targets = [SELECT target__c FROM B where Name IN b_names];

Above is my half-baked idea but it's obviously not working.

One possible solution I had was to create a map so that I only reduce the number of lookups to the number of unique field_b__c's but I'm wondering if there's a better way of doing what I'm trying to do

2

As far as I understood from your description, field_b__c is text field which contains name of B object. It might be ok to use such configuration if name on b is unique.

So you could write some code like following to operate over it:

List<String> bNames = new List<String>();
for ( A__c a: Trigger.new ) {
    bNames.add( a.field_b__c );
}
List<B__c> bs = [SELECT Name, target__c FROM B where Name IN :bNames];
Map<String, B__c> bMap = new Map<String, B__c>();
for ( B__c b: bs ) {
    bMap.put( b.Name, b );
}
for ( A__c a: Trigger.new ) {
    B__c relatedB = bMap.get( a.field_b__c );
    a.target_from_b__c = relatedB.target__c ;
}

In this way you might copy the way from relatedB.target__c to some new custom field target_from_b__c.

However, this might not working as expected is Name on b is not unique.

It is more efficient and reliable to have lookup relationship from a to b. Lookup relationship will contain id. Lookup will simplify the code.

If you have field lookupToB__c on A, you might have simplier code:

List<A__c> arecords = [SELECT target_from_b__c, lookupToB__r.target__c FROM A where Id IN :Trigger.newMap.keySet()];
for ( A__c a: arecords ) {
   a.target_from_b__c = a.lookupToB__r.target__c;
}

Also, if you have lookup, you might create cross-object formula on A, which would simply point to lookupToB__r.target__c and you can use the formula field whenever you need the value from b

7
  • Can I specify a lookup during the data upload process? Would I need to change anything? Right now it's just a text field
    – IanQ
    Nov 15 '17 at 21:13
  • If you had a lookup, there would be no reason to use a trigger to set the value, you would just need a formula like LookupToB__r.Target__c. Nov 15 '17 at 21:21
  • See this question for reference on converting your text fields into lookups (You may need to do a little extra work since you're using names as keys instead of ids). Nov 15 '17 at 21:23
  • @user49458 yes, you can specify a lookup value during data upload process, you need to specify Id value of B object record
    – Patlatus
    Nov 15 '17 at 21:30
  • @battery.cord I know about formula, I just explain to post owner how to operate with lookups
    – Patlatus
    Nov 15 '17 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.