Skip to main content

Open Facebook Messenger | Open Chat Window | Send Message using Lightning Component Salesforce

APEX Trigger and Test Class to Prevent Account Deletion | #Salesforce Tutorials



In this article you will learn to create a Before Delete Trigger and Test Class to prevent Account deletion based on a field's value condition.

Please follow below code : 

Trigger
1
2
3
4
5
6
7
8
trigger beforeAccDeletion on Account (before delete) {
    for(Account acc : trigger.old){
        if(acc.myField__c != null){
            acc.adderror('Can not delete the Account if myField is available');
        }
    }

}

Test Class
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
@isTest
private class beforeAccDeletionTest {
    static testmethod void testAccDelete(){
        Account acc = new Account();
        acc.Name='Acc1';
        acc.myField__c='AnyValue';
        insert acc;
        try{
            delete acc;
        }catch(DMLexception e){
            system.assert(e.getMessage().contains('Can not delete the Account if myField is available'),'Can not delete the Account if myField is available');                       
        }
    }
}

Check other posts as well !


If you have any question please leave a comment below.
If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 


Comments

Popular posts from this blog

How to send WhatsApp from Lightning Component Salesforce ?

Send WhatsApp from Lightning Component SalesforceHi folks, in this article I will explain how you can send WhatsApp from Lightning Component using https://wa.me/
In this example I will create a Quick Action button on Contact which will be connected with my Lightning Component.
Please follow the steps below : 
Step 1 : Create a Lightning Component & Apex as shown below
WhatsApp.cmp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30<aura:componentcontroller="Vlog_whatsappCTRL"implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"access="global"><aura:attributename="recordId"type="String"/><aura:attributename="con"type="Contact"/><aura:attributename="msg"type="String"/><aura:handlername="init"action…

Send WhatsApp and Save Chat using Lightning Component Salesforce | Salesforce Tutorials

Hello folks,
In this article you will learn to send WhatsApp and Save the Chat using Lightning Component Salesforce. 
Prerequisite : 
Create a custom object WhatsApp__c
Create custom fields as shown below : 


Please follow the code below : 

WhatsAppComponent 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30<aura:componentcontroller="Vlog_whatsappCTRL"implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"access="global"><aura:attributename="recordId"type="String"/><aura:attributename="con"type="Contact"/><aura:attributename="msg"type="String"/><aura:handlername="init"action="{!c.doInit}"value="{!this}"/><articleclass="slds-card"><divclass="slds-card__header…

Set selected value dynamically in Picklist/lightning:select in lightning component.

How to set selected value dynamically in lightning select in salesforce lightning component ? If you are developing something in lightning component then you must have face the situation to store selected value in your picklist.
Sometimes it works directly by setting the value in your lightning select. But if you are using a dynamic list and Key Value pair with an iteration to set the values, in that case setting value directly will not work.
We have to do some extra efforts with setting the value from controller. 
Please check the example below : 
In this example I am having a picklist of months and I want to change the selected value dynamically.
Component

1 2 3 4 5<lightning:select value="{!v.selectedMonth}" aura:id="selectMonth" label="Select Month" onchange="{!c.changeMonth}"><aura:iteration items="{!v.picklistValues}"var="item" indexVar="index"><option value="{!item.key}">{!item.valu…