Tuesday, 20 March 2018

Trigger to Avoid Duplicate Combination of Parents in Junction Object

This trigger will help you to avoid duplicate entries on the Junction object with the same set of parent records.

For Ex: Let's consider we have three objects Parent1, Parent2, and Child1(Junction Object between Parent1 and Parent2).



  • We have inserted a record in Junction Object (CH1) with values as a parent1 record (P1) and Parent2 Record (P2). 
  • If we try to enter another Child record with again same parent combination i.e., P1 and P2 then the below trigger throw an error message saying duplicates not allowed.

Trigger:


 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
trigger AvoidDuplicateOnJunctionObject on Child1__c (before insert,before Update) {
 
    set<Id> Parent1Set = new set<Id>();
    set<Id> Parent2Set = new set<Id>();

    Set<Id> AvailableParent1Set = new set<Id>();
    set<Id> AvailableParent2Set = new set<Id>();   
    
    for(Child1__c vp : trigger.new){
        Parent1Set.add(vp.Parent1__c);
        Parent2Set.add(vp.Parent2__c);
    }
    
    
    for(Child1__c vp : [Select id, Parent1__c, Parent2__c from Child1__c WHERE Parent1__c IN: Parent1Set AND Parent2__c IN: Parent2Set]){
        AvailableParent1Set.add(vp.Parent1__c);
        AvailableParent2Set.add(vp.Parent2__c);        
    }
    
    for(Child1__c vp : trigger.new){
        if(AvailableParent1Set.contains(vp.Parent1__c) && AvailableParent2Set.contains(vp.Parent2__c)){
            vp.adderror('Duplicate Combination Values are Not Allowed');
        }        
    }
}

Monday, 5 March 2018

Trigger to Update the Child record with Parent field data - Using Map(only on insert)

This trigger is to show the use of Map in apex. Here we are updating the contact's type field with the corresponding parent(Account)'s name.

for ex:
       if Account name = 'Prestige';
          Contact name = 'contact 3'
       then the 'Type' field from the contact will get updated with 
          Contact Type - 'Prestige - Contact'


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
trigger UpdateContactFieldWithAccountUsingMap on Contact (before insert) {

    set<id> Accountids = New set<id>();
    for(contact con : trigger.new){
        Accountids.add(con.AccountId);
    }
    map<Id,Account> IdAccountMap = new map<Id, Account>([select id, name from account where id in : Accountids]);
    for(contact con : trigger.new){
        if(con.accountId!=null && IdAccountMap.containskey(con.accountId)){
            con.Title = IdAccountMap.get(con.AccountId).name + '- Contact';
        }
    }    
}

Friday, 2 March 2018

Aggregate Result Query- Sum, Minimum, Maximum, Average and Count

Below query is on Opportunity object and on Amount Field.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Aggregateresult[] aggresult= [Select COUNT(amount)countt, SUM(amount)sum, MIN(amount)min, MAX(amount)max, AVG(amount)avg from Opportunity];

System.debug('opportunity Count amount******'+aggresult[0].get('countt'));

System.debug('opportunity SUM amount******'+aggresult[0].get('sum'));

System.debug('opportunity MIN amount******'+aggresult[0].get('min'));

System.debug('opportunity MAX amount******'+aggresult[0].get('max'));

System.debug('opportunity Average amount******'+aggresult[0].get('avg'));

Thursday, 1 March 2018

Trigger to calculate the minimum date of Child records and populate on Parent record

Here Child -> Buyers__c

         Parent -> oneplus__c

 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
trigger FirstBuyer on Buyers__c (after Insert, after update, after Delete, after Undelete) {

   set<id>OnePlusIds = new set<Id>();
   
   if(trigger.isInsert || trigger.isUndelete || trigger.isUpdate){
           for(Buyers__c buy : Trigger.new){
               OnePlusIds.add(buy.oneplus__c);
           }
       }    

   if(trigger.isDelete || trigger.isUpdate){   
       for(Buyers__c buy : Trigger.old){
           OnePlusIds.add(buy.oneplus__c);
       } 
   }   
   
   List<OnePlus__c>Parent_List = [Select Id, name, First_Buying_Date__c,  (Select Id, Name,
                                  Buying_date__c From Buyers__r ORDER BY Buying_date__c ASC 
                                  LIMIT 1) From oneplus__c WHERE ID IN: OnePlusIds];
   
   List<OnePlus__c>Update_Parent_List = New List<OnePlus__c>();
   
   for(Oneplus__c oneplus : Parent_List){                         
       oneplus.First_Buying_Date__c =  oneplus.Buyers__r[0].Buying_date__c; 
       Update_Parent_List.add(oneplus);                                                
   }
   
   Update Update_Parent_List;
}