1

I have a trigger which calls a future method and the future method uses the ToolingAPI to execute something.But I dont know how to pass the session ID to the future method from the trigger.

trigger appTrigger on Applicant__c (after insert,after update) {
    public CustBusinessPro__Criteria__c Criteria {get;set;}
    system.debug('---------- hello');
    public List < CustBusinessPro__Criteria__c > criteriaList {get;set;}
    ToolingAPI.SforceService x = new ToolingAPI.SforceService();
    x.SessionHeader = new ToolingAPI.SessionHeader_element();
    x.SessionHeader.sessionId = UserInfo.getSessionId();
    String sessionId = UserInfo.getSessionId();
     String test;
    criteriaList=new LIST<CustBusinessPro__Criteria__c>([SELECT Id,Name,CustBusinessPro__Where_Clause__c FROM CustBusinessPro__Criteria__c] );
    for (CustBusinessPro__Criteria__c cd: criteriaList) {

    for(CustBusinessPro__Applicant__c app:Trigger.new)
    {

  System.debug('>>>>>>>>>>>>>>>>>'+ cd.CustBusinessPro__Where_Clause__c);
  test=cd.CustBusinessPro__Where_Clause__c;
  test=test.replaceAll('AND','&&');
  test=test.replaceAll('OR','||');

   test=test.replaceAll('CustBusinessPro__Age__c',String.valueOf(app.CustBusinessPro__Age__c));
    test=test.replaceAll('CustBusinessPro__Gender__c ',app.CustBusinessPro__Gender__c );
     test=test.replaceAll('CustBusinessPro__Country__c',app.CustBusinessPro__Country__c);
      test=test.replaceAll('CustBusinessPro__City__c',app.CustBusinessPro__City__c);
       test=test.replaceAll('CustBusinessPro__Salary__c',String.valueOf(app.CustBusinessPro__Salary__c));
        test=test.replaceAll('CustBusinessPro__Marital_Status__c',app.CustBusinessPro__Marital_Status__c);

              System.debug('>>>>>>>>>>>>>>>>>'+ test);

      String ifcondition = 'if('+test+' == true)';
      TriggerClass.ifMethod( sessionId,ifcondition);
}

This is the class which has the future method:

global  class TriggerClass{    
  @future (callout=true)
  Public static void ifMethod(String sessionId,String ifcondition){
  ToolingAPI.SforceService x = new ToolingAPI.SforceService();
  // x.SessionHeader = new ToolingAPI.SessionHeader_element();
  x.SessionHeader.sessionId = UserInfo.getSessionId();
  x.SessionHeader.sessionId = sessionId;
  System.debug('inside myMethod...'); 
  x.executeAnonymous(ifcondition);
  System.debug('Result of if-------------' +ifcondition);
  System.debug('exiting myMethod...'); 
 } 
}
5
  • You're already passing it, but the try to retrieve it again with UserInfo.getSessionId() thus not using the one you pass.... – Phil Hawthorn Mar 21 '16 at 12:48
  • I tried commenting that part and only using the passed ID , but it throws the following error: Attempt to dereference the null object. – Jyoti Zanwar Mar 21 '16 at 13:01
  • That's because you commented out the line which creates the SessionHeader element – Phil Hawthorn Mar 21 '16 at 13:04
  • Thankyou so much! That was so silly!!! Also I want to check the result of the executed if condition! how to do that? it would be a great help! – Jyoti Zanwar Mar 21 '16 at 17:25
  • As in I wish to execute some more code if the ifcondition evaluates to true, how do I do that? – Jyoti Zanwar Mar 21 '16 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.