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
Robert Goldberg 9Robert Goldberg 9 

Visualforce & Java/Apex Question

I have an issue that I would love some advice on.  I have a working button on both Leads & Opportunities, that sends a webservice call to an external system, and then responds accordingly to create events on the related record.

Instead of having this button work on demand, my users want this to fire upon page load, so I would like to insert the code to run this webservice call upon a page load of a simple Visualforce page.  So, as I tried to do that, I'm getting an error: Content cannot be displayed: List has no rows for assignment to SObject.  I'm unsure where the assignment is failing, or if I'm calling the wrong thing.  Any advice?

Here's my page:

<apex:page standardcontroller="Opportunity" extensions="LeadRouter_GetHistory,customActivityHistoryController" action="{!GetLeadCallLogs}"> <apex:outputText style="color: blue; float:right; font-weight: bold" value="{!showMsg}" rendered="{!Msgid}"/>
</apex:page>

And my Controllers:
1:
public class customActivityHistoryController
{
    
    public String taskId {get; set;}
    public String prefix {get; set;}
    public String showMsg {get; set;}
    public boolean  Msgid {get; set;}
    public Id recordId {get; set;}
    private String sObjName {get; set;}
    private string leadRouterId;
    private string LRUserId;
    private datetime LRDate;
   
        
    public customActivityHistoryController (ApexPages.StandardController controller )
    {
        // get the record id. It could be any sobject type.
        recordId = controller.getId();
       
    }
    
    public customActivityHistoryController ( )
    {       
        // get the record id. It could be any sobject type.
        recordId = ApexPages.currentPage().getParameters().get('id');
    }
    
    
    public void getLeadHistory()
    {
        init('History');
        LeadRouter leadRouter = new LeadRouter();
        leadRouter.GetLeadHistory(leadRouterId, LRUserId , LRDate);
       
        getActivities();
    }
    
    public void GetLeadCallLogs()
    {
        init('CallLog');
        LeadRouter leadRouter = new LeadRouter();
        LeadRouter_GetHistory.getCalls(leadRouterId, recordId);
        getActivities();
    }
    
public void init(string typeOfLog)
    {
        // get the object name according to the id
         sObjName = recordId.getSObjectType().getDescribe().getName();
         prefix = recordId.getSObjectType().getDescribe().getKeyPrefix();
        
        if ( sObjName.equalsIgnoreCase('lead') )
        {            
            Lead lead = [select Id, LR_User_ID__c, Last_LeadRouter_CallLog_Date__c, Last_LR_History_Check__c, Last_LeadRouter_History_Date__c, Lead_Router_ID__c, Date_Entered_Into_Lead_Router__c, CreatedDate from Lead where Id = :recordId ];
            leadRouterId=lead.Lead_Router_ID__c;
            System.debug('>>>>> Lead.Id: ' + lead.id);
            LRUserId    =lead.LR_User_ID__c ;
            if (typeOfLog=='History')
                LRDate      = lead.Last_LeadRouter_History_Date__c;
                        if (lead.Last_LeadRouter_CallLog_Date__c ==NULL )
        {
        Showmsg='No Lead Router Calling History' ;  
         Msgid =true;
            }
         //   else if (typeOfLog=='CallLog')
         //     LRDate      = lead.Last_LeadRouter_CallLog_Date__c;
         //   if (LRDate == null && lead.Date_Entered_Into_Lead_Router__c != null)
         //       LRDate = lead.Date_Entered_Into_Lead_Router__c;
         //   else
         //       LRDate = lead.CreatedDate;
            
            
        } else if ( sObjName.equalsIgnoreCase('opportunity')){
            Opportunity opportunity = [select Id, LR_User_ID__c, Last_LeadRouter_CallLog_Date__c, Last_LR_History_Check__c, Last_LeadRouter_History_Date__c, Lead_Router_ID__c, Date_Entered_Into_Lead_Router__c, CreatedDate from Opportunity where Id = :recordId];
            leadRouterId=opportunity.Lead_Router_ID__c;
            LRUserId    = opportunity.LR_User_ID__c ;
              if (typeOfLog=='History')
                LRDate      = opportunity.Last_LeadRouter_History_Date__c;
                        if (opportunity.Last_LeadRouter_CallLog_Date__c ==NULL )
        {
        Showmsg='No Lead Router Calling History' ;  
         Msgid =true;
            }            
        //    else if (typeOfLog=='CallLog')
        //      LRDate      = opportunity.Last_LeadRouter_CallLog_Date__c;
        //    if (LRDate == null && opportunity.Date_Entered_Into_Lead_Router__c != null)
        //        LRDate = opportunity.Date_Entered_Into_Lead_Router__c;
        //    else
        //        LRDate = opportunity.CreatedDate;
            
        }
        System.debug('>>>>> LRDate: ' + LRDate);
       
    }
    
    public List<ActivityHistory> getActivities()
    {
        
        //Build a SOQL to get the activities related to the sObject type         
        string query  = 'Select Id, (Select Id, Subject, ownerId, Owner.Name, who.Name, ActivityDate, lastModifiedDate, isTask From ActivityHistories order by  lastModifiedDate desc) ' +
                        'from  ' + sObjName +
                        ' where Id = :recordId ';
        
        List<ActivityHistory> result = new List<ActivityHistory>();
        
        
        // get the activities
        list<sObject> activities = database.query(query);
        
        system.debug(activities);
        
        for(sObject l : activities )
        {
            for(sObject ah : l.getSObjects('ActivityHistories') ) {
                result.add((ActivityHistory)ah);
            }
        }
        
        return result;
        
    }
    
    
    public PageReference editRecord(){
        String url = '/';
        url += taskId;
        url += '/e?';
        
        return new PageReference(url).setRedirect(true);
    }
    
    public PageReference deleteRecord(){
        delete [Select Id
                From event
                Where Id = :taskId];
        
        return null;
    }
    

}

2:
global class LeadRouter_GetHistory {

    public Id recordId {get; set;}
    public Opportunity currentOpportunity;
    public LeadRouter_GetHistory (ApexPages.StandardController controller )
    {
        // get the record id. It could be any sobject type.
        recordId = controller.getId();
       
    }
    webService static string getHistory(String entity, string eid){
        
        if (entity == 'Opportunity') {
            Opportunity o = [Select Id, Lead_Router_ID__c, LR_User_ID__c, Last_LeadRouter_History_Date__c, Last_LeadRouter_CallLog_Date__c from Opportunity where Id = :eid];
            //if (!string.isBlank(o.Lead_Router_ID__c)) {
                LeadRouter.GetLeadHistoryInfo(o.Lead_Router_ID__c, o.LR_User_ID__c, o.Last_LeadRouter_History_Date__c);
            //}
        } else {
            Lead l = [Select Id, Lead_Router_ID__c, LR_User_ID__c, Last_LeadRouter_History_Date__c, Last_LeadRouter_CallLog_Date__c from Lead where Id = :eid];
            //if (!string.isBlank(l.Lead_Router_ID__c)) {
                LeadRouter.GetLeadHistoryInfo(l.Lead_Router_ID__c, l.LR_User_ID__c, l.Last_LeadRouter_History_Date__c);
            //}
        }
        return eid;
    }
    
    webService static string getCalls(String entity, string eid){
        
        if (entity == 'Opportunity') {
            Opportunity o = [Select Id, Lead_Router_ID__c, LR_User_ID__c, Last_LeadRouter_History_Date__c, Last_LeadRouter_CallLog_Date__c from Opportunity where Id = :eid];
            //if (!string.isBlank(o.Lead_Router_ID__c)) {
                LeadRouter.GetLeadCallLogsInfo(o.Lead_Router_ID__c, o.Last_LeadRouter_CallLog_Date__c);
            //}
        } else {
            Lead l = [Select Id, Lead_Router_ID__c, LR_User_ID__c, Last_LeadRouter_History_Date__c, Last_LeadRouter_CallLog_Date__c from Lead where Id = :eid];
            //if (!string.isBlank(l.Lead_Router_ID__c)) {         
                LeadRouter.GetLeadCallLogsInfo(l.Lead_Router_ID__c, l.Last_LeadRouter_CallLog_Date__c);
            //}
        }
        return eid;
    }
    
    public static void test(String entity, string eid){
        if (entity == 'Opportunity') {
            Opportunity o = [Select Id, Lead_Router_ID__c, LR_User_ID__c, Last_LeadRouter_History_Date__c, Last_LeadRouter_CallLog_Date__c, Last_LR_History_Check__c from Opportunity where Id = :eid];
            o.Last_LR_History_Check__c = DateTime.now();
            update o;
            Event ev = new Event();
            ev.WhatId = o.ID;
            ev.StartDateTime = DateTime.now();
            ev.ActivityDateTime = DateTime.now();
            ev.Subject = 'LeadRouter Test Activity';
            ev.Description = 'LeadRouter Test Activity completed';      
            ev.DurationInMinutes = 0;
            ev.runSubjTrigger__c = false;//so trigger doesn't run on it
            insert ev;
            //}
        } else {
            Lead l = [Select Id, Lead_Router_ID__c, LR_User_ID__c, Last_LeadRouter_History_Date__c, Last_LeadRouter_CallLog_Date__c, Last_LR_History_Check__c from Lead where Id = :eid];
            l.Last_LR_History_Check__c = DateTime.now();
            update l;
            Event ev = new Event();
            ev.WhoId = l.ID;
            ev.StartDateTime = DateTime.now();
            ev.ActivityDateTime = DateTime.now();
            ev.Subject = 'LeadRouter Test Activity';
            ev.Description = 'LeadRouter Test Activity completed';      
            ev.DurationInMinutes = 0;
            ev.runSubjTrigger__c = false;//so trigger doesn't run on it
            insert ev;
        }
        
    }
}