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
RajanRajan 

Unable to update record using trigger

I'm having process builder in my org for autocalculation of some fields. Now I have to replace that process builder with code. So I have written code for that but unable to insert fields after any DML operation.Below is my code for the same. data should automatically inser in field 'Exposure_at_Default__c' from  field Limit__c 
*******
trigger EAD_Calculator on Facility__c (after insert, after update) {
    Facility__c[] facList = new Facility__c[] {};
        
        //if (trigger.isAfter){
            if (trigger.isUpdate || trigger.isInsert){
                for (Facility__c fac: Trigger.new) {
                    Facility__c newFac = new Facility__c(id= fac.Id);
                     if(newFac.Limit__c != null){
                            newFac.Exposure_at_Default__c = newFac.Limit__c;
                         system.debug('Limit__c' +newFac.Limit__c);
                         system.debug('Exposure_at_Default__c' +newFac.Exposure_at_Default__c);
                     facList.add(newFac);
               }
            }
         }
      upsert facList;
    }
Best Answer chosen by Rajan
Nayana KNayana K
trigger EAD_Calculator on Facility__c (before insert, before update) 
{
	if(Trigger.isBefore)
	{
		if (trigger.isUpdate || trigger.isInsert)
		{
			
			Ent_BDL_Configuration__c objSetting = Ent_BDL_Configuration__c.getInstance();
			for (Facility__c fac: Trigger.new) 
			{
				if(fac.Limit__c != null)
				{
					fac.Exposure_at_Default__c = objSetting.EAD_Calculation_Rate__c != NULL ? objSetting.EAD_Calculation_Rate__c * fac.Limit__c : fac.Limit__c;
				}
			}
		}
	}
}

 

All Answers

Nayana KNayana K
I think you just want a code to copy one field value to other which you can do with before context (Not sure though):
trigger EAD_Calculator on Facility__c (before insert, before update) 
{
	if(Trigger.isBefore)
	{
		if (trigger.isUpdate || trigger.isInsert)
		{
			for (Facility__c fac: Trigger.new) 
			{
				if(fac.Limit__c != null)
				{
					fac.Exposure_at_Default__c = fac.Limit__c;
				}
			}
		}
	}
}
OR
trigger EAD_Calculator on Facility__c (before insert, before update) 
{
	for (Facility__c fac: Trigger.new) 
	{
		if(fac.Limit__c != null)
		{
			fac.Exposure_at_Default__c = fac.Limit__c;
		}
	}
}



 
RajanRajan
Thanks Nayana for the help,
Need one more help.
I have custom setting with name 'Ent_BDL_Configuration__c' and its field is 'EAD_Calculation_Rate__c'.
As per process builder, the result of 'Exposure_at_Default__c' was calculation of Limit__c  and custom setting field 'EAD_Calculation_Rate__c'.
How to do that calculation in trigger?
Nayana KNayana K
Is this hierarchy custom setting? and you want to do something like  Exposure_at_Default__c = custom setting rate * limit__c ?
RajanRajan
Right Nayana.
Nayana KNayana K
trigger EAD_Calculator on Facility__c (before insert, before update) 
{
	if(Trigger.isBefore)
	{
		if (trigger.isUpdate || trigger.isInsert)
		{
			
			Ent_BDL_Configuration__c objSetting = Ent_BDL_Configuration__c.getInstance();
			for (Facility__c fac: Trigger.new) 
			{
				if(fac.Limit__c != null)
				{
					fac.Exposure_at_Default__c = objSetting.EAD_Calculation_Rate__c != NULL ? objSetting.EAD_Calculation_Rate__c * fac.Limit__c : fac.Limit__c;
				}
			}
		}
	}
}

 
This was selected as the best answer
Nayana KNayana K
For your reference, https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm#apex_methods_system_custom_settings
Nayana KNayana K
Please mark this as solved if it helps!
RajanRajan
Thanks alot Nayana,
Its working fine.
RajanRajan
Hi Nayana,

Suppose I have this below method and I want to perform the same operation on multiple objects(3 objects) then How to do that?
 Also I have to check old.value and new.value on update cases.
Method is :
********************
  public static void fca_EAD_Calculator(List<sobject> newFacList){
        set<Id> fcaId = new Set<Id>();
        
        Ent_BDL_Configuration__c objSetting = Ent_BDL_Configuration__c.getInstance();
        Acorn.debug('objSetting #####' +objSetting );
        for(sObject obj : newFacList){
            Facility_Contract_Application__c fca = (Facility_Contract_Application__c) obj;
            if(fca.Credit_Limit__c != null){
                fca.Exposure_at_Default_EAD__c = (objSetting.EAD_Calculation_Rate__c != NULL ? objSetting.EAD_Calculation_Rate__c * fca.Credit_Limit__c : fca.Credit_Limit__c)/100;
                fcaId.add(fca.Id);
            }
        }
    }
Nayana KNayana K
What you need here is reusable method using sobject:
 
public static void fca_EAD_Calculator(List<sobject> newFacList, String limitFieldApi, String exporeFieldApi){

        Ent_BDL_Configuration__c objSetting = Ent_BDL_Configuration__c.getInstance();
		Decimal temp;
        Acorn.debug('objSetting #####' +objSetting );
        for(sObject obj : newFacList){
            temp = Decimal.valueOf(obj.get(limitFieldApi));  /*or try,  temp = (Decimal)obj.get(limitFieldApi); */
            if(temp != null){
                fca.put(exporeFieldApi, (objSetting.EAD_Calculation_Rate__c != NULL ? objSetting.EAD_Calculation_Rate__c * temp : temp)/100);
                
            }
        }
    }
I hope you are using it on before trigger context not after context.