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
sreeda4011.3971223684430222E12sreeda4011.3971223684430222E12 

how to update Cross Object fields though trigger?

Hi, i have 2 objects with the fields

OBJECT1__C  --  Checkbox1
OBJECT2__C  --  Checkbox2.  i have an junction object bewteen theser two. NOW i need to update Checkbox2 If Checkbox1 is checked. Please any help?
praveen murugesanpraveen murugesan
Hello sree,

If I understood your question correctly.

You should write the trigger on parent object that should have query to select all the child records.

If your query is not null you can update like this Checkbox1 = True.

Eg.

trigger test on OBJECT1__C (after insert) {
   set<id> idval = new set<id>();
    for(object__c ben : Trigger.New){
           if(ben.Checkbox1 = true){
                  idval.add(ben.id);                                 //adding id value to query child records
          }
   }
   if(!idval.isEmpty())
  {
       list<childobj> tet = [select id,Checkbox2 from OBJECT2__C where OBJECT1__C IN:idval ];
  }
for(childobj t : tet)
{
  t.Checkbox2 =true;
}
  update tet;   //updating child values.
}

I hope this will helpful.


Thanks.

Praveen Murugesan
Ramu_SFDCRamu_SFDC
Here you go mate.

** Juncobj__c is a junction object
**Account is one of the parent objects
**Travel__c is another parent object

the below code works something like this. On setting the checkbox2__c to true on travel__c the related account objects to the common junction object will have the field checkbox__1 chekced .


trigger travelcheckbox on travel__c (after insert,after update) {
set<id> ids=new set<id>();
    for(travel__c travel:trigger.new){
        if(travel.Checkbox2__c==true){
            ids.add(travel.Id);
        }  
    }
    list<account> acctstoupdate=new list<account>();
    if(ids.size()>0){
    list<juncobj__c> juncobjslist=new list<juncobj__c>([select id,Account__c from juncobj__c where travel__c=:ids]);
    set<id> acctids=new set<id>();
        if(juncobjslist.size()>0){
    for(juncobj__c junc:juncobjslist){
        acctids.add(junc.Account__c);
    }
    if(acctids.size()>0){
    list<account> accounts=new list<account>([select id,Checkbox1__c from account where id=:acctids]);
    for(account accts:accounts){
        accts.Checkbox1__c=true;
        acctstoupdate.add(accts);
    }
    }      
    }
       
}
    update acctstoupdate;
}

Please mark this as a solution if this resolved your requirment.
sreeda4011.3971223684430222E12sreeda4011.3971223684430222E12
Thank you it is working fine
praveen murugesanpraveen murugesan
good