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
sumit dsumit d 

batch to migrate caseComment from Zendesk to salesforce

Hi All ,
i have created a batch to migrate case Comment from zendesk to salesforce.
My batch is given below:-
/*
* Batch to upsert Zendesk Organizations into salesforce caseComments
*/
public class BatchZendeskToSalesforceComments implements Database.Batchable<sObject>,Database.AllowsCallouts, Database.Stateful {
    // start method to return the list of end points of Zendesk CaseComments
        string query;
    public Database.querylocator start(Database.BatchableContext BC){
        query = 'SELECT Id, Name,parentid,commentbody,ispublished ' +
                'FROM caseComment ' +
                'WHERE parentid = \'500m0000008psIS\'  ';
        return Database.getQueryLocator(query);
    }
    //execute method to upsert CaseComments
    public void execute( Database.BatchableContext BC, List<caseComment> comments){
        getComments();
    }
    public void finish(Database.BatchableContext BC){
        
    }
    public static List< caseComment > getComments(){
        
        HttpRequest req = new HttpRequest();
        req.setMethod( 'GET' );
        String username = System.Label.Zendesk_UserName;
        String password = System.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/14732/comments.json ' );
        Http binding = new Http();
        HttpResponse res = binding.send( req );
        
        Map<String, Object> results = ( Map<String, Object> )JSON.deserializeUntyped( res.getBody() );
        List<Object> lstComments = ( List<Object> )results.get( 'comments' );
        List<Map<String, Object>> commentsAtt = new List< Map< String, Object >>();
        for ( Object comment : lstComments ) {
            Map<String, Object> commentsAttributes = ( Map< String, Object >)comment;
            commentsAtt.add( commentsAttributes );
        }
        List< caseComment > listCommentToUpsert = new List< caseComment >();
        for( Map< String, Object> attMap : commentsAtt ){
            caseComment ct = new caseComment(); 
            ct.parentid = '500m0000008psIS';
            ct.commentbody = String.valueOf( attMap.get( 'body' ));
            ct.ispublished = true;
             listCommentToUpsert.add(ct);
            
        }
        return listCommentToUpsert;
     }
}
Can anyone help me out with this requirement?
any suggestions that i am using it right or am i missing something?
Paras PrajapatiParas Prajapati
Hi Sumit,
1.Your batch will always execute for single Case Records as you have hard coded your Case Id in Query String.
2.As you are not passing any criteria to fetch the records (Comments) from ZenDesk, it will alwasy gives you all the records from Comments.JSON

Pain Points
1.Find a strategy to link Comments to Case Records, how do you relate Case to Case Comments.
2.Is there any idefier in Zendesk Comments which actully highlight the, which case is reffered.
3.Once you know how you are going to relate the Case and Case Comments, then only you can look for Solution Approche.

Let me know this will help you in any case or not, or please write your comments to understand your requirement,

Regards,
Paras