Monday, 17 October 2016

Trigger to Delete All child records when parent record is deleted.

Deleting all child records when parent record is deleted.

Code:

trigger TriggerTo_Delete_ChildRecords on Account (before delete) {
    
    //To store parent ids
    list<id> AccountIds=new list<id>();
    for(Account accountVar:trigger.old)
    {
        AccountIds.add(accountVar.id);
    }  
    //Collecting all child records related to Parent records
    list<contact> listOfContacts=[select id from Contact where accountid in :AccountIds];
    system.debug('listOfContacts'+listOfContacts);
    //deleting child records
    delete listOfContacts;
}

Friday, 14 October 2016

How to Delete all scheduled Jobs in one go


To delete All the scheduled jobs in one Go, need to follow few steps.

Step 1: Login to your org.

Step 2: Open Developer Console.

Step 3: Copy and Paste the below code.

Step 4: Execute the code.


Code:

List<CronTrigger> cronstodelete = [Select Id from CronTrigger Limit 100];
for(CronTrigger CT: cronstodelete){
    System.abortjob(CT.Id);      
}

Wednesday, 20 July 2016

How to create Custom setting and retrieve the records from Custom setting object and its feilds.


Step 1: Create New Object in Custom setting.
Step 2: create new custom Fields what you require.
Step 3: Create records in Custom setting object in "Manage"

use the following code in the apex classes to get values

  list<(custom object api)> trtd = (custom object api).getall().values();

for Example:

list<US_Holidays__c> trtd = US_Holidays__c.getall().values();

Tuesday, 19 July 2016

How to avoid Recursive trigger

DescriptionMany Developers face this issue because of recursive trigger, or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.
ResolutionIn order to avoid the situation of recursive call, make sure your trigger is getting executed only one time. To do so, you can create a class with a static boolean variable with default value true.

In the trigger, before executing your code keep a check that the variable is true or not.

Once you check make the variable false.



Class code :

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


Trigger code :

trigger updateTrigger on anyObject(after update) {

    if(checkRecursive.runOnce())
    {
    //write your code here          
    }

}



Reference Link: https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US