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
DeepuDeepu 

Dynamic Trigger

Hello All

 

I have a requirement to create triggers dynamically using apex code.(and not through the setup option )

Is this possible?

 

Thanks in advance

Ankit AroraAnkit Arora

What do you exactly mean by dynamic triggers? Can you please explain bit more?

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

DeepuDeepu

I have a user interface where user can select 2 objects.(say Account and Contact)

As and when an update happens in Account, Contact should also get updated.

The object changes based on user selection.

 

Thanks

 

 

Ankit AroraAnkit Arora

You need to do this using the apex controller. Apply logic in controller where you will fetch both object names using describe and on one update other will get get update.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

DeepuDeepu

apex controller code wont work here.

I want Contact to be updated each time an Account gets updated

Shashikant SharmaShashikant Sharma

No such option available. You can not create triggers using Apex.

Ankit AroraAnkit Arora

Am still not able to understand why apex controller won't work here. If you put condition that if account is selected then update contact. Is there any specific requirement which I am missing?

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

DeepuDeepu

Please give me a sample code which does this.

 

Each time an account is updated, update contact

 

I am not able to find any solution other than triggers

Shashikant SharmaShashikant Sharma

I understand this requiremnt as to create a Apex Trigger on any object from  Apex Code. Thast why I said it is not possible. If I misunderstood this please let me know.

Ankit AroraAnkit Arora

Let me make it very clear now. You can't create dynamic trigger that's true and exactly what I said. But you are providing object name from UI Lets say Account that means you are updating account object and applying this login in your controller you can update all contacts related to that account :

 

Account acc = [select id , name from account limit 1] ;
List<Contact> conLst = new List<Contact>() ;
conLst = [select id , lastName from Contact where AccountId =: acc.Id] ;
for(Contact c : conLst)
{
      c.lastName = acc.name ;
}
update conLst ;

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Ankit AroraAnkit Arora

Deepu, can you please explain me why the workaround will not work for you?

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

DeepuDeepu

I am tryning to solve a discripency in existing salesforce workflow.

 

My aim :

to create relationships between objects so that workflow rules that are “cross-object dependent” get updated without the need for users to edit both records.

For example, a workflow field update to a field on the Contact object must be triggered by a workflow rule on the Contact object.
Without this, if the conditions that trigger the workflow are on the Account object, when the Account record is edited to meet the conditions, the workflow on the Contact will not get evaluated to trigger the field update until a user manually edits the Contact record also.

 

The applications stack we develop may vary. I need a standard solution to apply. I can not go and edit a controller each time a requirement like this comes.

JayantJayant

Write a trigger on Account only, fetch all related contacts using a relationship query and then loop through all child records to check whether the condition for field update is met and modify the field accordingly.

 

e.g.

List <Account> lstAcc = new List <Account> ();

List <Contact> lstCon = new List <Contact> ();

lstAcc = [SELECT Id, Name, (SELECT FirstName, LastName FROM Contacts) FROM Account where Id in: trigger.newMap.keyset()]; //returns all Accounts which invoked the trigger and related Contacts

 

for (Account a : lstAcc)  //iterate through all accounts

{

          for (Contact c : a.Contacts)   //iterate through all Contacts related to Account a

          {

                   Your logic here;

                    lstCon.add(c);         //after doing necessary modifications to the contact record, add it to a list

          }

}

 

if (lstCon.size() > 0)

Database.Update(lstCon, False);    

 

/* If lstCon does have any contacts, update the same in database. Using False in Database.Update would not revert any records already updated before update fails for any contact record, not using it would revert the modifications even for records which were successfully updated before the operation failed for any record */

 

JayantJayant

If you just want to update all related Contacts for the sake of running all your workflows on Contact object, create a dummy number field on Contact object, increment it and fire the update.

 

e.g.

 

for (Account a : lstAcc)  //iterate through all accounts

{

          for (Contact c : a.Contacts)   //iterate through all Contacts related to Account a

          {

                    c.dummy__c = c.dummy__c + 1;

                    lstCon.add(c);         //after doing necessary modifications to the contact record, add it to a list

          }

}

 

Database.Update(lstCon, False);

 

And you're done.



Sforce.NinjaSforce.Ninja

Deepu, I am working on something similar. Did you get any solution for it yet? 

sai007sai007
Deepu, I am working on something similar. Did you get any solution for it yet? 
sai007sai007
Hi Sforce.Ninja 
I am working on something similar. Did you get any solution for it yet?