+ Start a Discussion

Related objects query.

Hi guys,


I'm a newbee to Apex coding and I'm not long in my new role working on Salesforce admin and development. I'm just wondering if any of you kind people can help me with my following scenario.


I have 2 objects and I'm trying to figure out a way to perform a check (from Object A) on the status of a number of instances of the other object (Object B). Basically what I need to setup is a check that will change the status of Object A to complete, once the statuses of all instances of Object B are marked as complete.


My 2 objects are related through a lookup field that is on Object B, and it is a lookup field for Object A.


I hope my query makes sense. If any of you can provide me with some pointers on how to achieve what I'm looking for I would be very grateful.


Many thanks in advance for your help with this!


I suggest creating a trigger on Object B.


What I suggest is that whenever a record is updated on Object B to complete, run a query to check the status of all Object B records related to Object A. If all Object B records related to Object A is complete, update Object A to complete, else do nothing.


Hi Rustan,


Thanks for your suggestion. I have been having problems trying to get a list of status for all Object B records related to Object A but I haven't been successful. I'm wondering if you would have any sample code to achieve this as I am probably doing something wrong.


Thanks again for your help!

Eugene PozniakEugene Pozniak

Don't know, are you going to use a trigger or any methods from a class, but think that logic will be like shown below:


List<ObjectA__c> A_Objects = [
		SELECT Status_A__c,
			(SELECT Status_B__c
			FROM ObjectsB__r LIMIT 2000)
		FROM ObjectA__c
		WHERE /*Your where condition*/ LIMIT 50000];

for(ObjectA__c entryA : A_Objects) {
	if(entryA.ObjectsB__r != NULL && (!entryA.ObjectsB__r.isEmpty())) {
		Boolean isAllBCompleted = TRUE;
		for(ObjectB__c entryB : entryA.ObjectsB__r) {
			if(entryB.Status_B__c != 'Completed') {
				isAllCompleted = FALSE;
		if(isAllBCompleted) {
			entryA.Status_A__c = 'Completed';

Hope, it will be useful for you.