+ Start a Discussion
sumit suwalkasumit suwalka 

batch to get zendesk ticket audits fields in salesforce task fields

Hi All,
         i am creating a batch to get all the ticket audits fields in task fields. i have created a batch below:-

global class BatchZendeskActivityToTask implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = ' SELECT Id, Zd_Zendesk_Id__c, Status ' +
                       ' FROM Case ' +
                       ' WHERE Zd_Zendesk_Id__c != Null '+
                       ' AND Id = \'500S000000AEXIEIA5\'';
        return Database.getQueryLocator(query);
    }

    global void execute( Database.BatchableContext BC, List<Case> caseRecords ){         
        
        for ( Case caseObj : caseRecords ){
            try {                  
                HttpRequest req = new HttpRequest();
                HttpResponse res = new HttpResponse();
                Http http = new Http();
                
                // Set values to Params
                String username = Label.Zendesk_UserName;
                String password = Label.Zendesk_Password;
                Blob headerValue = Blob.valueOf( username + ':' + password );
                String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode( headerValue );
                
                req.setHeader( 'Authorization', authorizationHeader );
                req.setHeader('Content-Type', 'application/json');
                req.setEndpoint( 'https://timerack.zendesk.com/api/v2/tickets/'+caseObj.Zd_Zendesk_Id__c+'/audits.json' );
                req.setMethod('GET');
                
        
                if( !Test.isRunningTest() ) {      
                    res = http.send( req );
                    String sJson = res.getBody();
                     TaskJSON2Apex obj = parseTaskData( sJson );
                    System.assert( false, obj );
                }             
                               
            }
            catch (Exception e) {         
                System.debug( 'Error:' + e.getMessage() + 'LN:' + e.getLineNumber() );           
            }
        }
    }   

    global void finish( Database.BatchableContext BC ){    
    
    }
    
    //method to return Activity in Task. 
    public static TaskJSON2Apex parseTaskData( String json ){
        System.assert( false, ( TaskJSON2Apex ) System.JSON.deserialize( json, TaskJSON2Apex.class ) );
        return ( TaskJSON2Apex ) System.JSON.deserialize( json, TaskJSON2Apex.class );
    }
    
    //Wrapper Class
    //wrapper class to fetch users data in salesforce contact
    public class TaskJSON2Apex {
        public List<Audits> audits {get;set;} 
        public Object next_page {get;set;} 
        public Object previous_page {get;set;} 
        public Integer count {get;set;} 
        
        TaskJSON2Apex(){
            System.assert( false, count );
        }
    }
    
    public class Audits {
        public Integer id {get;set;} 
        public Integer ticket_id {get;set;} 
        public String created_at {get;set;} 
        public Integer author_id {get;set;} 
        public List<Events> events {get;set;} 
    }
    
    public class Events {
        public Integer id {get;set;} 
        public String type {get;set;} // in json: type
        public Integer author_id {get;set;} 
        public String body {get;set;} 
        public String html_body {get;set;} 
        public String plain_body {get;set;} 
        public Integer audit_id {get;set;} 
        public Object value {get;set;} 
        public String field_name {get;set;} 
        public String subject {get;set;} 
        public List<Integer> recipients {get;set;} 
    }
 }
Can anyone guide me how to move ahead and get the ticket audits fields in salesforce task fields?let me know if i am missing something?