Sunday, 16 September 2018

Trigger for All Rollup Summary calculations on Lookup Relation- Count, Minimum, Maximum, Sum and Average

This trigger will help you to update all the Rollup Summary calculations on Look up RelationShip.

Objects and Fields Information :
Contact (Object) --> Amount__c(Custom Field)
Account (Object) --> Sum__c (Custom Field)
                            --> Avg__c (Custom Field)
                           --> Min__c (Custom Field)
                           --> Max__c (Custom Field)
                           --> Count__c (Custom Field)

Here we are updating all the Account fields based on Contact's Amount__c Field data


 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
31
32
33
34
35
trigger FullRollup on Contact (after insert, after update, after delete, after undelete){
   
    set<Id>Parent_set = new set<id>();
    List<Account>AccUpdateList = New List<Account>();
   
    if(trigger.isdelete || trigger.isupdate){
        for(Contact con : Trigger.old){
            Parent_set.add(con.AccountId);
        }
    }
   
    if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete){
        for(Contact con : Trigger.New){
            Parent_set.add(con.AccountId);
        }
    }
   
   
    for(AggregateResult aggcon : [Select AccountId,
                                      Count(Id)countt, Sum(amount__c)summ, avg(amount__c)avgg, min(amount__c)minn, max(amount__c)maxx
                                      From contact Where AccountId IN :Parent_set Group By AccountId]){
        Account Acc = New Account();
        Acc.Id     = (Id)aggcon.get('AccountId');
        Acc.Sum__c = (Decimal)aggcon.get('summ');
        Acc.Avg__c = (Decimal)aggcon.get('avgg');
        Acc.Min__c = (Decimal)aggcon.get('minn');
        Acc.Max__c = (Decimal)aggcon.get('maxx');
        Acc.Count__c = (Decimal)aggcon.get('countt');  
       
        AccUpdateList.add(Acc);    
       
    }
    Update AccUpdateList;
   
}

Friday, 14 September 2018

Sample Rest Class

 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@RestResource(URLMapping = '/Interaction__c/*')

global with Sharing class MyFirstRestClass{

    @httpGet
    global static interaction__c getInteraction(){
    
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        string interactionId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        interaction__c result = [Select id, name, status__c from interaction__c WHERE ID =:interactionId];
        return result;        
    }
//    /services/data/v34.0/sobjects/interaction__c
    
    @HttpPost
    global static string createInteraction(string Name, String status){
       
        interaction__c inter = new interaction__c();
        inter.Name = Name;
        inter.status__c = status;
        insert inter;
        return inter.name;
    }
    
    @HttpPut
    global static interaction__c upsertInteraction(){
        RestRequest request = RestContext.request;
        string interId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
        interaction__c interact = [Select Id, Name, Status__c from interaction__c WHERE ID =:interId];
        Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());

        for(String fieldName : params.keySet()) {
            interact .put(fieldName, params.get(fieldName));
        }
        upsert interact ;
        return interact;
    
    }
    
    @HttpPatch
    global static string updateInteraction(){
        
        RestRequest request = RestContext.request;
        string interId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
        interaction__c thisInteraction = [Select Id, Name, Status__c from interaction__c WHERE ID =:interId];
        
        Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());

        for(String fieldName : params.keySet()) {
            thisInteraction.put(fieldName, params.get(fieldName));
        }
        update thisInteraction;
        
        return thisInteraction.name;
    }
//    /services/data/v34.0/sobjects/interaction__c/a076F00000qz2w4QAA


    @HttpDelete
    global static string DeleteInteraction() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String interactionId= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        interaction__c inter= [SELECT Id FROM interaction__c WHERE Id = :interactionId];
        delete inter;
        
        return 'Deleted SuccessFully';
    }
    
}

Batch Apex Example to Update Contact Email Id

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
global class ContactEmailUpdateBatch implements Database.Batchable < sObject > {

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = SELECT Id, Email FROM Contact;
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List < Contact > scope) {

        for (Contact a: scope) {
            a.email = a.email + .test;
        }
        update scope;
    }

    global void finish(Database.BatchableContext BC) {}
}


Executing the above batch class from the anonymous window of developer console:



1
2
ContactEmailUpdateBatch CEUB = new ContactEmailUpdateBatch();
Database.Executebatch(CEUB ,1);