+ Start a Discussion
Edward Scott 5Edward Scott 5 

Set the value of a field to another

Hi Community,

I am trying to write a trigger that will update a field when the value of another field goes up or down by 10%. So I set the initial value of the field equal to the second field but from that point I would like the first field to stay the same until the value of the second field increases or decreases by 10 percent. 

I was able to get the field to copy but right now it is updating for every value change of the second field. Any help would be appreciated. I am posting the code below. 
 
trigger updateMK on Brief__c (before update, before insert) {

    

     
     
     for (Brief__c  brf : trigger.new){
     
        if (brf.Media_Kit_Needed__c == null){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
     }
     
        else if (brf.Media_Kit_Needed__c <= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
        
        else if (brf.Media_Kit_Needed__c >= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
        
        else{
        brf.Media_Kit_Needed__c = brf.Media_Kit_Needed__c;
        
        }

     
     }

}

 
Best Answer chosen by Edward Scott 5
Shikha AgashiShikha Agashi
I am so sorry, I ignore the fact to we need to look in (+/-) 10%. So what your above code was doing that it was wither looking greater or smaller than +10%. Even, I went in rythem. I fixed your original code. Please try this. I am removing my own logic also.
 
trigger updateMK on Brief__c (before update, before insert) {
        
     
     for (Brief__c  brf : trigger.new){
	 
	 
     
        if (brf.Media_Kit_Needed__c == null){
			brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
		}
     
        else {
		
		if (brf.Media_Kit_Needed__c <= integer.valueof(brf.List_Size_Updated_Monthly__c) - (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c))){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
        
        else if (brf.Media_Kit_Needed__c >= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
		
		else{
        brf.Media_Kit_Needed__c = brf.Media_Kit_Needed__c;
        
        }
        
        }
    }
}

 

All Answers

Shikha AgashiShikha Agashi
Please try below code:

 
trigger updateMK on Brief__c (before update, before insert) {
	Boolean flag =false;
        
     
     for (Brief__c  brf : trigger.new){
	 
	 
     
        if (brf.Media_Kit_Needed__c == null){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
     }
     
        else {
		if ((brf.Media_Kit_Needed__c <= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c))||
			(brf.Media_Kit_Needed__c >= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c))){
				flag=true;
			}
		(flag){
		if (brf.Media_Kit_Needed__c <= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
		flag=false;
        }
        
        else if (brf.Media_Kit_Needed__c >= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
		flag=false;
        }
        
        else{
        brf.Media_Kit_Needed__c = brf.Media_Kit_Needed__c;
        flag=false;
        }

     }
     }
	 }

}

 
Edward Scott 5Edward Scott 5
Hey Shikha,

Thanks for your response. I am getting an error on line 18. Compile Error: unexpected token: '{' at line 18 column 12. When I try to change it I can get it to compile but the field just continues to copy the second field.
Shikha AgashiShikha Agashi
I am so sorry, I ignore the fact to we need to look in (+/-) 10%. So what your above code was doing that it was wither looking greater or smaller than +10%. Even, I went in rythem. I fixed your original code. Please try this. I am removing my own logic also.
 
trigger updateMK on Brief__c (before update, before insert) {
        
     
     for (Brief__c  brf : trigger.new){
	 
	 
     
        if (brf.Media_Kit_Needed__c == null){
			brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
		}
     
        else {
		
		if (brf.Media_Kit_Needed__c <= integer.valueof(brf.List_Size_Updated_Monthly__c) - (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c))){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
        
        else if (brf.Media_Kit_Needed__c >= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
		
		else{
        brf.Media_Kit_Needed__c = brf.Media_Kit_Needed__c;
        
        }
        
        }
    }
}

 
This was selected as the best answer
Edward Scott 5Edward Scott 5
Thanks it worked. I appreciate your help.