0

I need your help, I am a newbie in Salesforce.

Requirement : Based on employee id need to display assets(like laptop mobile,computers provided by organisations to employees) issued to him by employer

created user__C lookup(user) field on asset

<apex:page standardController="Asset" extensions="assetNewExtension">
<apex:form >
<apex:outputPanel id="userDetails">
    <apex:pageBlock >
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!Asset.User__c}">
                <apex:actionSupport event="onchange" action="{!readUser}" reRender="userDetails"/>
            </apex:inputField>
            <apex:outputField value="{!asset.Quantity}"/>
            <apex:outputField value="{!asset.Quantity}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>    
</apex:outputPanel>
</apex:form>
</apex:page>

Controller

public with sharing class assetNewExtension {
    public User selectedUser { get; set; }
    public Asset asset { get; set; }

    public assetNewExtension(ApexPages.StandardController controller) {

    }
     public assetNewExtension()
    {
        Asset = new asset();
        Asset.User__c = UserInfo.getUserId();
        selectedUser = [ Select Id From User Where Id = :UserInfo.getUserId() ];
    }

     public pageReference readUser()
    {
        Asset = [ Select  Quantity From asset Where Id =:Asset.User__c];

        return null;
    } 

}

complete error : System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!readUser}' in page vfpage: Class.assetNewExtension.readUser: line 17, column 1

Class.assetNewExtension.readUser: line 17, column 1

1

The problem is your malformed SOQL query in the readUser method - namely, you are trying to retrieve an Asset with Id equal to the Id of the current User.

So instead of query:

Asset = [ Select  Quantity From asset Where Id =:Asset.User__c];

...try to use the following:

Asset = [SELECT Quantity FROM Asset WHERE User__c = :UserInfo.getUserId()];

Oh, and try to consider naming members and variables slightly different than the name of the object. That way it would be much easier to clearly distinct object name from variable name. For instance, in your constructor and readUser method, I initially thought you forgot to declare a variable.

public with sharing class assetNewExtension {
    public User selectedUser { get; set; }
    public Asset selectedAsset { get; set; }

    //...constructor(s)...

    public pageReference readUser()
    {
        selectedAsset = [SELECT Quantity FROM Asset WHERE User__c = :UserInfo.getUserId()];

        return null;
    } 
}
  • It worked correctly ,Thank you dino ursic , Mukesh verma , you people helped me alot . – siva p Sep 22 '16 at 12:17
0

The exception is because your code is wrong constructor. It should be :

    public assetNewExtension(ApexPages.StandardController controller) {

            Asset = new asset();
            Asset.User__c = UserInfo.getUserId();
            selectedUser = [ Select Id From User Where Id = :UserInfo.getUserId() ];
        }
 public pageReference readUser()
    {
        Asset = [ Select  Quantity From asset Where user__c =:Asset.User__c];

        return null;
    } 
  • Hi Mukesh verma ,thanks for responding , i tried as you said but this is error i got , System.QueryException: List has no rows for assignment to SObject Error is in expression '{!readUser}' in page vfpage: Class.assetNewExtension.readUser: line 17, column 1 Class.assetNewExtension.readUser: line 17, column 1 --------- Is there is any document to deal with errors ,plz share – siva p Sep 22 '16 at 7:58
  • See revised Answer. SOQL should be of form Select Quantity From asset Where user__c =:Asset.User__c. – Mukesh Verma Sep 22 '16 at 8:22
0

posting completed page & controller

       <apex:outputField value="{!selectedAsset.quantity}"/>
       <apex:outputField value="{!selectedAsset.Name}"/>
       <apex:outputField value="{!selectedAsset.SerialNumber}"/>
    </apex:pageBlockSection>
</apex:pageBlock>    

public with sharing class assetNewExtension { public User selectedUser { get; set; } public Asset selectedAsset { get; set; } public Asset Asset { get; set; }

public assetNewExtension(ApexPages.StandardController controller) {

}
 public assetNewExtension()
{
    Asset = new asset();
    Asset.User__c = UserInfo.getUserId();
    selectedUser = [ Select Id From User Where Id = :UserInfo.getUserId() ];
}
public pageReference readUser()
{
    selectedAsset = [SELECT Quantity ,Name,SerialNumber FROM Asset WHERE User__c = :UserInfo.getUserId()];

    return null;
} 

}

  • Are you intending to post this as an answer? As you have already had the question answered and accepted one of the answers this is not really necessary. Please see How to Answer. – Dave Humm Sep 22 '16 at 13:23

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.