You need to sign in to do that
Don't have an account?
Thakkar Parth
Validate trigger
How do i validate through trigger to see that the account and contact associated with an opportunity are correct ? Thanks in advance . Marc
trigger fieldUpdate on Opportunity (after update) { List<Task> tasks = new List<Task>(); List<Opportunity> Opps = Trigger.new; Map <Id, String> oppsOwner = new Map <Id, String> (); // Go through every opportunity in the trigger for (Opportunity opp : trigger.new) { // Check if the StageName has been changed if (opp.StageName != trigger.oldMap.get(opp.Id).StageName) { // get the owner ID's that have been affected oppsOwner.put(opp.ownerId, null); } } // Map the owner ID to it's email address for (User owner : [SELECT Id, Email FROM User WHERE Id = :oppsOwner.keySet()]) { oppsOwner.put(owner.Id, owner.Email); } for (Opportunity Opp : Opps) { if(opp.StageName == 'Closed Won' && trigger.oldMap.get(opp.Id).StageName != opp.StageName) { Task tsk = new Task(whatID = Opp.ID, Ownerid = Opp.OwnerId); tasks.add(tsk); } } insert tasks; List <Messaging.SingleEMailMessage> emails = new List <Messaging.SingleEMailMessage> (); // Go again through every opportunity in the trigger for (Opportunity opp : trigger.new) { // Only work with opportunities that have owners mapped to their email addresses (only those ones have their status changed) if (oppsOwner.get(opp.OwnerId) != null) { // Create an email message and add it to the list Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); List <String> toAddresses = new List <String> {oppsOwner.get(opp.ownerId)}; // set it in meassage as mail.setToAddresses(toAddresses); mail.setSubject('Automated Email : Opportunity StageName Updated'); String body = 'The status has been changed on the opportunity record with StageName ' + opp.StageName; mail.setPlainTextBody(body); emails.add(mail); } } Messaging.sendEmail(emails); }
if you wish to know that the contactrole(contact) belong to the acocunt on the opportunity, you can run a SOQL on OpportunityContactRole using the Opportunity ID and get the contact ID and then cross check if this contact belongs to the account on the opportunity.