3

My problem is as follows:

I've got a Site VF page with fields bound to a custom Lead object. One of these fields is a id field that's used for contact lookup. The controller seems to work fine but all records created with it are empty, since the lookup in the controller code returns 0 rows.

This is the controller:

public with sharing class VF_TabletWaitingList {

    private final CustomLead__c sales;
    private Contact c;

    public VF_TabletWaitingList(ApexPages.StandardController stdcontroller) {

      this.sales= (CustomLead__c)stdController.getRecord();
    }

    public PageReference createNew()
    {
        //CustomLead__c solumal = new CustomLead__c();
        //Set the record type on CustomLead
        RecordType r = [select Id 
                        from RecordType 
                        where Name = 'Verslun' 
                        AND SobjectType = 'CustomLead__c' limit 1];
        solumal.RecordTypeId = r.Id;

        try{
            Contact c = [select id, tx_Id__c 
                         from contact 
                         where tx_Id__c =: sales.tx_Id__c limit 1];
            system.debug('Id: '+c.tx_Id__c);
            sales.Contact__c = c.Id;
        }
        catch (Exception e){
        }
        try{
            insert sales;
            system.debug('Inserted'+sales.Id);
        }

The debug log for the Site user shows this:

09:54:31.059 (59258000)|SOQL_EXECUTE_BEGIN|[25]|Aggregations:0|select id, tx_Id__c from contact where tx_Id__c = :tmpVar1 limit 1
09:54:31.076 (76765000)|SOQL_EXECUTE_END|[25]|Rows:0

I thought it would be enough to refer to the sales object like I do in the standard controller. Any ideas where I'm going wrong?

2

You need to check whether your field tx_Id__c is referenced on the visualforce page. Otherwise this field is not available and you need to query it using SOQL:

StandardController Class: getRecord()

Note that only the fields that are referenced in the associated Visualforce markup are available for querying on this SObject. All other fields, including fields from any related objects, must be queried using a SOQL expression.

  • Yeah, I'm doing that, the variable is bound to the input field on the VF page. – akarnid Aug 26 '13 at 11:42
  • I would try outputField or just query the object: sales = [select id, tx_Id__c, ... from customLead__c where id =: sales.id]; – Sergej Utko Aug 26 '13 at 11:44
2

FIXED:

The problem was that the class was setup With Sharing. Changed it to Without Sharing, then it started to work correctly.

  • 1
    Sorry... It doesn't let me comment on your solution since I don't have enough reputation, but I had something I wanted to add. Solution: User did not have permissions and so could not see the records. Changed the class to specify 'without sharing'. Addition: So that you don't have to run the entire page in the 'without sharing' context, you could specify a method call to either an inner class or a separate class. public class with sharing MyClass { //Some stuff up here public class without sharing MyInnerClass { public static List<SObject> mySOQLCall(String someFilter) { //work } } } – dphil Sep 24 '13 at 19:36
  • 1
    Be very aware of the repercussions of using the without sharing keywords on a class. You're bypassing the record sharing configuration of your org and could be granting the user who is visiting this page access to far more records than they would normally have access to. It would be a better solution to determine why the sharing rules are preventing access to these records than to allow the VF page controller class to ignore security altogether. – Mark Pond Sep 24 '13 at 19:48
  • Yeah, it can be a bit problematic to troubleshoot these Site Users, since they don't show up in the normal Users list and they do not have a Role, which governs sharing in our org (and most orgs for that matter). Now that I've given it more thought, I guess I can put a custom field on these Site Users and have the sharing rules reflect that, ie share Accounts & Contacts with users who fit the criteria. – akarnid Sep 25 '13 at 10:48
  • addendum: I'm just discovering now that these users can be added to Public Groups. Never really imagined that it was possible. That should fix the Sharing dilemma. – akarnid Sep 25 '13 at 10:50

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.