You need to sign in to do that
Don't have an account?
sreeda4011.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?
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?
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
** 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.