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
Tom DawsonTom Dawson 

Create variable in APEX and access it in the controller

Hi All

I have a problem at the moment where i am trying to access a variable in the controller which i am creating in APEX but i cannot pull it through. Any suggestions would be great!

APEX
@AuraEnabled
    public static Senior_Parking_Permit__c checkPermit(String permitNumber){
        system.debug('Hit the server');
        system.debug(permitNumber);
        
        User currUser = getCurrPortalUser();
        system.debug('currUser =  ' + currUser );
         
        if (currUser.ContactId == null ) return null;
        
        List<Senior_Parking_Permit__c> foundPermits = [SELECT Id, Name, Permit_Number__c, Claimed__c FROM Senior_Parking_Permit__c 
                                                               WHERE Permit_Number__c = :permitNumber
                                                               LIMIT 1];
        
        if(foundPermits.size() == 0) {
            system.debug('Could not find any matching Senior_Parking_Permit__c rec, do nothing ');
            
            return null;
        }
        else{
            Senior_Parking_Permit__c foundPermit = foundPermits[0];
            
            if(foundPermit.Claimed__c == true){
                
                Boolean Claimed = true;
            }
            else {
                Boolean Claimed = false;
            }
            
            system.debug('Found a Senior_Parking_Permit__c record - ' + foundPermit);
            system.debug('... and it hasnt been claimed yet, lets claim it. ');
            
            foundPermit.Claimed__c = true;
            system.debug('Before update foundPermit  - ' + foundPermit);
            
            update foundPermit;
            return foundPermit;
        }
    }

Controller
permitClaim : function(component, event, helper){
        var action = component.get("c.checkPermit");
        var permitNumber = component.get("v.simpleNewCase.Permit_Number__c");
        
        console.log("permitNumber",permitNumber);
        
        action.setParams({
            permitNumber : permitNumber
        });
        
        action.setCallback(this, function(a) {
            
            var state = a.getState();
            console.log("State",state);
            var permitData = a.getReturnValue(); 
            console.log("permitData",permitData);
            
            if(permitData === null){
                debugger;
                var permitNotFound = true;
                component.set("v.permitNotFound",true);
                console.log(permitNotFound);
            }
            
            if(a.getState() === 'SUCCESS'){
                
                if(permitData != null){
                    debugger;
                component.set("v.foundPermit",permitData.Permit_Number__c);
                component.set("v.permitClaimed",permitData.Claimed);
                    
                var permitFound = component.get("v.foundPermit");
                console.log("permitNumber",permitNumber);
                    
                var permitClaimed = component.get("v.permitClaimed");
                console.log("permitClaimed",permitClaimed);
                    
                var permitNotFound = false;
                component.set("v.permitNotFound",false);
                console.log(permitNotFound);
                    
                }
                
                if((permitFound == permitNumber) && (permitClaimed === false)){
                    var showToast = $A.get("e.force:showToast");
                    console.log("showToast")
                    showToast.setParams({
                        "title": "Permit Found",
                        "type" :"Success",
                        "message": "Your existing parking permit has been found."
                    });
                    showToast.fire();
                } else if ((permitFound == permitNumber) && (permitClaimed === true)){
                    
                    var showToast = $A.get("e.force:showToast");
                    console.log("showToast")
                    showToast.setParams({
                        "title": "Permit Already Claimed",
                        "type" :"error",
                        "message": "The parking permit number you have selected has already been claimed. If this has been claimed incorrectly, please contact Customer Services."
                    });
                    showToast.fire();
                } else if(permitNotFound === true){
                    var showToast = $A.get("e.force:showToast");
                    console.log("showToast")
                    showToast.setParams({
                        "title": "Permit Not Found",
                        "type" :"error",
                        "message": "The parking permit number you have entered has not been recognised. Please try again."
                    });
                    showToast.fire();
                }
            }
            
        });
        $A.enqueueAction(action);
    }
I am trying to get the attribute 'Claimed' to appear with a simple true or false but cant get it to work. 

Thanks for your help!

Tom 
 
Best Answer chosen by Tom Dawson
Tom DawsonTom Dawson
Hi All

I found a workaround for my situation. I add in a variable to the custom object called 'Checked at load' which assess whether the claimed box is checked at load. This sets the variable and then sends it over to the controller. 

The rest of the script runs as previously and goes ahead and ticks the box, so next time that record is loaded it will know that the record has been claimed.

Thanks for the help everyone :)

Tom

All Answers

imrohitimrohit

Hey Tom,

Since you are locally creating the claimed variable in your apex class and you are returning the only foundpermint which is the instance of your Senior_Parking_Permit__c object, so that is why you can't access the updated value of the claimed variable which you are doing in line 24 and 28 of your code 

So if you want to get the updated values of the claimed variable you to modify the line of your code as below

if(foundPermit.Claimed__c == true){
                
                foundPermit.Claimed__c = true;
            }
            else {
               foundPermit.Claimed__c = false;
            }
 

thanks

hope this help 

you can ask your questions if you have any

imrohitimrohit

and also modify your line 40 in js controller like below

component.set("v.permitClaimed",permitData.Claimed__c);
Tom DawsonTom Dawson
Hi Likesh

Thanks for the quick reply! 

The problem that i have is that later down on the script i'm updating the Claimed__c variable to true, which will overwrite this. I want something that will go back to the controller to show the value of Claimed__c before i update it. Is it possible to create a varible in APEX and access it in the controller?

Thanks

Tom 
imrohitimrohit

If that is the case then you  need to create a wrapper class and return its obj

I tried to modify your code hope this help 

public yourClass{
	public class WrapperClass{
		@AuraEnabled Senior_Parking_Permit__c seniorParkingPermit;
		@AuraEnabled Boolean claimed;
		public WrapperClass(Senior_Parking_Permit__c seniorParkingPermit, Boolean claimed){
			this.seniorParkingPermit = seniorParkingPermit;
			this.claimed = claimed;
		}
	}
	@AuraEnabled
    public static WrapperClass checkPermit(String permitNumber){
       WrapperClass wrapperObj = new WrapperClass();
        system.debug('Hit the server');
        system.debug(permitNumber);
        
        User currUser = getCurrPortalUser();
        system.debug('currUser =  ' + currUser );
         
        if (currUser.ContactId == null ) return null;
        
        List<Senior_Parking_Permit__c> foundPermits = [SELECT Id, Name, Permit_Number__c, Claimed__c FROM Senior_Parking_Permit__c 
                                                               WHERE Permit_Number__c = :permitNumber
                                                               LIMIT 1];
        
        if(foundPermits.size() == 0) {
            system.debug('Could not find any matching Senior_Parking_Permit__c rec, do nothing ');
            
            return null;
        }
        else{
            //Senior_Parking_Permit__c foundPermit = foundPermits[0];
            wrapperObj.Senior_Parking_Permit__c = foundPermits[0];
            if( wrapperObj.Senior_Parking_Permit__c.Claimed__c == true){
                
                wrapperObj.Claimed = true;
            }
            else {
                wrapperObj.Claimed = false;
            }
            
            //system.debug('Found a Senior_Parking_Permit__c record - ' + foundPermit);
            //system.debug('... and it hasnt been claimed yet, lets claim it. ');
            
            //.Claimed__c = true;
            wrapperObj.Senior_Parking_Permit__c.Claimed__c = true;
            //system.debug('Before update foundPermit  - ' + foundPermit);
            
            //update foundPermit;
           update wrapperObj.Senior_Parking_Permit__c;
            //return foundPermit;
        }
        return wrapperObj;
    }
}
 
permitClaim : function(component, event, helper){
        var action = component.get("c.checkPermit");
        var permitNumber = component.get("v.simpleNewCase.Permit_Number__c");
        
        console.log("permitNumber",permitNumber);
        
        action.setParams({
            permitNumber : permitNumber
        });
        
        action.setCallback(this, function(a) {
            
            var state = a.getState();
            console.log("State",state);
            var permitData = a.getReturnValue(); 
            console.log("permitData",permitData);
            
            if(permitData === null){
                debugger;
                var permitNotFound = true;
                component.set("v.permitNotFound",true);
                console.log(permitNotFound);
            }
            
            if(a.getState() === 'SUCCESS'){
                
                if(permitData != null){
                    debugger;
                //updated lines
                component.set("v.foundPermit",permitData.seniorParkingPermit.Permit_Number__c);
                component.set("v.permitClaimed",permitData.seniorParkingPermit.claimed);
                //updated lines    
                var permitFound = component.get("v.foundPermit");
                console.log("permitNumber",permitNumber);
                    
                var permitClaimed = component.get("v.permitClaimed");
                console.log("permitClaimed",permitClaimed);
                    
                var permitNotFound = false;
                component.set("v.permitNotFound",false);
                console.log(permitNotFound);
                    
                }
                
                if((permitFound == permitNumber) && (permitClaimed === false)){
                    var showToast = $A.get("e.force:showToast");
                    console.log("showToast")
                    showToast.setParams({
                        "title": "Permit Found",
                        "type" :"Success",
                        "message": "Your existing parking permit has been found."
                    });
                    showToast.fire();
                } else if ((permitFound == permitNumber) && (permitClaimed === true)){
                    
                    var showToast = $A.get("e.force:showToast");
                    console.log("showToast")
                    showToast.setParams({
                        "title": "Permit Already Claimed",
                        "type" :"error",
                        "message": "The parking permit number you have selected has already been claimed. If this has been claimed incorrectly, please contact Customer Services."
                    });
                    showToast.fire();
                } else if(permitNotFound === true){
                    var showToast = $A.get("e.force:showToast");
                    console.log("showToast")
                    showToast.setParams({
                        "title": "Permit Not Found",
                        "type" :"error",
                        "message": "The parking permit number you have entered has not been recognised. Please try again."
                    });
                    showToast.fire();
                }
            }
            
        });
        $A.enqueueAction(action);
    }

ask if you have any concerns
thanks
PabitraPabitra
Hello Tom Dawson,

In Apex method else blok you have declared,
Senior_Parking_Permit__c foundPermit = foundPermits[0];
Please declare 'foundPermit ' out of method with Public scope.
Then only define the foundPermit withing else block.
Please let me if you are still facing problem after doing this.

Thanks!
Pabitra 
Tom DawsonTom Dawson
Hi Likesh & Pabitra

Again, thanks for the help.

Likesh, when using that method i get the following errors:
User-added image

Pabitra, what do you mean when you say to declare it out of method with Public scope? 

Thanks

Tom 
Tom DawsonTom Dawson
Hi All

I found a workaround for my situation. I add in a variable to the custom object called 'Checked at load' which assess whether the claimed box is checked at load. This sets the variable and then sends it over to the controller. 

The rest of the script runs as previously and goes ahead and ticks the box, so next time that record is loaded it will know that the record has been claimed.

Thanks for the help everyone :)

Tom
This was selected as the best answer
PabitraPabitra
Tom,
I mean to say that in Apex method what the value you are returning to JavaScript controller, that is local to this method only and not accessible to outside of class. That's why I was telling before returning this variable make sure that the variable is publicly accessible outside of class.So declare it as public outside of method.
Thanks!
Pabitra