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
SF7SF7 

Validation in trigger should not fire for System Admin?

hi i have a trigger in which i have some validations and i dont want them to fire for system admins ........is this possible?

 

 

trigger OpportunityBeforeTrigger on Opportunity (before insert,before update) {
Set<Id> parentOppIdSet = new Set<Id>();
Set<Id> validOppIdSet = new Set<Id>();
for(Opportunity opp: Trigger.new) {
if(opp.Parent_Opportunity__c != null) {
if(opp.Opportunity_Type__c == 'Parent')
opp.addError('Parent should not have another opportunity as parent.');

parentOppIdSet.add(opp.Parent_Opportunity__c);
} else {
if(opp.Opportunity_Type__c == 'Child')
opp.addError('Please enter a Parent Opportunity. ');
validOppIdSet.add(opp.Id);
}
}
System.debug('valididset'+validoppidset);
if(validOppIdSet != null) {
try {
Map<Id, Opportunity> childOppMap = new Map<Id, Opportunity>([Select Id, Name, Parent_Opportunity__c from Opportunity where Parent_Opportunity__c IN: validOppIdSet limit 50]);
Set<Id> errorOppIdSet = new Set<Id>();
List<OpportunityLineItem> oppLineItemList = new List<OpportunityLineItem>();
if(childOppMap != null && childOppMap.size() > 0) {
Set<Id> childOppIdSet = childOppMap.keySet();
try{
oppLineItemList = [Select Id, OpportunityId from OpportunityLineItem where OpportunityId IN: childOppIdSet limit 50];
}
catch(Exception e){
}
if(oppLineItemList != null && oppLineItemList.size() > 0) {
for(Opportunity childOpp: childOppMap.values()) {
Id childOppId = childOpp.Id;
Boolean isOppLineItemExists = false;
for(OpportunityLineItem oppLineItem : oppLineItemList) {
if(childOppId == oppLineItem.OpportunityId) {
isOppLineItemExists = true;
break;
}
}

if(!isOppLineItemExists) {
errorOppIdSet.add(childOpp.Parent_Opportunity__c);
//Opportunity opp = Trigger.new.get(childOpp.Parent_Opportunity__c);
//opp.addError('Opportunity cannot be moved to stage \'Proof\', or beyond, until it has at least one fully populated service.');
}
}
} else {
for(Opportunity childOpp: childOppMap.values()) {
errorOppIdSet.add(childOpp.Parent_Opportunity__c);
}
}
}
if(errorOppIdSet.size() > 0) {
for(Opportunity opp: Trigger.new) {

if(opp.StageName == 'Proof' || opp.StageName == 'Close' || opp.StageName == 'Result - Won (Deploy)' || opp.StageName == 'Result - Lost'
|| opp.StageName == 'Result - Walk Away' || opp.StageName == 'Result - Client Withdrew') {
if(erroroppIdSet.contains(opp.Id)) {

opp.addError('Opportunity cannot be moved beyond “Solution” until all Child Opportunities have at least one Service.');
}
}
}
}
} catch(Exception e) {
System.debug('Warning: There is no child Opportunities exists.');
}
}
Map<Id, Opportunity> parentOppMap = new Map<Id, Opportunity>([Select Id,Name, StageName,CloseDate, Opportunity_Type__c from Opportunity where Id IN: parentOppIdSet ]);
for(Opportunity opp: Trigger.new) {
if(opp.Parent_Opportunity__c != null) {
Opportunity tempOpp = parentOppMap.get(opp.Parent_Opportunity__c);
if(tempOpp.Opportunity_Type__c == 'Child')
opp.addError('Can not create Child Opportunity to another child Opportunity.');
//if(tempOpp.Opportunity_Type__c == 'Independent')
// opp.addError('Can not create Child Opportunity to independent Opportunity.');
}
}

for(Opportunity opp: Trigger.new) {
if(Opp.Opportunity_Type__c == 'Child'){
try{
Opportunity tempParentOpp = parentOppMap.get(opp.Parent_Opportunity__c);
if(opp.StageName != 'Result - Won (Deploy)' && opp.StageName != 'Result - Lost'
&& opp.StageName != 'Result - Walk Away' && opp.StageName != 'Result - Client Withdrew'){
opp.StageName = tempParentOpp.StageName;
opp.CloseDate = tempParentOpp.CloseDate;
}
} catch(Exception e ) {
System.debug('Warning: Child Opportunity should have a Parent Opportunity');
}
}
}

if(Trigger.isUpdate) {
Set<Id> changedOppIdSet = new Set<Id>();
Map<Id, Opportunity> changedOppMap = new Map<Id,Opportunity>();
Integer count = 0;
while(count < Trigger.new.size()) {
if(Trigger.new[count].Opportunity_Type__c != Trigger.old[count].Opportunity_Type__c) {
if(Trigger.old[count].Opportunity_Type__c == 'Parent') {
changedOppIdSet.add(Trigger.new[count].Id);
changedOppMap.put(Trigger.new[count].Id,Trigger.new[count]);
}
}
count++;
}
List<Opportunity> childOppList = [Select Id, Name, Parent_Opportunity__c from Opportunity where Parent_Opportunity__c IN: changedOppIdSet limit 99];
for(Opportunity opp: childOppList) {
Opportunity tempOpp = changedOppMap.get(opp.Parent_Opportunity__c);
if(tempOpp != null)
tempOpp.addError('Can not change the parent opportunity when there are child opportunities exists.');
}
}
}

 

The validations which are bolded and italic should not fire for sys admin.

 

Any help plz.

 

thanks

akhil

Best Answer chosen by Admin (Salesforce Developers) 
LoserKidLoserKid

so im not sure if you can see the user that fired the trigger. i think counts as you deflault system user. we have an intergations user set up and any time code fires it is fired as that user not the user that fired the trigger. i found this out with some code that sends emails cause we started high daily meail limits cause the trigger was always firing as out Intergations User. If it is possible then you wilkl just have to gather some user info and find the peice that say they are sys admin. but i dont think its possible. :/

All Answers

LoserKidLoserKid

so im not sure if you can see the user that fired the trigger. i think counts as you deflault system user. we have an intergations user set up and any time code fires it is fired as that user not the user that fired the trigger. i found this out with some code that sends emails cause we started high daily meail limits cause the trigger was always firing as out Intergations User. If it is possible then you wilkl just have to gather some user info and find the peice that say they are sys admin. but i dont think its possible. :/

This was selected as the best answer
AmitSahuAmitSahu

Trigger always runs in system mode.Not sure if you can validate user profile ...