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
sfadm sfadmsfadm sfadm 

Custom Object how to get actual query data?

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?
Pranav ChitransPranav Chitrans
HI,

what is your trigger context, please make sure it after update if you want the updated data not the previous one. There is few other possibilites of the issue your are facing through. you can past the code here.

Thanks
GhanshyamChoudhariGhanshyamChoudhari
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;
}