Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question

2 Answers

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.

share|improve this answer
Yeah, I'm doing that, the variable is bound to the input field on the VF page. – akarnid Aug 26 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]; – mast0r Aug 26 at 11:44

FIXED:

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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