+ Start a Discussion
riffindusriffindus 

owner.isactive behaving differently

Hi All,

in my APEX code, i pull the owner id and owner active status. if i check the status with system.debug, it shows as False (where the owner is active). if i copy paste the same SOQL and check in developer console -> query editor, i am getting status as TRUE.

I don't know, how to handle this. can someone please point me where am missing?

Below is the APEX code,

userPersonAccount = [
                select
                Id,
                FirstName,
                LastName,
                Middle_Name__pc,
                OwnerId,
                Owner.IsActive,
                PersonEmail,
                (select
                    Id,
                    StageName,
                    Opportunity_Status__c,
                    isClosed,
                    Application_Step__c
                    From Account.Opportunities
                    Where StageName  = 'Application'
                )
                from Account where Id =: currentUser.get(0).AccountId];
            System.debug('\nAcocunt : '+ userPersonAccount);
            system.debug('owner active?'+userPersonAccount.Owner.IsActive);
Best Answer chosen by riffindus
riffindusriffindus
I got answer from salesforce support. i am posting the solution here. customer portal user doesn't have access to user object, so the code was not able to check whether the owner is actve or not.

solution: created a formaula field to capture owner active status and included that field in the code to check activeness.


All Answers

Cory CowgillCory Cowgill
Please post more of the code. Are your apex classes using WITH SHARING? You need to QUERY this data in a trigger. You won't get the values in a trigger context for related records unless you specifically query for them for example.
riffindusriffindus
Hi Cory,

Thanks for the reply. Yes this class is using WITH SHARING? It is a VF page controller, i cannot use this in Trigger. Please find more code on it.

public with sharing class ApplicationEntryController {
   
    // Public properites
    public String username { get; set; }
    public String password {get; set {password = value == null ? value : value.trim(); } }
   
    public String currentUserName { get { return UserInfo.getName(); } set; }
    public String currentUserUsername { get { return UserInfo.getUsername(); } set; }
    public String currentUserType { get { return UserInfo.getUserType(); } set; }  
   
    public Boolean wasSubmitted     {get;set;}
   
    public Boolean hasErrorMessages     { get { return !ApexPages.getMessages().isEmpty(); } set; }
    public Opportunity theOpp;
    public integer count=0;
    private Map<String, String> pageParameters;
    /* Constructor */
    public ApplicationEntryController() {
        username = ApexPages.currentPage().getParameters().get('username');
        this.pageParameters = ApexPages.currentPage().getParameters();
    }
public PageReference createNewApp() {
        try {
            theOpp = new Opportunity();
            Account userPersonAccount;
            PageReference successfulLoginPageRef;
           
            List<User> currentUser = [
                select
                Id,
                AccountId
                from User where Id =: UserInfo.getUserId()
                and IsActive = true];
            System.debug('\nUser is '+ currentUser);
           
            userPersonAccount = [
                select
                Id,
                FirstName,
                LastName,
                Middle_Name__pc,
                OwnerId,
                Owner.IsActive,
                PersonEmail,
                (select
                    Id,
                    StageName,
                    Opportunity_Status__c,
                    isClosed,
                    Application_Step__c
                    From Account.Opportunities
                    Where StageName  = 'Application'
                )
                from Account where Id =: currentUser.get(0).AccountId];
            System.debug('\nAcocunt : '+ userPersonAccount);
            system.debug('owner active?'+userPersonAccount.Owner.IsActive);
           Datetime curDT = Datetime.now();
            String curDate = curDT.format('MMddyyyy');
            String middleName = userPersonAccount.Middle_Name__pc!=null?' '+userPersonAccount.Middle_Name__pc:'';          
            // Fill up basic info
            theOpp.StageName = 'Application';
            theopp.Inbound_Type__c = 'Direct App';
            theOpp.Opportunity_Status__c = 'New';
            theOpp.Application_Step__c = 'Page 1';
            theOpp.CloseDate = Date.today();
            theOpp.RecordTypeId = [Select Id from RecordType Where SobjectType = 'Opportunity' And Name = 'Application' limit 1].Id;
            //theOpp.Name = userPersonAccount.FirstName + ', ' + userPersonAccount.LastName + ' - Application';
            theOpp.Name = userPersonAccount.LastName + ', ' + userPersonAccount.FirstName + middleName + '-App-' + curDate;
            // Tie the Opportunity to the Account
            theOpp.AccountId = userPersonAccount.Id;
            Id agentId = findOwner();
            if(userPersonAccount.ownerId==Label.ESM_User){
                theOpp.OwnerId = agentId;
                userPersonAccount.OwnerId=agentId;
            }else{
             system.debug('is owner active'+userPersonAccount.Owner.isActive);
                if(userPersonAccount.Owner.isActive){
                    theOpp.OwnerId = userPersonAccount.OwnerId;
                }else{
                    System.debug('The agentId is***: ' + agentId);
                    userPersonAccount.OwnerId = agentId;
                    theOpp.OwnerId = agentId;
                }
            }
            theOpp.Student_Email__c = userPersonAccount.PersonEmail;
            // Reset the Application Step back to 1
            userPersonAccount.Current_Application_Step__c = 'Page 1';
            insert theOpp;
            //update userPersonAccount;
           
            // Instantiate and populate a redirect URL for the Site.login method; use
            String redirectURL = Application_Settings__c.getInstance(theOpp.Application_Step__c).Value__c + '?id=' + theOpp.Id;
            System.debug('The redirectURL is: ' + redirectURL);
           
            //processing marketing information
            ApplicationUtils.processMarketingInfo(userPersonAccount.Id, theOpp.Id, pageParameters, false);
           
            return new PageReference(redirectURL);
           
        } catch (DmlException e) {
            count++;
            System.debug('DML Exception Captured');
            for (Integer i = 0; i < e.getNumDml(); i++) {
                // Process exception here
                System.debug('DML Exception ' + i.format() + ': ' + e.getDmlMessage(i));
                ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getDmlMessage(i)));
            }
            return null;
        } catch (Exception e) {
            count++;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getMessage()));   // TODO Custom Label - General Exception?
            return null;
        }
       
    }
riffindusriffindus
Any help?
riffindusriffindus
I got answer from salesforce support. i am posting the solution here. customer portal user doesn't have access to user object, so the code was not able to check whether the owner is actve or not.

solution: created a formaula field to capture owner active status and included that field in the code to check activeness.


This was selected as the best answer