+ Start a Discussion
ShreyankaShreyanka 

how to fix below error

Hi Everyone,
I am getting "CSRF with VF Call" checkmarx error from below code. So i have added get method to apex class as shown below. But I am not getting how to call get method in test class.

Please help me to include get method in test class.

Thanks in advance!
 
Apex class:


public class xyz {

        public abc objabc{get;set;}
        public Boolean sendEmail {get; set;}
        public Task objtask{get;set;}
        public boolean isChecked{get;set;}
    public xyz() {
        
        
        String objabcId = ApexPages.currentPage().getParameters().get('id');
                
            objabc = new abc();
        
             if(objabcId !=null && objabcId !=''){
                 
                 objabc = [Select id,OwnerId,name from abc 
                            where id=:ApexPages.currentPage().getParameters().get('id')];
             }
              
            if(objabc.id!=null){
                sendEmail = true;
                objtask= new task();
                objtask.whatid=objabc.id;
                objtask.RecordTypeId = [select Id,Name from RecordType where name='abcd' and SOBjectType='Task' limit 1].Id;
                objtask.status = 'Completed';
                objtask.type = 'Assigned';
                objtask.OwnerID = UserInfo.getUserId();
                objtask.Subject='Assignment';
            }
    }
	public abc getabc(){
		return objabc;
	}
	
      
        public pagereference save() {
            try {
                if(objtask.subject!=null){
                    insert objtask;
                    update objabc;
                    if(sendEmail){                       
                        Database.DMLOptions dlo = new Database.DMLOptions();
                        dlo.EmailHeader.triggerUserEmail = true;
                        database.update(objabc,dlo);                        
                    }
                }else{
                    Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,'Subject cannot be Null !!!'));
                    return null;
                }
                string abcobjurl = objabc.id;
                abcobjurl = abcobjurl.substring(0,3);
                PageReference orderPage = new PageReference('/' + abcobjurl);
                return orderPage;
            } catch (Exception e) {
                system.debug('---inside Exception---'+e.getMessage());
                system.debug('---inside Exception Line---'+e.getLineNumber());
                Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,e.getMessage()));
                return null;
            }
        }
        
}

Below code is the test class that i have written but not getting how to include get method in it.
"i just tried by calling it as Classname.methodname" getting error.
Test class:

@IsTest
Public class TestShareFilesWithCommunityUsers {
    @IsTest
    Public static void testmethod1(){
        Test.startTest();
        abc oAbc =new abc (Name='Demo');//Fill All Required Fields
        insert oAbc ; 
       ApexPages.currentPage().getParameters().put('id',oAbc .id);
       xyz abc= new xyz();
        abc.save();
        abc.objabc = oAbc;
        abc.sendEmail = true;
        Test.stopTest();
        
    }
}

 
AnkaiahAnkaiah (Salesforce Developers) 
try with below.
 
@IsTest
Public class TestShareFilesWithCommunityUsers {
    @IsTest
    Public static void testmethod1(){
        Test.startTest();
PageReference pageRef = Page.yourPageName;
        abc oAbc =new abc (Name='Demo');//Fill All Required Fields
        insert oAbc ; 

   Test.setCurrentPage(pageRef);
   pageRef.getParameters().put('id',oAbc.id);
   ApexPages.StandardController sc = new ApexPages.standardController(oAbc );

  
       xyz abc= new xyz(sc);
        abc.save();
        abc.objabc = oAbc;
        abc.getabc();
        abc.sendEmail = true;
        Test.stopTest();
        
    }
}

If this helps, Please mark it as best answer.

Thanks!!
ShreyankaShreyanka
Hi  Ankaiah,
Getting errors as below:

1) In test class for below lines getting error as "line 1: Final members can only be assigned in their declaration, init blocks, or constructors: objabc"
    and "line 2: Static method cannot be referenced from a non static context: abc xyz.getabc()"
abc.objabc=oAbc;
abc.getabc();

2) In apex code getting error as "line 2: Variable does not exist: objabc"
public abc getabc(){
		return objabc;
	}
Please help me to resolve this.


Thanks!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sheyanka,

 Static method cannot be referenced from a non static context: abc xyz.getabc()" to fix error, try with below.
 //Classname.methodname
xyz.save();

You need to declare the variable for objabc

If still facing an error, then send me the VF page code also.

Thanks!!


 
ShreyankaShreyanka
Hi Ankaiah,

1) If i use classname.methodname getting error "Non static method cannot be referenced from a static context: System.PageReference abc.save()"

2) where to declare the variable and how
 
VF page code:

<apex:page lightningStylesheets="true" Controller="abc" >
    
    <apex:form >
    <apex:pageblock id="PageBlock-Id" title="Select New Owner">
        <apex:pageBlockSection >
            <apex:outputfield value="{!objabc.Name}"/><br/>
            <apex:inputField value="{!objabc.OwnerId}"/>
                <br/>
            
            <apex:inputcheckbox label="Send Notification Email" value="{!sendEmail}" />
            
        </apex:pageBlockSection> 
        
        <apex:pageBlockSection title="Create Task and Log Hours" >
            <apex:pageMessages id="PageMsgId" /><br/>
            <apex:outputField value="{!objtask.RecordTypeId}"/>
            <apex:outputField value="{!objtask.Status}"/>
            <apex:inputField value="{!objtask.Priority}"/>
            <apex:outputField value="{!objtask.type}" />
            <apex:inputField value="{!objtask.Time_Logged__c}"/>
            <apex:inputField style="width:240px;" value="{!objtask.Description}"/>
        </apex:pageBlockSection> 
        
        <div align="center" draggable="false" >
            <apex:commandButton action="{!save}" value="Save" rerender="PageMsgId"/>
        </div>
    </apex:pageblock> 
    </apex:form> 
</apex:page>

Thanks!!