1) Write a trigger on the Account when the Account is updated check all opportunities related to the account. Update all Opportunities Stage to close lost if an opportunity created date is greater than 30 days from today and stage not equal to close won.
trigger UpdateOpportunitiesOnAccountUpdate on Account (after update) {
// Get the IDs of all the updated Accounts
Set<Id> accountIds = new Set<Id>();
for (Account acc : Trigger.new) {
accountIds.add(acc.Id);
}
// Query for all Opportunities related to the updated Accounts
List<Opportunity> opportunitiesToUpdate = [
SELECT Id, StageName, CreatedDate
FROM Opportunity
WHERE AccountId IN :accountIds
];
// Update Opportunities that meet the criteria
List<Opportunity> opportunitiesToUpdateStage = new List<Opportunity>();
for (Opportunity opp : opportunitiesToUpdate) {
if (opp.CreatedDate.addDays(30) < Date.today() && opp.StageName != 'Closed Won') {
opp.StageName = 'Closed Lost';
opportunitiesToUpdateStage.add(opp);
}
}
// Save the updated Opportunities
if (!opportunitiesToUpdateStage.isEmpty()) {
update opportunitiesToUpdateStage;
}
}
2) Once an Account is inserted an email should go to the System Admin user with specified text below.
An account has been created and the name is “Account Name”.
trigger SendEmailOnAccountInsert on Account (after insert) {
// Get the email address of the System Admin user
User adminUser = [
SELECT Email
FROM User
WHERE Profile.Name = 'System Administrator'
LIMIT 1
];
// Create the email message
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String>{ adminUser.Email });
email.setSubject('New Account Created');
email.setPlainTextBody('An account has been created and the name is "' + Trigger.new[0].Name + '".');
// Send the email
Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{ email });
}
3) Once an Account will update then that Account will update with the total amount from All its Opportunities on the Account Level. The account field name would be ” Total Opportunity Amount “.
trigger UpdateAccountTotalAmount on Opportunity (after insert, after update, after delete) {
// Get the IDs of all Accounts related to the Opportunities being updated or deleted
Set<Id> accountIds = new Set<Id>();
for (Opportunity opp : Trigger.new) {
accountIds.add(opp.AccountId);
}
for (Opportunity opp : Trigger.old) {
accountIds.add(opp.AccountId);
}
// Query for all Accounts related to the updated or deleted Opportunities
List<Account> accountsToUpdate = [
SELECT Id, Name, (SELECT Id, Amount FROM Opportunities)
FROM Account
WHERE Id IN :accountIds
];
// Loop through each Account and update the Total Opportunity Amount field
for (Account acc : accountsToUpdate) {
Decimal totalAmount = 0;
for (Opportunity opp : acc.Opportunities) {
if (opp.StageName != 'Closed Won') {
continue; // exclude opportunities that aren't Closed Won
}
totalAmount += opp.Amount;
}
acc.Total_Opportunity_Amount__c = totalAmount;
}
// Save the updated Accounts
if (!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
}
}
4) Write a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.
trigger PreventDuplicateContacts on Contact (before insert, before update) {
// Create sets to hold unique email and phone values
Set<String> uniqueEmails = new Set<String>();
Set<String> uniquePhones = new Set<String>();
// Loop through each Contact in the trigger context
for (Contact cont : Trigger.new) {
// Check if the Contact's Email is a duplicate
if (cont.Email != null && uniqueEmails.contains(cont.Email)) {
cont.Email.addError('A Contact with this Email already exists.');
} else {
uniqueEmails.add(cont.Email);
}
// Check if the Contact's Phone is a duplicate
if (cont.Phone != null && uniquePhones.contains(cont.Phone)) {
cont.Phone.addError('A Contact with this Phone already exists.');
} else {
uniquePhones.add(cont.Phone);
}
}
}
5) Write a trigger, only the system admin user should be able to delete the task.
trigger PreventTaskDeletion on Task (before delete) {
// Get the ID of the System Admin profile
Id systemAdminProfileId = [
SELECT Id
FROM Profile
WHERE Name = 'System Administrator'
LIMIT 1
].Id;
// Get the ID of the current user
Id currentUserId = UserInfo.getUserId();
// Check if the current user is a System Admin
Boolean isSystemAdmin = [
SELECT COUNT()
FROM User
WHERE Id = :currentUserId
AND ProfileId = :systemAdminProfileId
] > 0;
// If the current user is not a System Admin, prevent the Task from being deleted
if (!isSystemAdmin) {
for (Task t : Trigger.old) {
t.addError('Only the System Administrator can delete Task records.');
}
}
}
Comments
Post a Comment
Please Write your comment here.