0

We have a controller code that was working normally last year for us. I took over this probject with no information from the last person to work on these. I am trying to solve an issue where the owner of an application (custom object) is being created as just a basic guest user instead of the user they create by filling out the form. There is a piece of code here that seems to be causing the issue I believe but I cannot find how to fix

    public class NewPARedirectExt {
    public String conId {get;set;} 
    public String url {get;set;}
    public NewPARedirectExt(ApexPages.StandardController controller){
        Id usrId = UserInfo.getUserId();
        User u = [SELECT Id, Contact.Name FROM User WHERE Id = :usrId];
        if (u.ContactId != null){
            conId = u.Contact.Name;
        }
    }

    public PageReference goToCreate(){
        Schema.DescribeSObjectResult r = Presentation_Application__c.sObjectType.getDescribe();
        String keyPrefix = r.getKeyPrefix();
        Event__c ev = [SELECT Id, Name FROM Event__c WHERE Name LIKE '%2019 COABE%' limit 1];

        PageReference pr = new PageReference('/presenters/'+keyPrefix+'/e?nooverride=1&CF00No000000CGo3v='+conId+'&CF00No000000CGo3c='+ev.Name);

        pr.setRedirect(true);
        url = pr.getUrl();
        if(conId != null){
            return pr;
        } else { 
            return new PageReference('/'+keyPrefix+'/e?nooverride=1&CF00No000000CGo3c='+ev.Name);
        }
    }
}
2
  • 2
    While the code you included could definitely cause the QueryException (that Event record probably doesn't exist), the owner question appears to be an entirely separate problem upon which it is not clear this code bears.
    – David Reed
    Aug 31, 2018 at 19:42
  • The message tells you exactly what line and in what class the error is. Can you provide that detail? It would save a lot of guessing. Also you mentioned this is a guest user. Have you shared event records with that guest user?
    – Eric
    Aug 31, 2018 at 22:48

2 Answers 2

2

To expand a little on my comment:

SOQL queries can be evaluated in Apex to return a single sObject or a List<sObject>. Which evaluation mode is chosen by the compiler depends upon the context. When you do

Event__c e = [SELECT ... ];

or

User u = [SELECT  ... ];

or even

myAccountList.add([SELECT Id FROM Account ...]); // Note List.add() takes an sObject

the compiler is forced to execute the query in single-object mode. When executing in that mode, the only result that does not yield an exception is when exactly one sObject matches the query. Otherwise, you're going to get a QueryException.

The cure to this particular problem is to ensure that your queries are executing in list mode, i.e., assigning directly to a List<sObject> variable or passing them to a method that takes a List<sObject>, like List.addAll(). In that case, you receive no exception when there is not a responsive object, just an empty list, and you don't run the risk of an exception either if there's more than one responsive object.

Once you execute the query that way, you can examine myList.size() and make a decision about what you want to do - that could be taking the first returned object, if any; throwing another exception yourself; showing a page message; or other logic.

As for ownership: there's some confused-looking code here:

    Id usrId = UserInfo.getUserId();
    User u = [SELECT Id, Contact.Name FROM User WHERE Id = :usrId];
    if (u.ContactId != null){
        conId = u.Contact.Name;
    }

I'm not entirely sure why it's querying for the name of a Contact associated with the user and placing that name in a variable called conId. Since that name implies it's an Id (it's not), I can see that leading to confusion somewhere else in your code, although in most cases where the wrong value is substituted for an Id you'll get an exception rather than silent misbehavior.

Regardless, this code doesn't appear to have any direct bearing on the assignment of object ownership, so that issue appears to be separate and elsewhere.

0

Your query turned no results. If this is possilbe, then you need to add code to account for when there is no data.

you could put your query in a try catch section and then in the catch, set a value . An example might be.

String eventname = '';
try { 
    Event__c ev = [SELECT Id, Name FROM Event__c WHERE Name LIKE '%2019 
     COABE%' limit 1];
    eventname = ev.Name;
}catch(exception e){
     eventname = 'no event found';
}

then in your return, return eventname string. But to make it better, query as a list and check size and let that determine the value you return.

List<Event__c> ev = [SELECT Id, Name FROM Event__c WHERE Name LIKE '%2019 
 COABE%' limit 1];
    if(ev.size()>0){
     eventname = ev[0].name;
     }else{
     eventname = 'no event found';
     }

This is a better practice..you can use try catch as well as this is a good habit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.