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
Richa_LearningRicha_Learning 

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

k_bentsenk_bentsen

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....

 

Richa_LearningRicha_Learning

Is it not possible in this trigger?

k_bentsenk_bentsen

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.

Richa_LearningRicha_Learning

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.

 

vbsvbs
@Richa - Any reason why you have not considered cross-object formulas for the fields in IMP__c? All I see is you setting up IMP__c using account fields and then replicating them during an update. Any reason for this duplication of fields? Just trying to get a different perspective in as we do not have the whole business case for the request with us to set the context...
Richa_LearningRicha_Learning

this is the way it was setup from before. There is some business requirement that's y we are copying values.

Richa_LearningRicha_Learning

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;

    

    }

    }

vbsvbs
If thats the case, can you check what prevents you from just setting this IsClosed field as a formula field, for now? In any case, your logic will need to reflect the value of the Account IsClosed flag on to the IMP__c object for every update.
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.
Richa_LearningRicha_Learning

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.

k_bentsenk_bentsen

I noticed a couple of typos in your edits, not sure if that's your problem or not.