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
SabrentSabrent 

trigger to add currency value of 12 fields

Currently I have a workflow / field update but due to some limitations I want to change this workflow into a trigger

Note: All fields are in same object called object__c,

F1__c,F2__c etc are number fields,

FieldAddAll__c is of type Currency,

Field_1__c, Field_2__c etc are of type currency and the value in  these is populated by formula

evaluation criteria: Everytime a record is created or updated

Rule criteria:

OR(OR(RecordTypeId="012333333333",
RecordTypeId="0125555555555",
RecordTypeId="01266666666666"),
OR( ISCHANGED( F1__c ),
ISCHANGED( F2__c ),
ISCHANGED( F3__c ),
ISCHANGED( F4__c ),
ISCHANGED( F5__c ),
ISCHANGED( F6__c ),
ISCHANGED( F7__c ),
ISCHANGED( F8__c ),
ISCHANGED( F9__c ),
ISCHANGED( F10__c ),
ISCHANGED( F11__c ),
ISCHANGED( F12__c ),
))


Field update:

FieldAddAll__c =

( Field_1__c  + Field_2__c  + Field_3__c  + Field_4__c  + Field_5__c + Field_6__c + Field_7__c + Field_8__c + Field_9__c + Field_10__c + Field_11__c + Field_12__c  )

 

 

I want to chnage the above workflow into a trigger. Any help will be appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

Here's some of your code written as a trigger....

Keep in mind it's not all written out but you should have an idea.

Trigger <triggerName> on object__c (before insert, before update){
	Set<String> allowedRecordTypes = new Set<String>();
	allowedRecordTypes.add("012333333333");
	allowedRecordTypes.add("0125555555555");
	allowedRecordTypes.add("01266666666666");
	
	for(object__c o :trigger.new){
		
		if(trigger.isInsert && allowedRecordTypes.contains(o.RecordTypeId)){
			o.FieldAddAll__c = Field_1__c  + Field_2__c  + Field_3__c  + Field_4__c  + Field_5__c + Field_6__c + Field_7__c + Field_8__c + Field_9__c + Field_10__c + Field_11__c + Field_12__c;
		}else if(allowedRecordTypes.contains(o.RecordTypeId) && (o.F1__c != trigger.oldMap.get(o.Id).F1__c || o.F2__c != trigger.oldMap.get(o.Id).F2__c) || ...){
			o.FieldAddAll__c = Field_1__c  + Field_2__c  + Field_3__c  + Field_4__c  + Field_5__c + Field_6__c + Field_7__c + Field_8__c + Field_9__c + Field_10__c + Field_11__c + Field_12__c;
		}
		
	}
	
}

 

To make things even simpler I would just add up your values every time the records are changed just to simplify the trigger as long as the recordtype criteria matches of course

Trigger <triggerName> on object__c (before insert, before update){
	Set<String> allowedRecordTypes = new Set<String>();
	allowedRecordTypes.add("012333333333");
	allowedRecordTypes.add("0125555555555");
	allowedRecordTypes.add("01266666666666");
	
	for(object__c o :trigger.new){
		
		if(allowedRecordTypes.contains(o.RecordTypeId)){
			o.FieldAddAll__c = Field_1__c  + Field_2__c  + Field_3__c  + Field_4__c  + Field_5__c + Field_6__c + Field_7__c + Field_8__c + Field_9__c + Field_10__c + Field_11__c + Field_12__c;
		}
		
	}
	
}

 

 

All Answers

Alex.AcostaAlex.Acosta

Here's some of your code written as a trigger....

Keep in mind it's not all written out but you should have an idea.

Trigger <triggerName> on object__c (before insert, before update){
	Set<String> allowedRecordTypes = new Set<String>();
	allowedRecordTypes.add("012333333333");
	allowedRecordTypes.add("0125555555555");
	allowedRecordTypes.add("01266666666666");
	
	for(object__c o :trigger.new){
		
		if(trigger.isInsert && allowedRecordTypes.contains(o.RecordTypeId)){
			o.FieldAddAll__c = Field_1__c  + Field_2__c  + Field_3__c  + Field_4__c  + Field_5__c + Field_6__c + Field_7__c + Field_8__c + Field_9__c + Field_10__c + Field_11__c + Field_12__c;
		}else if(allowedRecordTypes.contains(o.RecordTypeId) && (o.F1__c != trigger.oldMap.get(o.Id).F1__c || o.F2__c != trigger.oldMap.get(o.Id).F2__c) || ...){
			o.FieldAddAll__c = Field_1__c  + Field_2__c  + Field_3__c  + Field_4__c  + Field_5__c + Field_6__c + Field_7__c + Field_8__c + Field_9__c + Field_10__c + Field_11__c + Field_12__c;
		}
		
	}
	
}

 

To make things even simpler I would just add up your values every time the records are changed just to simplify the trigger as long as the recordtype criteria matches of course

Trigger <triggerName> on object__c (before insert, before update){
	Set<String> allowedRecordTypes = new Set<String>();
	allowedRecordTypes.add("012333333333");
	allowedRecordTypes.add("0125555555555");
	allowedRecordTypes.add("01266666666666");
	
	for(object__c o :trigger.new){
		
		if(allowedRecordTypes.contains(o.RecordTypeId)){
			o.FieldAddAll__c = Field_1__c  + Field_2__c  + Field_3__c  + Field_4__c  + Field_5__c + Field_6__c + Field_7__c + Field_8__c + Field_9__c + Field_10__c + Field_11__c + Field_12__c;
		}
		
	}
	
}

 

 

This was selected as the best answer
SabrentSabrent

Thanks. I will try this and buidl upon the idea.

SabrentSabrent

Any reason why i am getting this error?

 

Error: Compile Error: line breaks not allowed in string literals at line 5 column -1

 

Alex.AcostaAlex.Acosta

missing a quote on line 5

 

allowedRecordTypes.add(012V00000004Q5F');

 

should be 

 

allowedRecordTypes.add('012V00000004Q5F');

SabrentSabrent

Thanks I noticed that after posting it here. Really appreciate your help. Trigger compiled without errors, will test for correctness and let you know the outcome. Will close the post shortly thereafter.

once again thanks.

SabrentSabrent

Thank you very much for your response.  

Based on your suggestion and one other member's suggestion this is what i did, seems to be working fine. 

Note: requirement changed a bit so got rid of recordtype.

 

trigger triggername on object__c (before insert, before update){
     
   for(object__c o :trigger.new){
         
          object__c oldFC = Trigger.oldMap.get(o.Id);

             if (
                  (oldFC.f1__c != o.f1__c) ||
                   (
                       (oldFC.f2__c != o.f2__c) || (......) || (..........)      
                   )
                 )
          
              o.fieldAddAll__c = o.field1__c + ..........   ;
   }
}
 

 

Alex.AcostaAlex.Acosta

Just keep an eye out for 

 

object__c oldFC = Trigger.oldMap.get(o.Id);

 

This will throw you an exception on inserts as the old record values do not exist.

SabrentSabrent

in that case should i just say ,

 

trigger triggername on object__c (before update){
     
   for(object__c o :trigger.new){
object__c oldFC = Trigger.oldMap.get(o.Id); if ( (oldFC.f1__c != o.f1__c) || ( (oldFC.f2__c != o.f2__c) || (......) || (..........) ) ) o.fieldAddAll__c = o.field1__c + .......... ; } }

 or should i do this - 

 

trigger triggername on object__c (before insert, before update){
     
   for(object__c o :trigger.new){

      if (trigger.isinsert){
              
         o.fieldAddAll__c = o.field1__c + ..........   ;       

      }
       
      if(trigger.isupdate){

          object__c oldFC = Trigger.oldMap.get(o.Id);

             if (
                  (oldFC.f1__c != o.f1__c) ||
                   (
                       (oldFC.f2__c != o.f2__c) || (......) || (..........)      
                   )
                 )
          
              {
                o.fieldAddAll__c = o.field1__c + ..........   ;
              }
      
      }
    }  
}

 

i would appreciate if you could give me a suggestion. thanks heaps!!

 

 

 

 

 

Alex.AcostaAlex.Acosta

Personally I prefer the second approach but you can split them up as you like. The last thing I can really say which could potentually give you an error is  if any of the fields you are adding up is

1) the field not an decimal / integer / double / currency

2)one of those values are null and maybe blank.

 

Test this senerio out and if you do have that issue there are a few ways to avoid this. Just make sure on your page layout / field level they are required, or check each field individually to validate they are not null / blank before adding them to your final value.

 

Otherwise it looks great.

SabrentSabrent

Thanks. I appreciate your time and help. 

tested scenario  2 . it works fine.

some of these fields are populated in workflow , if not they are required. i guess, i am good there.

 

 

once again, Thanks a lot.