-5
global class CasePRBugzilla_RestService{
    global class cpb
    {
        public string Batch;
    }
    global class RecordIDS
    {
        string vmstarid {get;set;}
        string gsid {get;set;}
    }

    global class ReadPRBugResponse
    {
        List<RecordIDS> PRDetails {get;set;} 
        List<RecordIDS> BugDetails {get;set;} 
        String responseMessage {get;set;}
        String status {get;set;}
    }

    if (response.getStatusCode() == 200) {
        /**
         * this line causes error not able to save it
         */
        ReadPRBugResponse bzResponse = JSON.deserialize(response.getBody(),ReadPRBugResponse.class);
        updatePRRecords(bzResponse.PRDetails);
    }


    public static void  updatePRRecords(List<RecordIDS> PRdetailsList) { 
        Set<String> gsids = new Set<String>();
        for (RecordIDS bzd:PRdetailsList) {
            gsids.add(bzd.gsid);
        }
        Map<Id, GSS_Case_BugZilla__c> PrRecordMap = new Map<Id, GSS_Case_BugZilla__c>( [ select id, VMstar_Id__c  from GSS_Case_BugZilla__c where id in:gsids]);
        for (RecordIDS  bzd : PRdetailsList) {
            PrRecordMap.get(bzd.gsid).VMstar_Id__c = bzd.vmstarid;

        }
        update PrRecordMap.values();
    }

It shows below error:

Compile Error: Illegal assignment from Object to CasePRBugzilla_RestService.ReadPRBugResponse

Response:

{
    "prDetails":[{"gsid":"pr1gsid","vmstarid":"pr1vmstarid"},{"gsid":" pr2gsid ","vmstarid":" pr2vmstarid "}],
    "bugDetails":[{"gsid":"bugzilla1gsid","vmstarid":" bugzilla1vmstarid "},{"gsid":" bugzilla2gsid","vmstarid":" bugzilla2vmstarid"}],
    "responseMessage":"",
    "status":""
}
  • 2
    I've downvoted because you've been around long enough that I expect you to be able to format your own questions sensibly. Your provided code also will not compile because of that if(response.statusCode == 200){..} hanging out in the middle of your class (instead of being in a method). – Derek F May 4 '19 at 12:51
4

If you take a look at the JSON class documentation, you'll see the following for the deserialize() method

deserialize(jsonString, apexType)

Deserializes the specified JSON string into an Apex object of the specified type.

Signature

public static Object deserialize(String jsonString, System.Type apexType)

Parameters

jsonString

Type: String
The JSON content to deserialize.

apexType

Type: System.Type
The Apex type of the object that this method creates after deserializing the JSON content.

Return Value

Type: Object

The important bit here is those last 2 lines. You're getting an Object as the return value.

Objects pretty much always need to be explicitly type-cast to be of any use.

Your code

ReadPRBugResponse bzResponse = JSON.deserialize(response.getBody(),ReadPRBugResponse.class);

does not do any type-casting. It's equivalent to

ReadPRBugResponse bzResponse = Object;

If you then look at the error message, it makes a lot of sense. "Illegal assignment from Object to CasePRBugzilla_RestService.ReadPRBugResponse"

The solution here is to type-cast the result of the deserialize call.

ReadPRBugResponse bzResponse = (ReadPRBugResponse)JSON.deserialize(response.getBody(),ReadPRBugResponse.class);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.