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
Thakkar ParthThakkar 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);
       
  }


Ankit AroraAnkit Arora
A little clarification, you want to check if the associated account and contact record with the opportuntiy exists/doesn't exists or anything else?
Sonam_SFDCSonam_SFDC
If I understand this correctly, you wish to check if the correct account and contact are marked on the opportunity are correct..against which data you wish to cross check that the account and contact are correct?

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.
Thakkar ParthThakkar Parth
Yes, with the same code , I want to check associated account and contact record with the opportunity exists / deosn't exists . Thanks again.
Ankit AroraAnkit Arora
I think SOQL is the option. You've to first fetch the Ids of both account and contact then query it to check if query returns the result or not. If yes then they exists else they don't. You can do this in one query using inner query if your account and contact are related.
Thakkar ParthThakkar Parth
Sorry Guys, was away . I was thinking the same to go with SOQL . But how do i go about it with my code . If you can suggest some modifications it would be helpful.Thanks again.