• Muskan Pradhan
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi

I need to cover below class. 

public class JWTDataAccess {
   
public static string getData(Query__c q, Map<string,string> FMD, Salesforce_Org__c org, String accessToken, String queryString, 

String QueryId, string TObject){
            HttpRequest req = new HttpRequest();
            req.setEndpoint(org.REST_endpoint__c +  queryString);
            req.setHeader('Authorization', 'OAuth ' + accessToken);
            req.setCompressed(true);    
            req.setMethod('GET');  
            Http http = new Http();  
            HTTPResponse res = http.send(req);  
            String input = res.getBody();
            //START ADDED DATA TO STAGING
                if (q.UseFieldMappings__c){
                    string TargetObject = TObject;
                    Map<string, string> fielddefinitionmap = FMD;
                    string s1 = input.substring(input.indexOf('['), 1 + input.indexOf(']'));
                    List<sobject> soblist = (List<sobject>)JSON.deserialize(s1, List<sobject>.class);
                    List<SObject> SobjRecords = new List<Sobject>(); 
                        for(SObject s: soblist){
                            Map<String, Object> fieldsToValue = s.getPopulatedFieldsAsMap();
                            sObject targetObj = Schema.getGlobalDescribe().get(TargetObject).newSObject() ;
                            for (String fieldName : fielddefinitionmap.keySet()){
                                string sourcefieldname = fieldName;
                                string targetfieldname = fielddefinitionmap.get(fieldName);
                                object o = fieldsToValue.get(fieldName);
                                if (o!=null)
                                {
                                string fieldvalue = fieldsToValue.get(fieldName).toString(); 
                                String x = (String)targetObj.put(targetfieldname, fieldvalue);
                                }
                                else
                                {
                                    String x = (String)targetObj.put(targetfieldname, fieldName);
                                }
                                }
                            SobjRecords.add(targetObj);}
                    String listType = 'List<' + TargetObject + '>';
                    List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
                    castRecords.addAll(SobjRecords);
                    upsert castRecords;}
            //END ADDED DATA TRANSFORM
            Map<String, object> o = (Map<String, object>) JSON.deserializeUntyped(input);
            string NewQS ='notrequired';
            if(o.get('nextRecordsUrl') != null){NewQS = o.get('nextRecordsUrl').tostring();}
            string resultsString =  input.substring(1, 1000);
            CallOut__c c = new CallOut__c(QueryName__c = QueryId, response__c ='Apex Job JWT Batch: ' + 'Call Out Result = ' + 

resultsString,nexturl__c = NewQS );
            insert c;
            Attachment attachment = new Attachment();
            attachment.Body = Blob.valueOf(input);
            attachment.Name = String.valueOf(q.Query_Name__c + '_'+ q.id + '_' + c.Id + '.txt');
            attachment.ParentId = c.Id;
            insert attachment;
            return NewQS;}}

Any kind of help would be appreciated.

Thanks!
Hi,

I'm getting System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY> error in Test Class. The response body that I have created is res.setBody('[{"attributes": {"type": "Account"},"name": "test"}]');
 This is the line of the main class where test class throws error.
Map<String, object> o = (Map<String, object>) JSON.deserializeUntyped(input);
Any help would be appreciated.
Thanks!!
Hi,

I'm getting System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY> error in Test Class. The response body that I have created is res.setBody('[{"attributes": {"type": "Account"},"name": "test"}]');
 This is the line of the main class where test class throws error.
Map<String, object> o = (Map<String, object>) JSON.deserializeUntyped(input);
Any help would be appreciated.
Thanks!!