function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PRASHANT BHARTIPRASHANT BHARTI 

Custom Rollup in lookup relationship

Custom roll-up – Keep relation between Position and Job application as lookup. Create a number field on Position object. This field should contain count of its child job applications. Do this using a trigger.
Deepali KulshresthaDeepali Kulshrestha
Hi Prashant
 
Greetings to you!

I have practiced this code in my org.
 
trigger jobapplicationTrigger on job_application__c (after insert,after delete) {
    if((Trigger.isInsert && Trigger.isAfter) || (Trigger.isDelete && Trigger.isAfter)){
        Set<Id> parentIdsSet = new Set<Id>();
        List<Position__c> positionList = new List<Position__c>();
        if(Trigger.isInsert){
            for(job_application__c jobObj : Trigger.new){
                parentIdsSet.add(jobObj.Position__c);
            }
        }else if(Trigger.isDelete)  {
            for(job_application__c jobObj : Trigger.old){
                parentIdsSet.add(jobObj.Position__c);
            }   
        }
        
        System.debug('parentIdsSet-->'+parentIdsSet);
        if(parentIdsSet.size() > 0){
            positionList = [SELECT Id,
                            Name,
                            job_count__c,
                            (SELECT Name
                             FROM job_applications__r)
                            FROM Position__c
                            WHERE Id IN : parentIdsSet];
        }
        System.debug('positionList-->'+positionList);
        if(positionList.size() > 0){
            for(Position__c postionObj : positionList){
                postionObj.job_count__c = postionObj.job_applications__r.size();
            }
            update positionList;
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
PRASHANT BHARTIPRASHANT BHARTI
Hi Deepali,

I tried this code but I am getting an error Position__c doesnot exist.

Thanks & Regards,
Prashant
Sainath VenkatSainath Venkat
Hello Prashant Bharti,

Can you please replace Position__c with the object API Name that was present in your org
PRASHANT BHARTIPRASHANT BHARTI
Hi Sainath,

This is the object API name only.

 
Sainath VenkatSainath Venkat
Prashant Bharti,

Can you please paste the code that you tried in your org and the line where you getting the error.
Sainath VenkatSainath Venkat
Prashant Bharti,

try below code, I assumed number field as Number_of_Applications__c
trigger JobappTrigger on Job_Application__c(after insert, after delete) {
    map<Id, Integer> mapjobappCount = new map<Id, Integer>();
    if(trigger.isInsert) {
        for(Job_Application__c hob : trigger.new) {
            if(hob.Employee__c != null) {
                if(!mapjobappCount .containsKey(hob.Position__c  )) {
                    mapjobappCount .put(hob.Position__c  , 1);
                } else {
                    mapjobappCount .put(hob.Position__c , mapjobappCount.get(hob.Position__c ) + 1);
                }
            }
        }
    } else {
        for(Job_Application__c hob : trigger.old) {
            if(hob.Position__c != null) {
                if(!mapjobappCount .containsKey(hob.Position__c )) {
                    mapjobappCount .put(hob.Position__c , -1);
                } else {
                    mapjobappCount .put(hob.Position__c , mapjobappCount .get(hob.Position__c ) - 1);
                }
            }
        }
    }
    if(mapjobappCount .size() > 0) {
        List<Position__c> listEmp = [SELECT Id, Number_of_Applications__c FROM Position__c  WHERE Id IN : mapjobappCount .keySet()];
        
        for(Position__c  emp : listEmp) {
            emp.Number_of_Applications__c += mapjobappCount .get(emp.Id);
        }
        
        update listEmp;
    }
}

 
Sainath VenkatSainath Venkat
@Prashant Bharti,, CAN you please mark it best if it solves the problem so that it will help others.