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
Harshala Godse 1Harshala Godse 1 

ERROR - showed -  FATAL_ERROR|System.NullPointerException: null argument for JSONGenerator.writeStringField()

SON ERROR - Null argument for JSONGenerator.writeStringField()
Here's my code for creating, I used this code to update JIRA from salesforce*****************




@RestResource(urlMapping='/test*****/*')
global without sharing class JiraIssueController {
    
    @future(callout=true)   
    global static void JiraIssueController(List<Id> rocStarId){
        //let's first query details on the recently created Jira Issue record
            list<ROC_Star__c> RocStars = [SELECT Id,Summary__c,Priority__c,Description__c,Type__c
                                         FROM ROC_Star__c
                                         WHERE ID IN :rocStarId];
        
        //let's instantiate a JSONGenerator and fill out details inside a For loop below
        String jiraSFDCCustomURLField = label.Jira_SFDC_URL_Custom_Field;
        JSONGenerator gen = JSON.createGenerator(true);
        for(ROC_Star__c rs : RocStars){
            String SForceURL = label.Jira_SForceURL + rs.id;
            system.debug('SForceURL: ' + SForceURL);
            gen.writeStartObject();
            gen.writeFieldName('fields');
            gen.writeStartObject();
            gen.writeFieldName('project');
            gen.writeStartObject();
            gen.writeStringField('key','ROC');
            gen.writeEndObject();
            gen.writeStringField(jiraSFDCCustomURLField,SForceURL);
            gen.writeStringField('summary',rs.Summary__c);
            gen.writeStringField('description', rs.Description__c);
            gen.writeFieldName('issuetype');
            gen.writeStartObject();
            gen.writeStringField('name',rs.Type__c);
            gen.writeEndObject();
            gen.writeFieldName('priority');
            gen.writeStartObject();
            gen.writeStringField('name',rs.Priority__c);
            gen.writeEndObject();
            gen.writeEndObject();
        }
        string s = gen.getAsString();
        system.debug(s);
        //now that our JSON message is ready to send to Jira
        //let's prepare an HTTPRequest and HTTPResponse to send via REST Api
        
        String username = label.Jira_Username; // username can be manipulated through Setup UI;
        String password = label.Jira_Token; // token can be manipulated through Setup UI 
        String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username + ':' + password));
        string url = label.Jira_IssueRestURL;
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept','application/json');
        req.setHeader('Authorization', auth_header);
        req.setHeader('X-Atlassian-Token', 'nocheck');
        req.setMethod('POST');
        req.setBody(s);
        req.setEndpoint(url);
        HttpResponse res = http.send(req); 
        system.debug('res ' + res);
        // Log the JSON content
        System.debug('******************JSON Response Start********************************');
        System.debug('JSON Response: ' + res.getBody()); 
        System.debug('******************JSON Response End***********************************');
        System.debug('response: ' + res);
        String JSONContent = res.getBody(); 
                
        Map<String,String> values = (Map<String,String>)
            JSON.deserialize(JSONContent, Map<String,String>.class);
        String key = values.get('key');
        String self = values.get('self');

        list<ROC_Star__c> RocStarsToUpdate = new list<ROC_Star__c>();
        for(ROC_Star__c rs : RocStars){
            rs.jira_key__c            = key;
            rs.Jira_Issue_Rest_URL__c = self;
            RocStarsToUpdate.add(rs);
            }
        Database.update(RocStarsToUpdate,false);
        
        }
    
    @future(callout=true)
    global static void JiraAssignIssue(list<id> rocStarsToUpdate){
        Map<String,String> jiraUserMap = new Map<String,String>();
            jiraUserMap.put('Cesar Bohorquez','5db75640fc11e40c2ff3f052');
            jiraUserMap.put('Sergio Gallegos','5dcf03a237249a0c693071c2');
            jiraUserMap.put('Anthony Jacovino','5df8f383eaf5880cad03734c');
            jiraUserMap.put('Joe Williams','5ced7740e2e3ec0fc196cf69');
            jiraUserMap.put('Michael Smith','5ec57e930363340c0f97faeb');        
        String jiraKey;
        //let's first query details on the recently created Jira Issue record
        list<ROC_Star__c> RocStars = [SELECT Id,Priority__c,Description__c,Type__c,Jira_Key__c,Assigned__r.Name
                                         FROM ROC_Star__c
                                         WHERE ID IN :rocStarsToUpdate
                                         LIMIT 1];
        //let's instantiate a JSONGenerator and fill out details inside a For loop below
        JSONGenerator gen = JSON.createGenerator(true);
        for(ROC_Star__c rs : RocStars){
            jiraKey = rs.Jira_Key__c;
            gen.writeStartObject();
            gen.writeStringField('accountId',jiraUserMap.get(rs.Assigned__r.Name));
            gen.writeEndObject();
        }
        system.debug(jiraKey);
        string s = gen.getAsString();
        system.debug(s);
        //now that our JSON message is ready to send to Jira
        //let's prepare an HTTPRequest and HTTPResponse to send via REST Api
        
        String username = label.Jira_Username; // username can be manipulated through Setup UI;
        String password = label.Jira_Token; // token can be manipulated through Setup UI 
        String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username + ':' + password));
        string url = label.Jira_IssueRestURL + jiraKey + '/assignee';
        
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept','application/json');
        req.setHeader('Authorization', auth_header);
        req.setHeader('X-Atlassian-Token', 'nocheck');
        req.setMethod('PUT');
        req.setBody(s);
        req.setEndpoint(url);
        HttpResponse res = http.send(req); 

        // Log the JSON content
        System.debug('******************JSON Response Start********************************');
        System.debug('JSON Response: ' + res.getBody()); 
        System.debug('******************JSON Response End***********************************');
        System.debug('response: ' + res);
        String JSONContent = res.getBody();        
        }
    
    @future(callout=true)
    global static void JiraUpdatePriority(list<id> rocStarsToUpdate){
        String jiraKey;
        //let's first query details on the recently created Jira Issue record
        list<ROC_Star__c> RocStars = [SELECT Id,Priority__c,Description__c,Type__c,Jira_Key__c,Assigned__r.Name
                                         FROM ROC_Star__c
                                         WHERE ID IN :rocStarsToUpdate
                                         LIMIT 1];
        //let's instantiate a JSONGenerator and fill out details inside a For loop below
        JSONGenerator gen = JSON.createGenerator(true);
        for(ROC_Star__c rs : RocStars){
            jiraKey = rs.Jira_Key__c;
            gen.writeStartObject();
            gen.writeFieldName('update');
            gen.writeStartObject();
            gen.writeFieldName('priority');
            gen.writeStartArray();
            gen.writeStartObject();
            gen.writeFieldName('set');
            gen.writeStartObject();
            gen.writeStringField('name', rs.Priority__c);      
            gen.writeEndObject();
            gen.writeEndObject();
            gen.writeEndArray();
            gen.writeEndObject();
            gen.writeEndObject();
        }
        system.debug(jiraKey);
        string s = gen.getAsString();
        system.debug(s);
        //now that our JSON message is ready to send to Jira
        //let's prepare an HTTPRequest and HTTPResponse to send via REST Api
        
        String username = label.Jira_Username; // username can be manipulated through Setup UI;
        String password = label.Jira_Token; // token can be manipulated through Setup UI 
        String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username + ':' + password));
        string url = label.Jira_IssueRestURL + jiraKey;
        
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept','application/json');
        req.setHeader('Authorization', auth_header);
        req.setHeader('X-Atlassian-Token', 'nocheck');
        req.setMethod('PUT');
        req.setBody(s);
        req.setEndpoint(url);
        HttpResponse res = http.send(req); 

        // Log the JSON content
        System.debug('******************JSON Response Start********************************');
        System.debug('JSON Response: ' + res.getBody()); 
        System.debug('******************JSON Response End***********************************');
        System.debug('response: ' + res);
        String JSONContent = res.getBody();        
        }
    
    @future(callout=true)
    global static void JiraUpdateStatus(list<id> rocStarsToUpdate){
        Map<String,integer> jiraStatusMap = new Map<String,integer>();
        integer backlogId    = integer.valueOf(label.JiraBacklogId);
        integer inProgressId = integer.valueOf(label.JiraInProgressId);
        integer doneId       = integer.valueOf(label.JiraDoneId);
        jiraStatusMap.put('Backlog',backlogId);
        jiraStatusMap.put('In Progress',inProgressId);
        jiraStatusMap.put('Done',doneId);
        
        String jiraKey;
        //let's first query details on the recently created Jira Issue record
        list<ROC_Star__c> RocStars = [SELECT Id,Status__c,Jira_Key__c
                                         FROM ROC_Star__c
                                         WHERE ID IN :rocStarsToUpdate
                                         LIMIT 1];
        //let's instantiate a JSONGenerator and fill out details inside a For loop below
        JSONGenerator gen = JSON.createGenerator(true);
        for(ROC_Star__c rs : RocStars){
            jiraKey = rs.Jira_Key__c;
            integer statusId = jiraStatusMap.get(rs.Status__c);
            gen.writeStartObject();
            gen.writeFieldName('transition');
            gen.writeStartObject();
            gen.writeNumberField('id', statusId);      
            gen.writeEndObject();
            gen.writeEndObject();
        }
        system.debug(jiraKey);
        string s = gen.getAsString();
        system.debug(s);
        //now that our JSON message is ready to send to Jira
        //let's prepare an HTTPRequest and HTTPResponse to send via REST Api
        
        String username = label.Jira_Username; // username can be manipulated through Setup UI;
        String password = label.Jira_Token; // token can be manipulated through Setup UI 
        String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username + ':' + password));
        string url = label.Jira_IssueRestURL + jiraKey + '/transitions';
               
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept','application/json');
        req.setHeader('Authorization', auth_header);
        req.setHeader('X-Atlassian-Token', 'nocheck');
        req.setMethod('POST');
        req.setBody(s);
        req.setEndpoint(url);
        HttpResponse res = http.send(req); 

        // Log the JSON content
        System.debug('******************JSON Response Start********************************');
        System.debug('JSON Response: ' + res.getBody()); 
        System.debug('******************JSON Response End***********************************');
        System.debug('response: ' + res);
        String JSONContent = res.getBody();        
        } 
    }
·
 
SwethaSwetha (Salesforce Developers) 
HI Harshala 
Recommend reviewing the approach followed for this error reported by another user from past

https://salesforce.stackexchange.com/questions/231062/null-argument-for-jsongenerator-writestringfield
https://salesforce.stackexchange.com/questions/310620/system-null-argument-for-json-generator-writestringfield
https://salesforce.stackexchange.com/questions/234508/system-nullpointerexception-null-argument-for-jsongenerator-writestringfield

If this information helps, please mark the answer as best. Thank you