Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
I have two related custom objects. I'll name them:

"Custom_Object1__c"

and 

"Custom_Object2__c".

In the "Custom_Object2__c" I have a lookup field to "Custom_Object1__c". In "Custom_Object1__c" I have a related list with "Custom_Object2__c".

In "Custom_Object2__c" I also have a checkbox field. When changing the state of the checkbox I need in the "Custom_Object2__c" trigger to get all checked "Custom_Object2__c" records of "Custom_Object1__c".

I've implemented a code in the trigger and I'm trying to get all checked "Custom_Object2__c" records via the following query:[SELECT checkbox__c, Custom_Object2_Status__c, Id FROM Custom_Object1__c where id =:Custom_Object1_ID__c]

The issue I have is that the query returns old data which is before the update of the record.

For instance I have four out of 4 checked Custom_Object2__c records but in the trigger the query returns 3 instead of 4 checked.

How can I get the actual data?
2 answers
  1. Apr 27, 2018, 12:36 PM
    if Custom_Object2__c update then checkbox field will update on Custom_Object1__c description field.

     

    trigger updateaccountfield on Custom_Object2__c (after insert, after update,after delete) {

    List<ID> AccID = New List<ID>();

    for(Custom_Object2__c con : Trigger.new){

    if(con.Custom_Object1__cId!=null&& con.Custom_Object1__cId != null){

    AccID.add(con.Custom_Object1__cId);

    }

    }

    List<Custom_Object2__c> ctwoList = [ select Id, checkbox___C from Custom_Object2__c where accountId in :AccID];

    List<Custom_Object1__c> coneList = [SELECT Id, description FROM Custom_Object1__c WHERE id in :AccID];

    String checkbox;

    for(integer j = 0 ; j < ctwoList.size(); j++){

    if(j==0){

    checkbox = ''+ctwoList[j].get('checkbox___C');

    system.debug('checkbox__c@@@1'+checkbox__c);

    }else{

    checkbox = checkbox__c+''+ctwoList[j].get('checkbox___C');

    system.debug('checkbox__c@@@1'+checkbox__c);

    }

    }

    Custom_Object1__c cone = new Custom_Object1__c();

    cone.Id= coneList[0].Id;

    cone.Description=checkbox;

    update cone;

    }

     
Loading
0/9000