You need to sign in to do that
Don't have an account?
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;
}
*******
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;
}
All Answers
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?
Its working fine.
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);
}
}
}
I hope you are using it on before trigger context not after context.