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
Hammad ShahHammad Shah 

Data Validation Trigger on Update for Multiple Child Records

Hello All,

 

I needed a bit of assistance from the Salesforce community. I have created a a Custom Object called Productivity Allocations. The purpose of this object is to allow users to add more then 1 sales person to any opportunity and split the revenue associated with this opportunity based on the amount of work each sales person has contributed in closing the deal. 

 

Please bare with me as I am mainly an admin who is trying to do some development work. I have done a lot of research on the boards along with the force.com cookbook to get the correct syntax, etc for this Trigger. However I believe I am doing something incorrectly as the Trigger is not functioning as it should.

 

Relationship:

Opportunity = Master

Productivity Allocations = Detail

 

Fields:

Allocation_Percentage__c (PA Object. Number field.)

Percentage_Allocated__c (Opportunity Object. Roll Up Sammary field summing all related PA records.)

 

Functionality:

Every time a Productivity Allocation (PA) record is updated I need to verify that the total cumilative percentage of all PA records relating to a specific opportunity do not = more then 100%. If the total percentage = more then 100% then I need to throw the emded error message.

 

Here is what I have so far.

trigger PA_Percentage_Check_on_Insert on Productivity_Allocations__c (before update) {

String errmsg = '';
Boolean isError = false;

Productivity_Allocations__c NewPA = Trigger.new[0];
Productivity_Allocations__c OldPA = Trigger.old[0];

Decimal Percent3 = 0.00;
	if(OldPA.Allocation_Percentage__c== null){
	Percent3 = NewPA.Allocation_Percentage__c;
		}
		else
			{Percent3 = NewPA.Allocation_Percentage__c - OldPA.Allocation_Percentage__c;
			}	
			
Opportunity RelatedOp = [Select Id, Opportunity_Percentage_Allocated__c, OwnerId
					from Opportunity
					where Id =: NewPA.Opportunity_Name__c Limit 1]; 
					Percent3 = Percent3 + RelatedOp.Opportunity_Percentage_Allocated__c;
	if (Percent3 > 100.00){
	errmsg += 'Total Productivity Allocation percent--'+Percent3+ 'cannot be greater then 100%';
	isError = true;
							}
						
	
	
}

 Any assistance would be greatly appreciated.

 

Regards,

Hammad Shah

 

Best Answer chosen by Admin (Salesforce Developers) 
Hammad ShahHammad Shah

Nevermind. I just figured it out. The code was just missing the addError line. Please note the corrected code below.

 

 

trigger PA_Percentage_Check_on_Insert on Productivity_Allocations__c (before update) {

String errmsg = '';
Boolean isError = false;

Productivity_Allocations__c NewPA = Trigger.new[0];
Productivity_Allocations__c OldPA = Trigger.old[0];

Decimal Percent3 = 0.00;
	if(OldPA.Allocation_Percentage__c== null){
	Percent3 = NewPA.Allocation_Percentage__c;
		}
		else
			{Percent3 = NewPA.Allocation_Percentage__c - OldPA.Allocation_Percentage__c;
			}	
			
Opportunity RelatedOp = [Select Id, Opportunity_Percentage_Allocated__c, OwnerId
					from Opportunity
					where Id =: NewPA.Opportunity_Name__c Limit 1]; 
					Percent3 = Percent3 + RelatedOp.Opportunity_Percentage_Allocated__c;
	if (Percent3 > 100.00){
	errmsg += 'Total Productivity Allocation percent is currently '+Percent3+'% The Total Allocation percentage cannot be greater than 100%';
	isError = true;
	NewPA.addError(errmsg);
							}	
}

 

 

Regards,

Hammad Shah