2

This one is driving me crazy!

Find the code here:

List<BMCServiceDesk__Urgency__c> urgencies = [Select id, name from BMCServiceDesk__Urgency__c];

Debugging this with system.debug(urgencies.size()); I get:

14:20:36:056 USER_DEBUG [80]|DEBUG|0

Now if I query this exactly within the developer console as below:

Select id, name from BMCServiceDesk__Urgency__c

I'm returned with 4 IDs:

enter image description here

What's going on!? I've tried numerous different query methods that all work within the Dev Console but not within my class?

Thanks for looking.

Full code snippet:

global class NewRelicInboundEmailAlerts implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail (Messaging.InboundEmail email, Messaging.InboundEnvelope env) {

        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

        NewRelicAlert alert;

        if (email.binaryAttachments != null && email.binaryAttachments.size() > 0)
        {
            for (integer i = 0 ; i < email.binaryAttachments.size() ; i++)
            {
                if (email.binaryAttachments[i].filename.contains('.json')) {
                    alert = NewRelicAlert.parse(email.binaryAttachments[i].body.toString());
                }
            }
        }

        BMCServiceDesk__Incident__c incident = new BMCServiceDesk__Incident__c();

        // Deal with the description
        String description = '';

        description = 'New Relic Incident.\n';
        description += '\n\t Account ID: ' + alert.account_id;
        description += '\n\t Account Name: ' + alert.account_name;
        description += '\n\t Condition ID: ' + alert.condition_id;
        description += '\n\t Condition Name: ' + alert.condition_name;
        description += '\n\t Condition ID: ' + alert.current_state;
        description += '\n\t Condition Name: ' + alert.details;
        description += '\n\t Condition ID: ' + alert.event_type;
        description += '\n\t Incident Acknowledge URL: ' + alert.incident_acknowledge_url;
        description += '\n\t Incident ID: ' + alert.incident_id;
        description += '\n\t Incident URL: ' + alert.incident_url;
        description += '\n\t Owner: ' + alert.owner;
        description += '\n\t Product: ' + alert.targets[0].product;
        description += '\n\t Type: ' + alert.targets[0].type_Z;
        description += '\n\t Policy Name: ' + alert.policy_name;
        description += '\n\t Policy URL: ' + alert.policy_url;
        description += '\n\t Severity: ' + alert.severity;
        description += '\n\t Violation Chart URL: ' + alert.violation_chart_url;

        incident.BMCServiceDesk__incidentDescription__c = description;

        // Deal with the category
        List<BMCServiceDesk__Category__c> categories = [Select ID from BMCServiceDesk__Category__c where Name = :alert.targets[0].product];

        if (categories.size() > 0) {
            System.debug('Categories');
            System.debug(categories[0].id);
            incident.BMCServiceDesk__FKCategory__c = categories[0].id;
        } else {
            incident.BMCServiceDesk__FKCategory__c = 'a174E0000014bjt';
        }

        // Deal with the owner
        List<User> owners = [Select ID from User where Name = :alert.owner];

        if (owners.size() > 0) {
            System.debug('Owner');
            System.debug(owners[0].id);
            incident.OwnerId = owners[0].id;
        }

//ALL WORKS UP UNTIL THIS POINT:

        // Deal with the priority
        List<BMCServiceDesk__Urgency__c> urgencies = [Select id, name from BMCServiceDesk__Urgency__c];
        List<BMCServiceDesk__Impact__c> impacts = [Select id, name from BMCServiceDesk__Impact__c];

        String urgencyValue = '';
        String impactValue = '';

        if (alert.severity.equals('CRITICAL')) {
            urgencyValue = 'HIGH';
            impactValue = 'HIGH';
        } else {
            urgencyValue = 'MEDIUM';
            impactValue = 'MEDIUM';
        }

        system.debug(urgencies.size());
        system.debug(impacts.size());

        for (BMCServiceDesk__Urgency__c urgency : urgencies) {
            system.debug('urgency');
            system.debug(urgency);
            if (urgency.name.equals(urgencyValue)) {
                incident.BMCServiceDesk__FKUrgency__c = urgency.id;
            }
        }
        for (BMCServiceDesk__Impact__c impact : impacts) {
            system.debug('impact');
            system.debug(impact);
            if (impact.name.equals(impactValue)) {
                incident.BMCServiceDesk__FKImpact__c = impact.id;
            }
        }

        // Deal with the source
        incident.BMCServiceDesk__contactType__c = 'New Relic';

        insert incident;

        // Set the result to true. No need to send an email back to the user
        // with an error message
        result.success = true;

        // Return the result for the Apex Email Service
        return result;
    }
}
7
  • 2
    What context is the query running under in Apex? Is the class with sharing or without sharing? Is the class executing with you as the context user?
    – David Reed
    Apr 12, 2018 at 13:34
  • Have you tried to substitute "SELECT Id, Name" with "Select count() FROM BMCServiceDesk__Urgency__c"? Also, have you tried to open up permissions for BMCServiceDesk__Urgency__c all the way, trying your query again, then closing them back to what they were after troubleshooting just to see if it is a permissions issue? Also, I might try commenting out all of the code in the file except the query, then half the file, etc. Apr 12, 2018 at 14:03
  • Permissions isn't the problem as I have full access to the object as system admin. All other SOQL queries in the code work fine. It's just the urgencies and the impacts. Substituting for the code you suggested returns a count of 4 within the developer console and 0 within my class test execution. Apr 12, 2018 at 14:21
  • 3
    Are you getting the error in a unit test? You have to create your own test data...
    – Adrian Larson
    Apr 12, 2018 at 14:33
  • I'm a complete and utter idiot. Thanks Adrian. Feel free to post as an answer I'll mark it up. Apr 12, 2018 at 14:38

2 Answers 2

0

You have to create your own test data.

Isolation of Test Data from Organization Data in Unit Tests

Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create. However, objects that are used to manage your organization or metadata objects can still be accessed in your tests such as:

  • User
  • Profile
  • Organization
  • AsyncApexJob
  • CronTrigger
  • RecordType
  • ApexClass
  • ApexTrigger
  • ApexComponent
  • ApexPage
0

I have face similar issue , while running the SOQL query returning records in developer console but same soql query returning zero record in Apex class. I was running the Apex class with community user and my apex class declare "with sharing" , community user does not have access for those records, i just change my apex class "Without Sharing" and it work.

1
  • 1
    That's a dangerous answer. If you want to share records with community users do so in using a Sharing Set. That way you control the access. The way you suggest can open you for unauthorized access, since all records are returned. That might be your desired solution now, but you've effectively stopped yourself from changing that in the future without writing code. Jun 6, 2019 at 17:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .