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
bharath kumar 52bharath kumar 52 

Extracting string/value from return type object in apex

Hi All,

I am working on a requirement where a method returns value and that value is used by some other method. But the method is returning object from which i am not able to get any values. How can i convert the object to string? Tried type casting but i am not able to do it.
public static object getCandidates(string serviceId )
   {
        
                CKSW_SRVC.GradeSlotsService candidatesService =         new CKSW_SRVC.GradeSlotsService(getCandidatesSchedulePolicyId(), serviceId)  ;
                system.debug('elad'+candidatesService);
                CKSW_SRVC.AdvancedGapMatrix candidates = candidatesService.GetGradedMatrix(false);
                System.debug('candidates  :: '+candidates );
             
                return candidates;
   }


Debug log received :

//As you can observe the debug log i get is returning values with flower braces in between and not in the beginning so i am not able to extract values from it.

15:40:15:745 USER_DEBUG [1056]|DEBUG|candidates  :: AdvancedGapMatrix:[CPUTimeTook=301, CurrentTime=2017-01-20 10:10:00, ResourceIDToScheduleData={a0Z6C000000ClQuUAK=ResourceScheduleData:[AdditionalObjects=null, CurrentSlotsIndexInAB=0, Resource=CKSW_BASE__Resource__c:{Id=a0Z6C000000ClQnUAK, Name=Test_FT1, CKSW_BASE__Location__c=a0F6C0000008kO2UAI, CKSW_BASE__Contractor__c=false, CKSW_BASE__Active__c=true}, SchedulingOptions=()], a0Z6C000000ClRHUA0=ResourceScheduleData:[AdditionalObjects=null, CurrentSlotsIndexInAB=0, Resource=CKSW_BASE__Resource__c:{Id=a0Z6C000000ClRHUA0, Name=Field Tech 1, CKSW_BASE__Location__c=a0F6C0000008kO2UAI, CKSW_BASE__Contractor__c=false, CKSW_BASE__Active__c=true}, SchedulingOptions=()], a0Z6C000000ClRMUA0=ResourceScheduleData:[AdditionalObjects=null, CurrentSlotsIndexInAB=0, Resource=CKSW_BASE__Resource__c:{Id=a0Z6C000000ClRMUA0, Name=Field Tech 2, CKSW_BASE__Location__c=a0F6C0000008kO2UAI, CKSW_BASE__Contractor__c=false, CKSW_BASE__Active__c=true}, SchedulingOptions=()], a0Z6C000000DykdUAC=ResourceScheduleData:[AdditionalObjects=null, CurrentSlotsIndexInAB=0, Resource=CKSW_BASE__Resource__c:{Id=a0Z6C000000DykdUAC, Name=Field Tech 3, CKSW_BASE__Location__c=a0F6C0000008kO2UAI, CKSW_BASE__Contractor__c=false, CKSW_BASE__Active__c=true}, SchedulingOptions=()], a0Z6C000000E2DpUAK=ResourceScheduleData:[AdditionalObjects=null, CurrentSlotsIndexInAB=0, Resource=CKSW_BASE__Resource__c:{Id=a0Z6C000000E2DpUAK, Name=Field Tech 4, CKSW_BASE__Location__c=a0F6C0000008kO2UAI, CKSW_BASE__Contractor__c=false, CKSW_BASE__Active__c=true}, SchedulingOptions=()]}, Service=CKSW_BASE__Service__c:{Id=a0n6C0000009sURQAY, Name=S-0709, CKSW_BASE__Resource__c=a0Z6C000000DykdUAC, CKSW_BASE__Location__c=a0F6C0000008kO2UAI, CKSW_BASE__Early_Start__c=2017-01-12 12:15:00, CKSW_BASE__Due_Date__c=2017-01-13 21:35:00, CKSW_BASE__Start__c=2017-01-12 16:00:00, CKSW_BASE__Finish__c=2017-01-12 17:30:00, CKSW_BASE__Duration__c=1.50, CKSW_BASE__Duration_Type__c=Hours, CKSW_BASE__Geolocation__Latitude__s=37.806308, CKSW_BASE__Geolocation__Longitude__s=-77.852860, CKSW_BASE__Appointment_Start__c=2017-01-12 16:00:00, CKSW_BASE__Appointment_Finish__c=2017-01-12 18:00:00, CKSW_BASE__Same_Resource__c=false, CKSW_BASE__Same_Day__c=false}]


//I expect the ABOVE debug log to appear in the below format i.e which generally displays list values when debugged. 
15:40:15:750 USER_DEBUG [10]|DEBUG|List of acc(Account:{Id=0016C000003xBCDQA2, Name=Mintus, RecordTypeId=01236000000nZNeAAM}, Account:{Id=0016C000002ZtrgQAC, Name=test, RecordTypeId=01236000000nZNeAAM}, Account:{Id=0016C000002ZyIaQAK, Name=ACME, RecordTypeId=01236000000nZNeAAM}, Account:{Id=0016C000002t9mQQAQ, Name=Reliance, RecordTypeId=0126C00000005xXQAQ}, Account:{Id=0016C000002XuCxQAK, Name=First Services Provider Account, RecordTypeId=01236000000nZNeAAM})

Any suggestions/ solutions on this would be of great help.

Thanks and Regards,
Bharath
bob_buzzardbob_buzzard
It looks like you are expecting the sObjects to be formatted as JSON when they are converted to strings, but that won't be the case. You also shouldn't rely on the string representation as it isn't documented. Apex has built in JSON support so you can just execute JSON.serialize on an sobject or array.

You can find out all about the built in JSON support at:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm
bharath kumar 52bharath kumar 52
Hi Bob,

Thanks for the reply.
I tried many ways like casting and using json string
But im getting the following error.
FATAL_ERROR System.JSONException: Cannot serialize type from package: CKSW_SRVC.AdvancedGapMatrix
// Below is the code snippet from which i got the mentioned error

 jsonStr =  json.serialize(candidates);

 
Siddharth Mathur 1Siddharth Mathur 1
Hi Bharat,

Did you manage to resolve your problem? I am also getting exception - "System.JSONException: Cannot serialize type from package" I'm serializing class which is global but in a package.

Any help will be much appreciated.

Thanks. 
Meenu SankarMeenu Sankar

Hi, I am getting the same error. Has anyone got any resolution for the same?

System.JSONException: Cannot serialize type from package: