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
tschatztschatz 

Multiple Object Troubles

Hi All,
 
I'm new and hoping someone can help me along. I've created a trigger that fires when an Opportunity is added or updated. It calls a class method that I have created.
 
The method checks to see if the Stage field is set to Closed Won, if it is the method does things. So far this is working as long as I don't have the method do to much. Basically I have it add an error to the page so I could see that it is firing, which it is.
 
Here's what I'm looking to do, when an Opportunity Stage is set to Closed Won, I want to find the First Name, Last Name, and Email Address of the Contact with the Contact Role of 'Ship To' (a custom role set in Salesforce) for that Opportunity.
 
Here's what I have in my Class so far:
 
Code:
public class CP_OpportunityClosedWon {
 public static void CP_createNewCPUser(Opportunity[] opps){
  for (Opportunity o:opps){
   if (o.StageName == 'Closed Won') {
    Opportunity ST = [Select (Select ContactId From OpportunityContactRoles where Role = 'Ship To') From Opportunity o];
    //o.StageName.addError(ST.FirstName); my failed attempt to see what is contained in ST
   }
  }
 }
}

 In the line: Opportunity ST = [Select (Select ContactId From OpportunityContactRoles where Role = 'Ship To') From Opportunity o];
I'm trying to get the ContactID of the Ship To person so I can access that person's Contact information, but I'm unsure how to get there.
 
Any help is appriciated.
 
_t
tschatztschatz

Okay I figured it out. Here's what I did in case anyone else is having the same troubles.

Code:

public class CP_OpportunityClosedWon {
 public static void CP_createNewCPUser(Opportunity[] opps){
  for (Opportunity o:opps){
   //System.debug(o.Id);
   if (o.StageName == 'Closed Won') {
    
    ID cID = [select contactid from opportunitycontactrole where role = 'Ship To' AND opportunityid = :o.id].contactId;
    Contact c = [select id from contact where id = :cID];
    String cName = [select title from contact where id = :cID].title;//Contact c = [select contactid from 
    c.Portal_User__c = True;
    c.tempName__c = cName;
    update c;
    o.tempName__c = cName;
    //System.debug(ocr);
   }
  }
 }
}