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
LithiumsLithiums 

Accessing ParentId in a Relationship

Hi, I am having an issue with a trigger. 
I have to check for a field value in all child records and see if it is exceeding 100% and throw an error based on that

Process is the Parent Object and its child is Phase and its child is goal.

So when a goal is inserted I have to check for all goals under process and see if the percentage exceeds 100% or not and throw an error.

Here is I am trying to accessing the Process id using the Phase__r foreign key, but it is returning null.

I can write couple of queries and access the same but it will hit governer limits when I insert a more 20 records, Any suggestions would be greatly appreciated.

trigger PercentageValidation on Goal__c (before insert, before update) {
   
    Map<Goal__c,Id> goalSpMap = new Map<Goal__c,Id> ();
   
    for(Goal__c g : Trigger.new){
     goalSpMap.put(g,g.phase__r.SalesProcess__c);
    }
    system.debug('Test 1 --->' + goalSpMap);
   
}
Ashish_SFDCAshish_SFDC

Hi , 


Need some more understanding of the functional flow on this requirement. 

What I understand is when a new Goal is created it cannot exceed 100% total of all the child Goal record for that particular Phase record. 

I think you should 1st get a Rollup Summary field that will Sum up the %ge of all the child Goal records for that Parent record. 

In the Trigger for the Child Record it should check the Value for the Parent Record Field - Rollup Summary - and get the Value - Say 90% then add this field to compare to the Newly entered Goal value - if > difference of 100 and Current value throw an error. 


trigger PercentageValidation on Goal__c (before insert, before update) {

get Goal__r.Phase__c.Rollup__c ; 

If (
Goal__c.Current%__c > 100 - Goal__r.Phase__c.Rollup__c ) {

throw error; 

Else insert, 

}

}

Hope this makes sense, reply back if the scenario is different.


Regards,

Ashish