0

I want to create an apex class from below json. I am using json2apex but i am not able to get proper wrapper class from it. Can any one help me on this. My Json:

{
  "id": "0Af2x000004Itv4",
  "validatedDeployRequestId": null,
  "deployOptions": null,
  "deployResult": {
    "id": "0Af2x000004Itv4CAC",
    "messages": null,
    "retrieveResult": null,
    "success": false,
    "checkOnly": true,
    "ignoreWarnings": false,
    "rollbackOnError": true,
    "status": "Failed",
    "numberComponentsDeployed": 1,
    "numberComponentsTotal": 2,
    "numberComponentErrors": 1,
    "numberTestsCompleted": 0,
    "numberTestsTotal": 0,
    "numberTestErrors": 0,
    "details": {
      "componentFailures": [
        {
          "componentType": "AssignmentRule",
          "fileName": "sdx_sourceDeploy_1590739049091/assignmentRules/Case.assignmentRules",
          "fullName": "Case.Standard",
          "id": null,
          "problem": "In field: AssignedTo - no User named [email protected] found",
          "success": false,
          "warning": false,
          "created": false,
          "changed": false,
          "deleted": false,
          "lineNumber": 4,
          "columnNumber": 21,
          "requiresProductionTestRun": false,
          "createdDate": "2020-05-29T07:57:36.000+0000",
          "knownPackagingProblem": false,
          "forPackageManifestFile": false,
          "problemType": "Error"
        }
      ],
      "componentSuccesses": [
        {
          "componentType": "",
          "fileName": "sdx_sourceDeploy_1590739049091/package.xml",
          "fullName": "package.xml",
          "id": null,
          "problem": null,
          "success": true,
          "warning": false,
          "created": false,
          "changed": true,
          "deleted": false,
          "lineNumber": null,
          "columnNumber": null,
          "requiresProductionTestRun": false,
          "createdDate": "2020-05-29T07:57:36.000+0000",
          "knownPackagingProblem": false,
          "forPackageManifestFile": true,
          "problemType": null
        },
        {
          "componentType": "AssignmentRules",
          "fileName": "sdx_sourceDeploy_1590739049091/assignmentRules/Case.assignmentRules",
          "fullName": "Case",
          "id": null,
          "problem": null,
          "success": true,
          "warning": false,
          "created": false,
          "changed": true,
          "deleted": false,
          "lineNumber": null,
          "columnNumber": null,
          "requiresProductionTestRun": false,
          "createdDate": "2020-05-29T07:57:36.000+0000",
          "knownPackagingProblem": false,
          "forPackageManifestFile": false,
          "problemType": null
        }
      ],
      "retrieveResult": null,
      "allComponentMessages": [
        {
          "componentType": "AssignmentRule",
          "fileName": "sdx_sourceDeploy_1590739049091/assignmentRules/Case.assignmentRules",
          "fullName": "Case.Standard",
          "id": null,
          "problem": "In field: AssignedTo - no User named [email protected] found",
          "success": false,
          "warning": false,
          "created": false,
          "changed": false,
          "deleted": false,
          "lineNumber": 4,
          "columnNumber": 21,
          "requiresProductionTestRun": false,
          "createdDate": "2020-05-29T07:57:36.000+0000",
          "knownPackagingProblem": false,
          "forPackageManifestFile": false,
          "problemType": "Error"
        },
        {
          "componentType": "",
          "fileName": "sdx_sourceDeploy_1590739049091/package.xml",
          "fullName": "package.xml",
          "id": null,
          "problem": null,
          "success": true,
          "warning": false,
          "created": false,
          "changed": true,
          "deleted": false,
          "lineNumber": null,
          "columnNumber": null,
          "requiresProductionTestRun": false,
          "createdDate": "2020-05-29T07:57:36.000+0000",
          "knownPackagingProblem": false,
          "forPackageManifestFile": true,
          "problemType": null
        },
        {
          "componentType": "AssignmentRules",
          "fileName": "sdx_sourceDeploy_1590739049091/assignmentRules/Case.assignmentRules",
          "fullName": "Case",
          "id": null,
          "problem": null,
          "success": true,
          "warning": false,
          "created": false,
          "changed": true,
          "deleted": false,
          "lineNumber": null,
          "columnNumber": null,
          "requiresProductionTestRun": false,
          "createdDate": "2020-05-29T07:57:36.000+0000",
          "knownPackagingProblem": false,
          "forPackageManifestFile": false,
          "problemType": null
        }
      ],
      "runTestResult": {
        "successes": [],
        "failures": [],
        "totalTime": 0,
        "apexLogId": null,
        "flowCoverage": [],
        "codeCoverage": [],
        "numFailures": 0,
        "codeCoverageWarnings": [],
        "flowCoverageWarnings": [],
        "numTestsRun": 0
      }
    },
    "createdDate": "2020-05-29T07:57:29.000+0000",
    "startDate": "2020-05-29T07:57:29.000+0000",
    "lastModifiedDate": "2020-05-29T07:57:37.000+0000",
    "completedDate": "2020-05-29T07:57:37.000+0000",
    "errorStatusCode": null,
    "errorMessage": null,
    "stateDetail": null,
    "createdBy": "0052x000001h6yF",
    "createdByName": "M Krishna",
    "canceledBy": null,
    "canceledByName": null,
    "runTestsEnabled": false,
    "runTestResult": null,
    "done": true
  }
}

Wrapper class generated by jsonapex:

public class GitDeployHistroryWrapper {

    public class Details {
        public List<ComponentFailures> componentFailures  {get;set;} 
        public List<ComponentSuccesses> componentSuccesses  {get;set;} 
        public String retrieveResult  {get;set;} 
        public List<ComponentFailures> allComponentMessages  {get;set;} 
        public RunTestResult runTestResult  {get;set;} 
    }

    public String id  {get;set;} 
    public String validatedDeployRequestId  {get;set;} 
    public String deployOptions  {get;set;} 
    public DeployResult deployResult  {get;set;} 

    public class DeployResult {
        public String id  {get;set;} 
        public String messages  {get;set;} 
        public String retrieveResult  {get;set;} 
        public Boolean success  {get;set;} 
        public Boolean checkOnly  {get;set;} 
        public Boolean ignoreWarnings  {get;set;} 
        public Boolean rollbackOnError  {get;set;} 
        public String status  {get;set;} 
        public Integer numberComponentsDeployed  {get;set;} 
        public Integer numberComponentsTotal  {get;set;} 
        public Integer numberComponentErrors  {get;set;} 
        public Integer numberTestsCompleted  {get;set;} 
        public Integer numberTestsTotal  {get;set;} 
        public Integer numberTestErrors  {get;set;} 
        public Details details  {get;set;} 
        public String createdDate {get;set;} 
        public String startDate {get;set;} 
        public String lastModifiedDate {get;set;} 
        public String completedDate {get;set;} 
        public String errorStatusCode {get;set;} 
        public String errorMessage {get;set;} 
        public String stateDetail {get;set;} 
        public String createdBy {get;set;} 
        public String createdByName {get;set;} 
        public String canceledBy {get;set;} 
        public String canceledByName {get;set;} 
        public Boolean done {get;set;} 
        public String runTestResult {get;set;} 
        public Boolean runTestsEnabled {get;set;} 
    }

    public class Successes {
    }

    public class RunTestResult {
        public List<Successes> successes {get;set;} 
        public List<Successes> failures {get;set;} 
        public Double totalTime {get;set;} 
        public String apexLogId {get;set;} 
        public Integer numTestsRun {get;set;} 
        public List<Successes> flowCoverage {get;set;} 
        public List<Successes> codeCoverage {get;set;} 
        public Integer numFailures {get;set;} 
        public List<Successes> codeCoverageWarnings {get;set;} 
        public List<Successes> flowCoverageWarnings {get;set;} 
    }

    public class ComponentFailures {
        public String componentType {get;set;} 
        public String fileName {get;set;} 
        public String fullName {get;set;} 
        public String id {get;set;} 
        public String problem {get;set;} 
        public Boolean success {get;set;} 
        public Boolean warning {get;set;} 
        public Boolean created {get;set;} 
        public Boolean changed {get;set;} 
        public Boolean deleted {get;set;} 
        public Integer lineNumber {get;set;} 
        public Integer columnNumber {get;set;} 
        public Boolean requiresProductionTestRun {get;set;} 
        public String createdDate {get;set;} 
        public Boolean knownPackagingProblem {get;set;} 
        public Boolean forPackageManifestFile {get;set;} 
        public String problemType {get;set;} 
    }
    public class ComponentSuccesses{
    }

    
    public static GitDeployHistroryWrapper parse(String json) {
        return (GitDeployHistroryWrapper) System.JSON.deserialize(json, GitDeployHistroryWrapper.class);
    }
}

Class to trigger api:

public String getHistory(){

String endpoint='https://gitpipeline-dev-ed.my.salesforce.com/services/oauth2/token';

String username = ''; 
String password = '';
String ClientId= 'z';
String ClientSecret = '';
String secToken='';
String pwdToken=password+secToken;  
String depStatus;

Httprequest req = new HttpRequest();    
req.setMethod('POST');    
//req.setHeader('Content-Type','application/json');

req.setBody('grant_type=password + &client_id=' + ClientId + '&client_secret=' + ClientSecret + '&username=' + username +'&password=' + pwdToken); 

req.setEndpoint(endpoint);
//req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
Http http = new Http();
HttpResponse res;       
String Access_Token;
try {
res = http.send(req);                
system.debug('body:'+res.getBody());  
JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token')) {
            parser.nextToken();
            Access_Token = parser.getText();    
            } 
    }            
}catch(system.CalloutException e){            
system.debug('error'+e);
}
system.debug('access token'+Access_Token);
Httprequest req1 = new HttpRequest();  
req1.setEndpoint('https://gitpipeline-dev-ed.my.salesforce.com/services/data/v48.0/metadata/deployRequest/0Af2x000004Itv4?includeDetails=true');  
req1.setMethod('GET');    
req1.setHeader('Content-Type','application/x-www-form-urlencoded');
req1.setHeader('Authorization','Bearer '+Access_Token);            
Http http1 = new Http();
HttpResponse res1 = http1.send(req1);                 
system.debug('body11==='+res1.getBody()); 

GitDeployHistroryWrapper callList= (GitDeployHistroryWrapper)JSON.deserialize(res.getBody(), GitDeployHistroryWrapper.class);
system.debug('callList-----'+callList);

return callList; }

I am not getting proper output by using below:

2
  • Works for me. Are you passing the right json? I don't see where the "id=login.salesforce.com/id/00D2x000004ubeDEAQ/0052x000001h6kKAAQ" part would come from. I deployed your class to my org and the json you provided was parsed correctly. Commented Jun 26, 2020 at 9:43
  • I have pasted my triggering class also. the id is coming from he access token variable. could u pls check.
    – Raj SF
    Commented Jun 26, 2020 at 10:27

1 Answer 1

2

To update each field to have format:

@AuraEnabled public $Type$ $fieldName$ { get; set; }

Aura/Lightning only works with @AuraEnabled fields, and {get;set;} allows you to convert your JSON to Apex

3
  • Thanks for your reply but even after mentioning { get; set; } i am getting the same output
    – Raj SF
    Commented Jun 26, 2020 at 9:40
  • Have you added @AuraEnabled?
    – ytiq
    Commented Jun 26, 2020 at 11:48
  • its resolved now.the issues was with my code. thanks
    – Raj SF
    Commented Jun 26, 2020 at 15:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .