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 issue:- Argument can not be null

Hi All, 
        i have a batch class in which i am migrating the users(Contact in salesforce) from Zendesk. so i created a batch and make an Api call to Zendesk to fetch All the contacts of Zendesk.
but when i run the batch the Apex job is showing Argument can not be null. what is the issue?
is it related to Contact's Account. i have some contact in Zendesk that dont have any parent Account.i am trying to debug but the debug log is blank.
Any suggestions how to solve the issue?
My Batch class is given below:-
public class BatchZendeskToSalesforceCon implements Database.Batchable<sObject>,Database.AllowsCallouts, Database.Stateful{
    public Database.querylocator start(Database.BatchableContext BC){
        string query;
        query = 'SELECT Id, Name,Email, Zd_url__c, External_Id__c, Custom_Role_id__c, Zd_ticket_restriction__c,'+
            'Zd_Organisation_id__c, Zd_report_csv__c, Zd_Suspended__c, locale__c, Zd_Tags__c,'+
            'Zd_two_factor_auth_enabled__c, Zd_verified__c, Active__c, Role_Type__c, Alias__c,'+
            'Moderator__c, Only_private_Comments__c, Zendesk_Id__c, Zd_default_group_id__c,'+
            'Zd_Restricted_Agent__c, phone, Zd_Shared__c, Notes__c, locale_id__c, Zd_time_zone__c,'+
            'Zd_Shared_phone_number__c, Zd_Shared_agent__c, Chat_only__c, Zd_Signature__c ' +
            'FROM Contact ';
        //System.assert( FALSE, query );
        return Database.getQueryLocator(query);
    }
    public void execute( Database.BatchableContext BC, List<Contact> Contacts){
        
        Set<Id> contactIds = new Set<Id>();
        for( Contact con : Contacts ){
            contactIds.add( con.Id );   
        }
        //System.assert(false, contactIds);
        getContacts();
    }
    
    public static void getContacts(){
        //ZendeskApis.ZendeskContactApi();
        HttpRequest req = new HttpRequest();
        req.setMethod( 'GET' );
        String username = 'kevin@sfdcconsultants.com';
        String password = '45j2JpHB^qy*';
        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/users.json' );
        Http binding = new Http();
        HttpResponse res = binding.send( req );
        
        Map<String, Object> results = ( Map<String, Object> )JSON.deserializeUntyped( res.getBody() );
        
       // system.assert(false,results);
        
        List<Object> lstUsers = ( List<Object> )results.get( 'users' );
        List<Map<String, Object>> customerAtt = new List< Map< String, Object >>();
        for ( Object customer : lstUsers ) {
            Map<String, Object> customerAttributes = ( Map< String, Object >)customer;
            customerAtt.add( customerAttributes );
        }
        List< Contact > listContactToUpsert = new List< Contact >();
        for( Map< String, Object> attMap : customerAtt ){
            Contact ct = new Contact();
            String fullName = String.valueOf( attMap.get( 'name' ));
            if( fullName.split(' ').size() > 1 ){
                ct.FirstName = fullName.substring( 0, fullName.indexOf(' ') );
                ct.LastName = fullName.substring( fullName.indexOf(' ') + 1 );
            }else{
                ct.LastName = fullName;
            }
            ct.Zendesk_Id__c = String.valueOf(attMap.get( 'id' ));
            System.debug('Zendesk id'+ct.Zendesk_Id__c);
            ct.email = String.valueOf( attMap.get( 'email' ));
                        System.debug('Zendesk id'+ct.email);

            ct.Zd_url__c =  String.valueOf( attMap.get( 'url' ));
                        System.debug('Zendesk id'+ct.Zd_url__c);

            ct.Zd_time_zone__c = String.valueOf(attMap.get('time_zone'));
                        System.debug('Zendesk id'+ct.Zd_time_zone__c);

            ct.Zd_iana_time_zone__c = String.valueOf( attMap.get( 'iana_time_zone' ));
                        System.debug('Zendesk id'+ct.Zd_iana_time_zone__c);

            ct.phone = String.valueOf( attMap.get( 'phone' ));
                        System.debug('Zendesk id'+ct.phone);

            ct.Zd_Shared_phone_number__c = Boolean.valueOf( attMap.get( 'shared_phone_number' ));
                        System.debug('Zendesk id'+ct.Zd_Shared_phone_number__c);

            ct.Photo__c = String.valueOf( attMap.get( 'photo' ));
                        System.debug('Zendesk id'+ct.Photo__c);

            ct.locale__c = String.valueOf( attMap.get( 'locale' ));
            System.debug('Zendesk id'+ct.locale__c);

            ct.locale_id__c =  Integer.valueOf(attMap.get('locale_id'));
            System.debug('Zendesk id'+ct.locale_id__c);

            ct.Zd_Organisation_id__c = String.valueOf( attMap.get( 'organization_id' ));
             System.debug('Zendesk id'+ct.Zd_Organisation_id__c);

            ct.Zd_role__c =  String.valueOf( attMap.get( 'role' ));
                System.debug('Zendesk id'+ ct.Zd_role__c);

            ct.Zd_verified__c = Boolean.valueOf( attMap.get( 'verified' ));
                 System.debug('Zendesk id'+ct.Zd_verified__c);

            ct.External_Id__c = String.valueOf( attMap.get( 'external_id' ));
                        System.debug('Zendesk id'+ct.External_Id__c);

            ct.Zd_Tags__c = String.valueOf(attMap.get('tags'));
                        System.debug('Zendesk id'+ct.Zd_Tags__c);

            ct.Alias__c = String.valueOf( attMap.get( 'alias' ));
                        System.debug('Zendesk id'+ct.Alias__c);

            ct.Active__c = Boolean.valueOf( attMap.get( 'active' ));
                        System.debug('Zendesk id'+ct.Active__c);

            ct.Zd_Shared__c = Boolean.valueOf( attMap.get( 'shared' ));
                        System.debug('Zendesk id'+ct.Zd_Shared__c);

            ct.Zd_Shared_agent__c = Boolean.valueOf( attMap.get( 'shared_agent' ));
                        System.debug('Zendesk id'+ct.Zd_Shared_agent__c);

            ct.Zd_last_login_at__c = Date.valueOf( attMap.get( 'last_login_at' ));
                        System.debug('Zendesk id'+ct.Zd_last_login_at__c);

            ct.Zd_two_factor_auth_enabled__c = Boolean.valueOf(attMap.get('two_factor_auth_enabled'));
                        System.debug('Zendesk id'+ct.Zd_two_factor_auth_enabled__c);

            ct.Zd_Signature__c = String.valueOf(attMap.get('signature'));
                        System.debug('Zendesk id'+ct.Zd_Signature__c);

            ct.Notes__c = String.valueOf( attMap.get( 'notes' ));
                        System.debug('Zendesk id'+ct.Notes__c);

            ct.Role_Type__c = Integer.valueOf( attMap.get( 'role_type' ));
                        System.debug('Zendesk id'+ct.Role_Type__c);

            ct.Custom_Role_id__c =  Integer.valueOf(attMap.get('custom_role_id'));
                        System.debug('Zendesk id'+ct.Custom_Role_id__c);

            ct.Moderator__c = Boolean.valueOf(attMap.get('moderator'));
                        System.debug('Zendesk id'+ct.Moderator__c);

            ct.Zd_ticket_restriction__c = String.valueOf(attMap.get('ticket_restriction'));
                        System.debug('Zendesk id'+ct.Zd_ticket_restriction__c);

            ct.Only_private_Comments__c = Boolean.valueOf( attMap.get( 'only_private_comments' ));
                        System.debug('Zendesk id'+ct.Only_private_Comments__c);

            ct.Zd_Restricted_Agent__c = Boolean.valueOf( attMap.get( 'restricted_agent' ));
                        System.debug('Zendesk id'+ct.Zd_Restricted_Agent__c);

            ct.Zd_Suspended__c = Boolean.valueOf( attMap.get( 'suspended' ));
                        System.debug('Zendesk id'+ct.Zd_Suspended__c);

            ct.Chat_only__c = Boolean.valueOf( attMap.get( 'chat_only' ));
                        System.debug('Zendesk id'+ct.Chat_only__c);

            ct.Zd_default_group_id__c = Integer.valueOf( attMap.get( 'default_group_id' ));
                        System.debug('Zendesk id'+ct.Zd_default_group_id__c);

            ct.Zd_report_csv__c = Boolean.valueOf( attMap.get( 'report_csv' ));
                        System.debug('Zendesk id'+ct.Zd_report_csv__c);

            
            listContactToUpsert.add(ct);
            /*if(ct.Name == 'testBC'){
               System.assert(false, listContactToUpsert);
              }*/
        }
        insert listContactToUpsert;
    }
    public void finish(Database.BatchableContext BC){
        
    }  
    
}
KuldeepNegiKuldeepNegi
Before getting values from attMap map, check if the attMap is not null.
SandhyaSandhya (Salesforce Developers) 
Hi,

I would suggest you debug the code for null values and use the condition !=NULL which would usually resolve this type of error.

Best Regards,
Sandhya
sumit dsumit d
Hi Sandhya and kuldeep,
    i did the same and now its showing me this error when i run the batch :-12:35:47:750 FATAL_ERROR System.TypeException: Invalid date: 2018-05-03T20:25:54Z.
how to solve this error?
Any suggestions?
KuldeepNegiKuldeepNegi
Hi Sumit,

I assume error is in below piece of code

 ct.Zd_last_login_at__c = Date.valueOf( attMap.get( 'last_login_at' ));
                     
The value that you are getting from Zendesk is Datetime and the field that you are assigning this value is Date. So you will have to convert Datetime to date. There are lot of exmpales to convert Datetime to Date

Hope this helps you.

Thanks
Kuldeep