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
Nagoor Shaik 16Nagoor Shaik 16 

trigger concepts

Hi could please solve this query.
i have two custom objects like company and department.If department >5 then display status is low or medium or high so please solve this
Best Answer chosen by Nagoor Shaik 16
Dushyant srivastava 8Dushyant srivastava 8
Hi Nagoor,
This can also be done without triggers by standard salesforce features.
Going by your Example, Company being a parent object and Department its child. We can create one Rollup summary and one Formula Field. Rollup Summary to calculate the number of Departments associated with the Account.
Formula Field to Display the Status according to your conditions 

If you specifically require a trigger then please use the code given below:
 
trigger DepartmentTrigger on Department__c (after insert, after Update) {
	if(Trigger.isAfter && Trigger.isInsert)
    {
        DepartmentTriggerHandler.onAfterInsert(Trigger.new);
    }
	if(Trigger.isAfter && Trigger.isUpdate)
	{
		DepartmentTriggerHandler.onAfterUpdate(Trigger.new , Trigger.oldMap);
	}
}


public class DepartmentTriggerHandler {
	public static void onAfterInsert(List<Department__c> lstDepartment)
    {
        collectCompanyID(lstDepartment);
    }
    
	public static void onAfterUpdate(List<Department__c> lstDepartment , Map<Id, Department__c> mapoldDepartment)
    {
        collectCompanyIDs(lstDepartment, mapoldDepartment);
    }
	
    private static void collectCompanyID(List<Department__c> lstDepartment)
    {
        List<Id> lstCompanyId = new List<Id>();
        
		for(Department__c objDepartment : lstDepartment)
        {
            if(objDepartment.Company__c != Null)
			{
				lstCompanyId.add(objDepartment.Company__c);
			}
        }
		
		if(lstCompanyId.size() > 0)
			changeCompanyStatus(lstCompanyId);
	}
	
	private static void collectCompanyID(List<Department__c> lstOldDepartment , map<Id, Department__c> mapoldDepartment)
	{
		List<Id> lstCompanyId = new List<Id>();
		
		for(Department__c objDepartment : lstOldDepartment)
		{
			if(objDepartment.Company__c != Null && mapoldDepartment.get(objDepartment.Id).Company__c != objDepartment.Company__c)
			{
				lstCompanyId.add(objDepartment.Company__c);
			}
		}
		
		if(lstCompanyId.size() > 0)
			changeCompanyStatus(lstCompanyId);
	}
	
	public static changeCompanyStatus(List<Id> lstCompanyId)
	{
		List<Company__c> lstUpdateCompany = new List<Company__c>();
		for(Company__c objCompany : [Select id, (Select id, Name from Department__c) From Company__c Where ID IN: lstCompanyId])
		{
			if(objCompany.Department__c.size() > 5)
			{
				objCompany.status = 'Hight/Medium/Low';
				lstUpdateCompany.add(objCompany);
			}
		}
		
		if(lstUpdateCompany.size() > 0)
			update lstUpdateCompany;
	}
}

 

All Answers

SwethaSwetha (Salesforce Developers) 
HI Nagoor, 
Are you meaning if there are more than 5 department records? On which object is the status field located?
Dushyant srivastava 8Dushyant srivastava 8
Hi Nagoor,
This can also be done without triggers by standard salesforce features.
Going by your Example, Company being a parent object and Department its child. We can create one Rollup summary and one Formula Field. Rollup Summary to calculate the number of Departments associated with the Account.
Formula Field to Display the Status according to your conditions 

If you specifically require a trigger then please use the code given below:
 
trigger DepartmentTrigger on Department__c (after insert, after Update) {
	if(Trigger.isAfter && Trigger.isInsert)
    {
        DepartmentTriggerHandler.onAfterInsert(Trigger.new);
    }
	if(Trigger.isAfter && Trigger.isUpdate)
	{
		DepartmentTriggerHandler.onAfterUpdate(Trigger.new , Trigger.oldMap);
	}
}


public class DepartmentTriggerHandler {
	public static void onAfterInsert(List<Department__c> lstDepartment)
    {
        collectCompanyID(lstDepartment);
    }
    
	public static void onAfterUpdate(List<Department__c> lstDepartment , Map<Id, Department__c> mapoldDepartment)
    {
        collectCompanyIDs(lstDepartment, mapoldDepartment);
    }
	
    private static void collectCompanyID(List<Department__c> lstDepartment)
    {
        List<Id> lstCompanyId = new List<Id>();
        
		for(Department__c objDepartment : lstDepartment)
        {
            if(objDepartment.Company__c != Null)
			{
				lstCompanyId.add(objDepartment.Company__c);
			}
        }
		
		if(lstCompanyId.size() > 0)
			changeCompanyStatus(lstCompanyId);
	}
	
	private static void collectCompanyID(List<Department__c> lstOldDepartment , map<Id, Department__c> mapoldDepartment)
	{
		List<Id> lstCompanyId = new List<Id>();
		
		for(Department__c objDepartment : lstOldDepartment)
		{
			if(objDepartment.Company__c != Null && mapoldDepartment.get(objDepartment.Id).Company__c != objDepartment.Company__c)
			{
				lstCompanyId.add(objDepartment.Company__c);
			}
		}
		
		if(lstCompanyId.size() > 0)
			changeCompanyStatus(lstCompanyId);
	}
	
	public static changeCompanyStatus(List<Id> lstCompanyId)
	{
		List<Company__c> lstUpdateCompany = new List<Company__c>();
		for(Company__c objCompany : [Select id, (Select id, Name from Department__c) From Company__c Where ID IN: lstCompanyId])
		{
			if(objCompany.Department__c.size() > 5)
			{
				objCompany.status = 'Hight/Medium/Low';
				lstUpdateCompany.add(objCompany);
			}
		}
		
		if(lstUpdateCompany.size() > 0)
			update lstUpdateCompany;
	}
}

 
This was selected as the best answer