You need to sign in to do that
Don't have an account?
Updating checkbox using Trigger
Hello,
This Trigger is copying some fields from Accoun to IMP__C Custom Object. It's working fine.
There is a field on Account "IsClosed" (standard field) , it's a checkbox. I need to copy the value of this check box to IMP__c. Means when checkbox is true it should update value on IMP__C "closed" field as True. I am not sure how to write this in this trigger?
trigger insertfielddat on Account(after insert,after update) {
Set<String> AccountTitle= new Set<String>();
Set<String> setAccount = new Set<String>();
List<Account> listAccount = new List<Account>();
Map<String, IMP__c> mapImp = new Map<String, IMP__c>();
List<IMP__c> listImp = new List<IMP__c>();
List<IMP__c> listUpdtImp = new List<IMP__c>();
if (Trigger.isInsert){
for (Account Accounts : Trigger.new)
{
IMP__c imp = new IMP__c(Name = Accounts.title,BR_ID__c=Accounts.Account__c,Description__c=Accounts.Body,Region_User__c=Accounts.Region__c);
listImp.add(imp);
}
insert listImp;
}
if (Trigger.isUpdate){
for (Account Accounts : Trigger.new)
{
listAccount .add(Accounts );
setAccount.add(Accounts.Account__c);
}
listImp = [select Name,BR_ID__c,Description__c,Region_User__c from IMP__c where BR_ID__c in :setAccount];
for( IMP__c imptmp : listImp ){
mapImp.put(imptmp.BR_ID__c,imptmp);
}
for( Account Accounttmp : listAccount ){
if( mapImp.get(Accounttmp.Account__c) != null ){
IMP__c uptIMP = mapImp.get(Accounttmp.Account__c);
uptIMP.Description__c=Accounttmp.Body;
uptIMP.Name = Accounttmp.title;
uptIMP.Region_User__c =Accounttmp.Region__c;
listUpdtImp.add(uptIMP);
}else{
IMP__c imp = new IMP__c(Name = Accounttmp.title,BR_ID__c=Accounttmp.Account__c,Description__c=Accounttmp.Body,Region_User__c=Accounttmp.Region__c);
listUpdtImp.add(imp);
}
}
upsert listUpdtImp;
}
}
Please Help!
Richa
I would actually suggest creating a new trigger rather than adding the functionality to the existing one, something like:
trigger UpdateIMPsOnClose Account(after update) etc....
Is it not possible in this trigger?
It is, but you'll have a lot going on, and your trigger starts to get a little messy. IMO, it's better to separate the two behaviors, even though they are closely related.
but I dont need to copy any logic if account is closed or not.
What I need is simply update the Field on IMP__c "closed" to True if Account "Isclosed" is true.
this is the way it was setup from before. There is some business requirement that's y we are copying values.
I tried this way but it's not updating the field
trigger insertfielddat on Account(after insert,after update) {
Set<String> AccountTitle= new Set<String>();
Set<String> setAccount = new Set<String>();
List<Account> listAccount = new List<Account>();
Map<String, IMP__c> mapImp = new Map<String, IMP__c>();
List<IMP__c> listImp = new List<IMP__c>();
List<IMP__c> listUpdtImp = new List<IMP__c>();
if (Trigger.isInsert){
for (Account Accounts : Trigger.new)
{
IMP__c imp = new IMP__c(Name = Accounts.title,BR_ID__c=Accounts.Account__c,Description__c=Accounts.Body,Region_User__c=Accounts.Region__c,closed__c=Accounts.isclosed);
listImp.add(imp);
}
insert listImp;
}
if (Trigger.isUpdate){
for (Account Accounts : Trigger.new)
{
listAccount .add(Accounts );
setAccount.add(Accounts.Account__c);
}
listImp = [select Name,BR_ID__c,Description__c,Region_User__c,closed__C from IMP__c where BR_ID__c in :setAccount];
for( IMP__c imptmp : listImp ){
mapImp.put(imptmp.BR_ID__c,imptmp);
}
for( Account Accounttmp : listAccount ){
if( mapImp.get(Accounttmp.Account__c) != null ){
IMP__c uptIMP = mapImp.get(Accounttmp.Account__c);
uptIMP.Description__c=Accounttmp.Body;
uptIMP.Name = Accounttmp.title;
uptIMP.Region_User__c =Accounttmp.Region__c;
uptIMP.closed__c=accountmo.isclosed;
listUpdtImp.add(uptIMP);
}else{
IMP__c imp = new IMP__c(Name = Accounttmp.title,BR_ID__c=Accounttmp.Account__c,Description__c=Accounttmp.Body,Region_User__c=Accounttmp.Region__c,closed__C=accountmp.isclosed);
listUpdtImp.add(imp);
}
}
upsert listUpdtImp;
}
}
Another comment on the above code, as it is very badly written. You do not have any need for the isUpdate condition nor the isInsert. If you just maintain the logic as-is and instead of an insert change it to upsert in the forst part your code this should work. All you will need then is to include the new field IsClosed in the initialisation of the IMP__c object.
Let me know and I can assist you if required in understanding this a bit better.
Thanks for the comments.
Actually there is no realtion between the Account and IMP__c object i.e. no master details or child relation.That's why was not able to write the Formula field.
The way data is going from Account to IMP__C is by checking the Auto generated number. If that number is same on account and same in IMP__C it updated the value.
I noticed a couple of typos in your edits, not sure if that's your problem or not.