You need to sign in to do that
Don't have an account?
Stephanie Zimmerman 7
How to reference fields on What parent object in Task Trigger
Hello, please see Event trigger below. It prevents the delete of an Event record if running user is not equal to created by user. I would like to update to add another condition to the IF statement ...and if Opportunity.Stage != 'Appt Canceled'. Perhaps something along the lines of if ((UserInfo.getUserId() != a.CreatedById)) && a.what.stage != 'Appt Cancelled')
However, I know that a.what.stage = 'Appt Cancelled' is not the right way to do this due to the polymorphic nature of the What relationship field. How can I write this in so that Salesforce knows I want to look only at Opportunity.Stage?
Below is the full trigger. Thank you to anyone who has a few minutes to help.
trigger NoDeleteonEvent on Event (before delete)
{
for (Event a : Trigger.old)
{
system.debug(UserInfo.getUserId()+ ' -- ' +a.CreatedById);
if (UserInfo.getUserId() != a.CreatedById)
{
a.addError('You cannot delete an event that was assigned to you by another person');
}
}
}
However, I know that a.what.stage = 'Appt Cancelled' is not the right way to do this due to the polymorphic nature of the What relationship field. How can I write this in so that Salesforce knows I want to look only at Opportunity.Stage?
Below is the full trigger. Thank you to anyone who has a few minutes to help.
trigger NoDeleteonEvent on Event (before delete)
{
for (Event a : Trigger.old)
{
system.debug(UserInfo.getUserId()+ ' -- ' +a.CreatedById);
if (UserInfo.getUserId() != a.CreatedById)
{
a.addError('You cannot delete an event that was assigned to you by another person');
}
}
}
try this one.....
{
Set<Id> setOpportunityId = new Set<Id>();
for (Event objEvent : Trigger.old)
{
if(objEvent.WhatId != null && Schema.Opportunity.SObjectType == objEvent.WhatId.getSobjectType())
setOpportunityId.add(objEvent.WhatId);
}
Map<Id, Opportunity> mapIdToOpportunity = new Map<Id, Opportunity>([SELECT Id, StageName FROM Opportunity WHERE Id IN: setOpportunityId]);
for (Event objEvent : Trigger.old)
{
if(UserInfo.getUserId()!= e1.CreatedById && objEvent.WhatId != null && mapIdToOpportunity.containsKey(objEvent.WhatId) && mapIdToOpportunity.get(objEvent.WhatId).StageName != 'Appt Cancelled')
{
objEvent.addError('You cannot delete an event that was assigned to you by another person');
}
}
}