function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Greg RohmanGreg Rohman 

Accessing class variable in method called by actionbutton?

Hello.

Please see the greatly simplified code below. When I click the VF button that calls the "actionClassName" class, the ContentVersionDetailsMap variable is blank. I am looking for a way to obtain the contents of the ContentVersionDetailsMap variable in a method being called from a Visualforce actionbutton. Is this possible?

Thanks in advance.
public with sharing class CtrlClassName {

    public Map<Id,ContentVersion> ContentVersionDetailsMap = new Map<Id,ContentVersion>();

    public CtrlClassName() {
        Map<Id,ContentVersion> ContentVersionDetailsMap = new Map<Id,ContentVersion>([SELECT Id,Title,PathOnClient,VersionData,FileExtension FROM ContentVersion WHERE Id IN :contentDocIds]);
        system.debug('ContentVersionDetailsMap=' + ContentVersionDetailsMap);
    }

    public void actionClassName() {
        system.debug('ContentVersionDetailsMap=' + ContentVersionDetailsMap);
    }

}

 
Best Answer chosen by Greg Rohman
Greg RohmanGreg Rohman
Hi Niraj. Thanks for the reply.

I should have specified that there were bits of Apex code removed from what I posted, including code where :contentDocIds was being populated. I was able to find the issue, though, and it was related to how I was assigning values to ContentVersionDetailsMap

I was initializing the ContentVersionDetailsMap as a class variable, as indicated above. I was also using the feature that allowed the auto-population of a map from a SOQL query, as indicated at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_map_sobject.htm. I believe the issue was that in my assignment statement, I was re-declaring ContentVersionDetailsMap as a new Map variable in the class, thus seeming to clear the already assigned class variable of the same name.

I was able to solve the problem by replacing this:
Map<Id,ContentVersion> ContentVersionDetailsMap = new Map<Id,ContentVersion>([SELECT Id,Title,PathOnClient,VersionData,FileExtension FROM ContentVersion WHERE Id IN :contentDocIds]);
with this:
Map<Id,ContentVersion> ContentVersionDetailsMapTemp = new Map<Id,ContentVersion>([SELECT Id,Title,PathOnClient,FileExtension FROM ContentVersion WHERE Id IN :contentDocIds]);
ContentVersionDetailsMap = ContentVersionDetailsMapTemp.deepClone();
I am now creating a new temporary map variable and then copying it to the class variable using the deepClone() function.

-Greg



 

All Answers

Niraj Kr SinghNiraj Kr Singh
Hi Greg,

You have written WHERE condition, but not passing any value to the "contentDocIds". Because of that you are not getting any values in map "ContentVersionDetailsMap".
There are two way to get values:
1. Either you to get the direct values in "contentDocIds" in controller.
2. Or you can pass the value from page while clicking on button by using "<apex:param>".
    <apex:param name="eventId" value="{!yourValue}" assignTo="{!paramValue}"/>

Thanks
Niraj
Greg RohmanGreg Rohman
Hi Niraj. Thanks for the reply.

I should have specified that there were bits of Apex code removed from what I posted, including code where :contentDocIds was being populated. I was able to find the issue, though, and it was related to how I was assigning values to ContentVersionDetailsMap

I was initializing the ContentVersionDetailsMap as a class variable, as indicated above. I was also using the feature that allowed the auto-population of a map from a SOQL query, as indicated at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_map_sobject.htm. I believe the issue was that in my assignment statement, I was re-declaring ContentVersionDetailsMap as a new Map variable in the class, thus seeming to clear the already assigned class variable of the same name.

I was able to solve the problem by replacing this:
Map<Id,ContentVersion> ContentVersionDetailsMap = new Map<Id,ContentVersion>([SELECT Id,Title,PathOnClient,VersionData,FileExtension FROM ContentVersion WHERE Id IN :contentDocIds]);
with this:
Map<Id,ContentVersion> ContentVersionDetailsMapTemp = new Map<Id,ContentVersion>([SELECT Id,Title,PathOnClient,FileExtension FROM ContentVersion WHERE Id IN :contentDocIds]);
ContentVersionDetailsMap = ContentVersionDetailsMapTemp.deepClone();
I am now creating a new temporary map variable and then copying it to the class variable using the deepClone() function.

-Greg



 
This was selected as the best answer