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
AbAb 

Trigger | Populating Id of currently Inserted or Updated Contact

Hello,

 
trigger triggerTest on Contact (after insert, after update) {
    Set<Id> contactIds = new Set<Id>();
    
    for (Contact obj : Trigger.new) {
        contactIds.add(obj.Id);
    }
    
    contactIds.remove(null);
    System.debug(contactIds);//This line gives me 6 repeated ids for contact, I want to get only one
what is the reason

 
Best Answer chosen by Ab
AshlekhAshlekh
Hi,

You need to create a class and define a statck Property.

For example.
Public class ConstantClass{
public static Boolean isContactTriggerFire = false;
}
In your trigger 
 
trigger triggerTest on Contact (after insert, after update) {
   if(!ConstantClass.isContactTriggerFire)
   {
      ConstantClass.isContactTriggerFire = true;
      //Your code
   }
}

-Thanks
Ashlekh Gera
 

All Answers

Sindhu1234Sindhu1234
After saving this trigger, check how many times your contact record gets updated. If you manullay update the contact or create the contact that will be count one and also check if any workflow or other code working the contact and making them to update.
AshlekhAshlekh
Hi Sandrine,
 
trigger triggerTest on Contact (after insert, after update) {
    Set<Id> contactIds = new Set<Id>();
    

*************instead of this loop*******************
    for (Contact obj : Trigger.new) {
        contactIds.add(obj.Id);
    }

**************you can do ****************************

system.debug('**********'+Trigger.new.size())

contactIds = Trigger.newMAP.keySet(); // This will give you the id of the all contact which are updated.

system.debug('**********'+contactIds .size())

contactIds.remove(null);
System.debug(contactIds);

And you need to check when you update or insert the contact, there is any other logic in code which update the other contacts or not.

-Thanks
Ashlekh Gera
AbAb

I think that, the trigger is recursively calling itself as in the end of the trigger, i update a field on the same contact.

How can i make the trigger execute only onces and not for future updates 

AshlekhAshlekh
Hi,

You need to create a class and define a statck Property.

For example.
Public class ConstantClass{
public static Boolean isContactTriggerFire = false;
}
In your trigger 
 
trigger triggerTest on Contact (after insert, after update) {
   if(!ConstantClass.isContactTriggerFire)
   {
      ConstantClass.isContactTriggerFire = true;
      //Your code
   }
}

-Thanks
Ashlekh Gera
 
This was selected as the best answer
AbAb

Hi AKG,

what can be test class for 
 

Public class ConstantClass{
public static Boolean isContactTriggerFire = false;
}
AshlekhAshlekh
You just need to wirte a test class for your trigger. When your test class cover the code of trigger this class will covered itself.

-Thanks
Ashlekh Gera