• Michael Snow 5
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies
Hi There,

So I am attempting to create a VF page that brings in all Activities onto an Opportunity from the Opportunities Primary Contact that is identified on the Opportunity. I attempted to create a custom controller class to complete this but then learned that you have to use the standard controller i norder to be able to embed the VF section onto SFDC. 

Also unfortunately now you can no longer switch the Controller from a standard controller to a custom controller anymore :( 

Any ideas on how to work around this? I shared the VF page and custom controller below

VF Page:
<apex:page controller="related_activities_from_contacts">
    <apex:form >
        <apex:pageBlock title="Activites with Contacts" id="activites_list">
            <!-- Events List -->
            <apex:pageBlockTable value="{! events }" var="evt">
                <apex:column value="{! evt.ActivityDate }"/>
                <apex:column value="{! evt.ActivityType }"/>
                <apex:column value="{! evt.Description }"/>
                <apex:column value="{! evt.OwnerId }"/>
                <apex:column value="{! evt.PrimaryWhoId }"/>
                <apex:column value="{! evt.Subject }"/>
                <apex:column value="{! evt.WhoId }"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Custom Controller:
public class related_activities_from_contacts {
    // This would be the Id of the Opportunity
    Id OppID = (Id) ApexPages.currentPage().getParameters().get('id');
    // Select only one of those contacts for use in the Contact ID Below
    OpportunityContactRole[] contactRoleArray =
        [select ContactID
         from OpportunityContactRole 
         where OpportunityId =: OppID AND isPrimary = TRUE];
    
    // Id ContactID = contactRoleArray[0];
    public List<ActivityHistory> getEvents() {
        List<ActivityHistory> results = Database.query(
            'SELECT ActivityDate, ActivityType, Description, OwnerId, PrimaryWhoId, Subject, WhoId' +
            'FROM ActivityHistory ' +
            'Where WhoId =' + contactRoleArray[0]
        );
        return results;
    }  
}

 
Hi There,

So this is my first time trying to use APEX code to automate a process for our teams - I'm not exactly sure why it is not working (or maybe not even triggering)

I guess my first question would be - is there a way to see if my APEX Trigger is even starting? and if so Is it possible to see a log of the flow of the Trigger is that done through code/comments that I have to write into the code? 

Otherwise, I copied a portion of the APEX below. The goal of this portion of the APEX is as to see if there are any custom objects - `Executive_Business_Review__c` - that are currently associated with the Account that the Opportunity is associated with (when created). These EBR objects must be within 1 year of the Opp close Date to be considered. If there are any EBR's within this one year period I want to associate them with the Opportunity. This association is done through a junction object - `EBR_Opportunity_Association__c`. 

Also, I am only posting this portion of the code for now in the hopes that if I can figure it out for this portion of the trigger then I can figure it out/ fix it for the remaining trigger types. I also plan on moving these loops into APEX Classes of their own but for now am planning to test and develop in this trigger to start 
 
trigger Auto_Associate_Opps_with_EBRs_1yr on Opportunity (after insert,after update, after undelete) {
    // This Trigger Auto Associates EBRs with an Opp when an Opp is created. The EBRs have to be within
    // one year of the close date of the Opp It assumes that there are only 4 within a year so we do 
    // not have a limit to the association of the 4 latest 
    
    //Lets Map out the logic first in commentes
    if (Trigger.isInsert) {
        //After Insert: 
        for(Opportunity Opp : Trigger.New) {
            //First Get the Account ID of the Account on the Opp
            ID OppID = Opp.Id;
            ID AcctID = Opp.Account.Id;
            Date OppCloseDate = Opp.CloseDate;
            Date OppCloseDateLimit;
            OppCloseDateLimit = OppCloseDate.addDays(-365);
            //Are there any EBRs with the Account from within the last year? - YES/NO
            List<Executive_Business_Review__c> EBRs_at_account = 
                [SELECT EBR_Account__c,Id,Name,EBR_Date__c 
                 FROM Executive_Business_Review__c 
                 WHERE EBR_Account__c =:AcctID AND EBR_Date__c >=:OppCloseDateLimit AND EBR_Date__c <=:OppCloseDate];
            
            if (EBRs_at_account.size()>0) {
                //If Yes auto associate those EBR's with the Opp      
                for(Executive_Business_Review__c EBR : EBRs_at_account) {
                    //Here I have to Create an EBR-Opportuity Association: 
                    EBR_Opportunity_Association__c EBR_Opp = new EBR_Opportunity_Association__c();
                    EBR_Opp.Executive_Business_Review__c=EBR.Id;
                    EBR_Opp.Opportunity__c=OppID;
                    insert EBR_Opp;
                    
                }
            }
        }
    }
}

Thanks for all the help and suggestions! 
Hi There,

So I am attempting to create a VF page that brings in all Activities onto an Opportunity from the Opportunities Primary Contact that is identified on the Opportunity. I attempted to create a custom controller class to complete this but then learned that you have to use the standard controller i norder to be able to embed the VF section onto SFDC. 

Also unfortunately now you can no longer switch the Controller from a standard controller to a custom controller anymore :( 

Any ideas on how to work around this? I shared the VF page and custom controller below

VF Page:
<apex:page controller="related_activities_from_contacts">
    <apex:form >
        <apex:pageBlock title="Activites with Contacts" id="activites_list">
            <!-- Events List -->
            <apex:pageBlockTable value="{! events }" var="evt">
                <apex:column value="{! evt.ActivityDate }"/>
                <apex:column value="{! evt.ActivityType }"/>
                <apex:column value="{! evt.Description }"/>
                <apex:column value="{! evt.OwnerId }"/>
                <apex:column value="{! evt.PrimaryWhoId }"/>
                <apex:column value="{! evt.Subject }"/>
                <apex:column value="{! evt.WhoId }"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Custom Controller:
public class related_activities_from_contacts {
    // This would be the Id of the Opportunity
    Id OppID = (Id) ApexPages.currentPage().getParameters().get('id');
    // Select only one of those contacts for use in the Contact ID Below
    OpportunityContactRole[] contactRoleArray =
        [select ContactID
         from OpportunityContactRole 
         where OpportunityId =: OppID AND isPrimary = TRUE];
    
    // Id ContactID = contactRoleArray[0];
    public List<ActivityHistory> getEvents() {
        List<ActivityHistory> results = Database.query(
            'SELECT ActivityDate, ActivityType, Description, OwnerId, PrimaryWhoId, Subject, WhoId' +
            'FROM ActivityHistory ' +
            'Where WhoId =' + contactRoleArray[0]
        );
        return results;
    }  
}