You need to sign in to do that
Don't have an account?
bela
can any one help to solve this trigger scinario
Hi,
I have a scenario where on account we have a field called primarychild text box and in contacts of account we have ischild checkbox field,if ischild is checked for one contact and the ischild should be false for remaining contacts of that account and checked contact lastname should be displayed on account primarychild text box.
I have tried but I am not able solve it.
As i am new to coding I can able to analyse but not able to implement .here is my code where i tried to get record of contacts based on account id and for every account I am checking its contacts and if ischild is updated to true old records should be false.
trigger checkonecontact on Contact (before update) {
Set<Id> s=new Set<Id>();
for(contact c:trigger.old){
s.add(c.accountId);
}
Map<id,contact> cmap=new Map <id,contact>([select name,childeligible__c,account.accountchild__c from contact where accountId in:s]);
for(account a:[select name,accountchild__c from account where id in:s]){
Contact obj = cmap.get(c.id);
}
}
I have a scenario where on account we have a field called primarychild text box and in contacts of account we have ischild checkbox field,if ischild is checked for one contact and the ischild should be false for remaining contacts of that account and checked contact lastname should be displayed on account primarychild text box.
I have tried but I am not able solve it.
As i am new to coding I can able to analyse but not able to implement .here is my code where i tried to get record of contacts based on account id and for every account I am checking its contacts and if ischild is updated to true old records should be false.
trigger checkonecontact on Contact (before update) {
Set<Id> s=new Set<Id>();
for(contact c:trigger.old){
s.add(c.accountId);
}
Map<id,contact> cmap=new Map <id,contact>([select name,childeligible__c,account.accountchild__c from contact where accountId in:s]);
for(account a:[select name,accountchild__c from account where id in:s]){
Contact obj = cmap.get(c.id);
}
}
Thanks
AMit Chaudhary
All Answers
Thanks
AMit Chaudhary
Thanks for the reply,it is working fine,but i have some questions can u help to understand the code.
1) In this condition,where we are checking whether c.childeligible__c is true or false and why we are taking two set of id's,we can take only account id ?
if(c.childeligible__c) // add check box name here
{
system.debug('childeligible__c111111111****************'+c.childeligible__c);
AccID.add(c.accountId);
setContId.add(c.id);
}
2)in debug logs I am getting two results for one update of record one showing true one showing false.below is the log
for(contact c : trigger.new )
{
if(c.childeligible__c) // add check box name here
{
system.debug('childeligible__c111111111****************'+c.childeligible__c);
AccID.add(c.accountId);
setContId.add(c.id);
}
}
for(contact c : trigger.new )
{
if(c.childeligible__c)
system.debug('childeligible__c2222222222222222****************'+c.childeligible__c);
{
system.debug('childeligible__c3333333333333333333333****************'+c.childeligible__c);
if(mapAccount.containsKey( c.accountId ) )
{
Thanks,
Mahanandeesh
1) In this condition,where we are checking whether c.childeligible__c is true or false and why we are taking two set of id's,we can take only account id ?
if(c.childeligible__c) // Amit:- I added This check because we need to update contact only is childeligible__c true on contact
{
system.debug('childeligible__c111111111****************'+c.childeligible__c);
AccID.add(c.accountId); // Amit:- This set is used to get AccountID
setContId.add(c.id); // Amit :- This set is used to get contact id because we dnt need to update this contact
}
I am usinge both set in below Query
17 Map<Id,Account> mapAccount = new Map<Id,Account>([select id,accountchild__c ,(select ischild__C from contacts where id not in :setContId) from account where id in :AccID ]);
2) in debug logs I am getting two results for one update of record one showing true one showing false.below is the log
First Time you are getting true for parent record. But when you are updating child record then you are getting false for other related contact.
Due to below code
35 for(Contact cont : lstContact)
36 {
37 // do changes on contact here
38 cont.ischild__C = false; // Child record false you are stting
39 lstContToUpdate.add(cont);
40 }
I hope this will help you
please also explain for below questions
1)will checkbox be true by default?
2)how and where you are comparing old records with new updated record to make the childeligible__c of other related contacts(old records ) false and updating accountchild__c on account
can you please explain
if(c.childeligible__c) //if childeligible__c is true only enter in to this condition right? but if false also it is entering why?
system.debug('childeligible__c2222222222222222****************'+c.childeligible__c);
{
system.debug('childeligible__c3333333333333333333333****************'+c.childeligible__c);
if(mapAccount.containsKey( c.accountId ) )
{
Account acc = mapAccount.get(c.accountId);
acc.accountchild__c =c.lastname;
List<Contact> lstContact = acc.contacts;
for(Contact cont : lstContact)
{
// do changes on contact here
cont.childeligible__c= false;
lstContToUpdate.add(cont);
}
Thanks,
B.Mahanandeesh
Amit:- No if you did not mark True as Default on field level
2) No. Code will come under only if condition will true. Update your code like below
System.debug('childeligible__c*** Before check****************'+c.childeligible__c); // This should be outside
if(c.childeligible__c) //if childeligible__c is true only enter in to this condition right? but if false also it is entering why?
{
system.debug('childeligible__c***After check**inside if**************'+c.childeligible__c); // this will print only if checkbox is true
if(mapAccount.containsKey( c.accountId ) )
{
Account acc = mapAccount.get(c.accountId);
acc.accountchild__c =c.lastname;
List<Contact> lstContact = acc.contacts;
for(Contact cont : lstContact)
{
// do changes on contact here
cont.childeligible__c= false;
lstContToUpdate.add(cont);
}
Got understood but little basic coding doubt at below if condition can you please explain.you did not mention if(c.childeligible__c==true) than how it is checking it is true.
Is if condition will automatically take value if the checkbox is true by default and if false it will not take in to if condition event if we dont mention c.childeligible__c==true or c.childeligible__c==false?
if(c.childeligible__c) // Amit:- I added This check because we need to update contact only is childeligible__c true on contact.
Thanks,
Mahanandeesh.