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
du-scodouglasdu-scodouglas 

Selecting Objects

Hi,

 

In a after updating trigger, how do I select objects within the updating object:

 

example

 

an object has a child object which has a name, I want to a text field of the parent object using that name

 

How would I access this in APEX?

 

Thanks,

Scott

Best Answer chosen by Admin (Salesforce Developers) 
PrashPrash

Add before for{} statement,

 

List<contact>tenantContact = [SELECT c.Id, c.name
                             FROM contact c
                             WHERE c.Id=Trigger.new[0].Tenant__c LIMIT 1];

 

Replace your statement with,

u.Current_Tenants__c = tenantContact[0].Name;

 

 

Let me know if that works..

All Answers

PrashPrash

Scott,

 

Not sure I understand your question entirely but do you want to update a text field on the parent object based on the name of child object?

 

If so, you can create an AFTER INSERT trigger on child object. In which select the parentTextField of parent record and update it as exaple below.

 

example:

parentTextField = ExistingParentTextFieldValue + 'separator' + NameOfChild;

 

You will also need a trigger on delete of child to remove the Name of Child from the parentTextField. Hope this helps in someway.

 

cheers,

Pr@sh

du-scodouglasdu-scodouglas

Let me give you more detail:

 

We have two custom junction objects Unit and Tenants that paralell with Accounts and Contacts respectively.

 

The trigger I am trying to write should, whenever a Tenant is inserted or updated to a Unit, there is a field that belongs to the unit called "Current Tenants" that I want to update using the new or modified Tenants Name.

 

How do I do this?

 

Scott

PrashPrash

You will have to do something like this,

 

  List<Unit>updateUnit = [Select Id, Current_Tenant__c from Unit where Id=:Trigger.new.Unit__c];
 
  updateUnit.Current_Tenant__c = Trigger.new.Tenant__c;
 
  update updateUnit;

 

There may be some syntax error as I just typed this here.. Let me know if still any issues or questions...

du-scodouglasdu-scodouglas

This is my source code atm, but I am getting an invalid foreign key relationship error: Unit__c.Tenant__c

 

trigger currentTenantNameUpdate on Unit__c (after update, after insert) {

  List<Unit__c>updateUnit = [SELECT Id, Current_Tenants__c
                             FROM Unit__c
                             WHERE Id IN:Trigger.newMap.keySet()];
 
  updateUnit[0].Current_Tenants__c = Trigger.new[0].Tenant__c.Name;
 
  update updateUnit;                                  
}

 

 

the error is coming from this line
  updateUnit[0].Current_Tenants__c = Trigger.new[0].Tenant__c.Name;

the second half...

 

What's wrong?

PrashPrash

Try the following,

updateUnit[0].Current_Tenants__c = Trigger.new[0].Name;

PrashPrash

You may also need to change your WHERE clause in the SELECT statement. As Unit field is referenced in Tenant object.

 

  List<Unit__c>updateUnit = [SELECT Id, Current_Tenants__c
                             FROM Unit__c
                             WHERE Id IN:Trigger[0].unit__c];

du-scodouglasdu-scodouglas

Wow, ok that helped alot. I have managed to get the trigger to fire when I want it, and it's getting the right objects.. now I am just having trouble getting to the name field within the contact, which is itself inside the Tenant_Junction object, here is the code:

 

trigger TenantNameUpdate on Tenant_Junction__c (after update, after insert) {

  List<Unit__c>updateUnit = [SELECT u.Id, u.Current_Tenants__c
                             FROM Unit__c u
                             WHERE u.Id=:Trigger.new[0].Unit__c];
 
  for(Unit__c u : updateUnit)
  {
      u.Current_Tenants__c = Trigger.new[0].Tenant__c.Name;
  }
 
  update updateUnit;
}

 

and the error is Error: Compile Error: Invalid foreign key relationship: Tenant_Junction__c.Tenant__c at line 13 column 45

 

The weird thing is I can access properties of Tenant__c such as Tenant__c.Status__c and that will execute fine, it's just accessing the properties that are part of the contact, not part of the Tenant itself...

 

:( Sorry for all the questions it's my first day on the job. Let's hope we can figure this out!

PrashPrash

Add before for{} statement,

 

List<contact>tenantContact = [SELECT c.Id, c.name
                             FROM contact c
                             WHERE c.Id=Trigger.new[0].Tenant__c LIMIT 1];

 

Replace your statement with,

u.Current_Tenants__c = tenantContact[0].Name;

 

 

Let me know if that works..

This was selected as the best answer
du-scodouglasdu-scodouglas

Gah, same error, different place... I wonder why it's unhappy with the relation...

 

trigger TenantNameUpdate on Tenant_Junction__c (after update, after insert) {

  if (Trigger.new[0].Status__c == 'Former Tenant'
      || Trigger.new[0].Status__c == 'Future Tenant')
  {
      return;
  }
 
  if (Trigger.new[0].Primary_Tenant__c == false)
  {
      return;
  }

  List<Unit__c>updateUnit = [SELECT u.Id, u.Current_Tenants__c
                             FROM Unit__c u
                             WHERE u.Id=:Trigger.new[0].Unit__c];
                            
  List<contact>tenantContact = [SELECT c.Id, c.name
                                FROM contact c
                                WHERE c.Id=:Trigger.new[0].Tenant__c LIMIT 1];
 
  for(Unit__c u : updateUnit)
  {
      u.Current_Tenants__c = Trigger.new[0].Tenant__c.FirstName;
  }
 
  update updateUnit;
}

 

Error: Compile Error: Invalid foreign key relationship: Tenant_Junction__c.Tenant__c at line 29 column 45

 

 

I tried using Trigger.new[0].Tenant__r, but then the error changes to:

 

Error: Compile Error: Invalid bind expression type of SOBJECT:Contact for column of type Id at line 25 column 60

 

 

Hmmm...

du-scodouglasdu-scodouglas

Oop!! It worked, was just a syntax error hehe, lowercase on the .name :)

 

Thanks a million Prash!!

 

Edit: for future reference, can you explain to me what the problem was? and how we solved it? how come I can't directly access the contact within Tenant?

PrashPrash

In Apex to use data value from some object, you gotta query it first. At least that's my understanding. :-)

 

You may want to mark the post as solved/completed.