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
Bhanu joshi 10Bhanu joshi 10 

How to get user session id , while making a callout using batch class?

I am calling a class that is using tooling API to update validation rule from BATCH CLASS.
I am getting user session id as null.

        req.setHeader('Authorization','Bearer '+UserInfo.getSessionID());   // here iam getting null
         req.setHeader('Content-Type', 'application/json');
         req.setHeader('Content-Length', '0');

and when i run class from developer console , all works fine.
Please let me know how to fix this ?
Thanks
om gupta(sfdc)om gupta(sfdc)
actually salesforce has stop to debug sessionid even you try to print in console also you will not get it. this is due to salesforce security issue.
Bhanu joshi 10Bhanu joshi 10

yes it has stoped to pront in debug,

but how to get session id if i want to make a http callout from batch Class

om gupta(sfdc)om gupta(sfdc)
i think you can call without printing session id. it will not print but it can work. other wise you have to use third party software such as java to get the access token by connected app and make as a service to call from salesforce to get the session id and then use that 
Bhanu joshi 10Bhanu joshi 10
it is giving error of invalid session id when i am using the following line:
        req.setHeader('Authorization','Bearer '+UserInfo.getSessionID());   

 
om gupta(sfdc)om gupta(sfdc)
may you try this req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
om gupta(sfdc)om gupta(sfdc)
ok just apply this and you will not get this issue
global class Test_Sched  implements Schedulable{
    private String sessionId;

    global Test_Sched( String sessionId ) {
        this.sessionId = sessionId;
    }

    global void execute(SchedulableContext sc) {

        //pass the session id to the class that call http and process the data in external apps
        staticclassprocess(sessionId)   ;      
    }
}

System.schedule('Test_Sched', '0 0 1-23 * * ?', new Test_Sched(UserInfo.getSessionId()));

hope this solve your problem . if it worked plz marked as a solved