+ Start a Discussion
KaityKaity 

Is this Trigger Bulkified?

Hi,

 

Is this trigger is bulkified? I have taken this trigger from one blogg???? Here, we  have not use any list or set....

 

 

trigger preventDeletion On Account(before delete){
for(Contact c :[SELECT Id, AccountId From Contact where AccountId In: Trigger.oldmap.keyset() ]){
Trigger.oldMap.get(c.AccountId).addError('Cannot delete Account with a Contact');
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Yes this code is bulkified and bulk safe

 

Since you are concerned about using set I am just adding some comments for you

 

trigger preventDeletion On Account(before delete) {
    for (Contact c: [SELECT Id, AccountId From Contact where AccountId In: Trigger.oldmap.keyset()]) {
        Trigger.oldMap.get(c.AccountId).addError('Cannot delete Account with a Contact');
    }
}

 

[SELECT Id, AccountId From Contact where AccountId In: Trigger.oldmap.keyset()] >> This returns a LIST

 

Trigger.oldMap >> this is a MAP

 

 

  Trigger.oldmap.keyset()  >> This gives you a SET

All Answers

Avidev9Avidev9

Yes this code is bulkified and bulk safe

 

Since you are concerned about using set I am just adding some comments for you

 

trigger preventDeletion On Account(before delete) {
    for (Contact c: [SELECT Id, AccountId From Contact where AccountId In: Trigger.oldmap.keyset()]) {
        Trigger.oldMap.get(c.AccountId).addError('Cannot delete Account with a Contact');
    }
}

 

[SELECT Id, AccountId From Contact where AccountId In: Trigger.oldmap.keyset()] >> This returns a LIST

 

Trigger.oldMap >> this is a MAP

 

 

  Trigger.oldmap.keyset()  >> This gives you a SET
This was selected as the best answer
KaityKaity

I hit kudos , not because you answered me but because you show true mentorship and your strength in APEX.  Superb explanation.

 

Need and expect much more such guidance. Thank you.

 

-Kaity

KaityKaity

Hi Avi,

The code is functionally working fine, but from UI, it looks very odd/ugly. Can't we bring the error message in Detail page of Account(like in VF page) ??

 

 

Avidev9Avidev9

Detail Page ? Are you looking something like "Page Messages" ?
Well probably not.

 

But you can send in your own html from the backend.

 

Trigger.oldMap.get(c.AccountId).addError('<b>Cannot delete Account with a Contact</b>', false);

 

 

KaityKaity

There are 2 questions:

 

How to make the error comment to show in red font.

Why did we use False?? (Is it act like return type???)

 

 

KaityKaity

First one is good:

 

 Trigger.oldMap.get(c.AccountId).addError('<b><font color="red"/><font>Cannot delete Account with a Contact</b>', false);

 

Donot know the second  query??

Avidev9Avidev9

Well the false is to specify whether to escape the html tags or not. If you specify the second param as false, html wont be escaped and browser will actually render the HTML content.

 

So if you are adding some html tags in your error message and want the same to render, you have to switch this flag to false.