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
Nikhil Garg 17Nikhil Garg 17 

static variables

Hi All,

This is My Code  
I need to access glist Type(id) in My postopptoslack() Method.
That's why I have made glist as Public But when I am Accessing glist In  My2() method it's showing value of Id.
But When I am try to accessing this glist variable in postopptoslack() method then it's Showing the value as Null. What Can I do to access this value in the method?.




public with sharing class PostToSlack {
   public static id glist;
@invocableMethod(label='post win opp to slack'
                description='posting deatils of opportunity'
                category='opportunity')
    public static void postopp(list<id> opp){
        system.debug('flow');
       system.debug('op'+opp[0]);     
        system.debug('oppid'+opp);
        PostToSlack.my2(opp[0]);
    }
   
    public static id my2(id gl){
            glist=gl;
        system.debug('methodlist'+glist+' '+gl);
        return gl;
    }
     static{
        system.debug('static'+glist);
    }
    @auraenabled(cacheable=true)
    public static void getlist(list<string> values1){
        PostToSlack.postopptoslack(values1);
    }
    public static void postopptoslack(list<string> values2){
        system.debug('this'+glist);
        string sn='';
         Date cd;
         decimal a;
         string n='';
        //string a='name';
        system.debug('opp'+values2);
        string q1='select ';
        for(integer i=0;i<values2.size();i++){
            q1+=values2[i]+',';
        }
        system.debug('g'+glist);
        q1+='id,Link__c from opportunity where id =:glist';
        
        list<opportunity> oplist=database.query(q1);
        system.debug('oplist'+oplist);

        if(!oplist.isEmpty()){
            string mssg='';
           
            for(opportunity op:oplist){
                
                for(integer i=0;i<values2.size();i++){
                   
                    String var =values2[i];
                    if(var=='stagename')
                    {
                        sn=op.StageName;  
                        mssg+='StageName ='+' '+sn+'\n';
                    }
                    else if(var=='amount'){
                        a=op.Amount;
                        mssg+='Amount ='+' '+a+'\n';
                    }
                    else if(var=='closeDate'){
                        cd=op.CloseDate;
                        mssg+='CloseDate ='+' '+cd+'\n';
                    }
                    else if(var=='name'){
                        n=op.Name;
                        mssg+='Name ='+' '+n+'\n';
                    }
                
               
                }
          
                }
            system.debug('message'+mssg);
            Http http=new Http();
            HttpRequest req=new HttpRequest();            
            req.setEndpoint('https://hooks.slack.com/services/xKdM9Mk29hFhhUcH2Khbkydf');
            req.setMethod('POST');
            req.setHeader('content-Type', 'application/json;charset=UTF-8');
            req.setBody('{"text":"'+ mssg+ '"}');
            
            try{
            HttpResponse response=http.send(req);
                if(response.getStatusCode()==200){
                    system.debug('success');
                }
                else{
                    system.debug('Error');
                }
            }
            catch(exception e){
                system.debug('Error'+e.getMessage());
            }
        }
    }
}
Shubham Jain 338Shubham Jain 338
Hi,

Annotate glist with @AuraEnabled and it will work.

Please mark this as the best answer if it helps.

Thanks
Shubham Jain