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
JaanuJaanu 

Creating apex class reading json static resource file

1. I have created a Json file and uploaded into static resources in salesfoce. The file name is "xyz"
2. Converted the json file to apex class using - http://json2apex.herokuapp.com/.
3. I have the below structure of the class ... can someone validate this please ? it's not working for some reason .. pls give me the correct code.

basically 3 separate classes or ... do I need to nest these classes in the main class ? can you pls suggest on the structure of these classes.

global class preparemysandbox implements SandboxPostCopy {
     global void runApexClass(SanboxContext context) {
           Here I need to update the custom setting fields with the values from Json text files saved as static resource.
            e.g url (custom setting field) = (Here I need to assign value from static resource value)
      }
}

Public class JSON2Apex {
     Converted the json file to apex class using - http://json2apex.herokuapp.com/. I have all that here in this class.
}

Public static JSON2Apex parse(string cd) {
     StaticResource sr = [select id, body from StaticResource where name = 'xyz'; - I am getting error on Select, id and name on this line of code.
      cd = sr.ody.toString();
     return (JSON2Apex) System.JSON.deserialize(cd, JSON2Apex.class)
}
JaanuJaanu
I could resolve select by having SELECT and Id. please let me know how I should have these classes ? I am stuck .. any help is appreciated. thanks
jane1234jane1234

Hi jaanu , when using covertor to covert the json  to wrapper class. Please ensure the type of variables are correct . Some times this connectors put variables and unecessary line of code in between 

 

JaanuJaanu
thanks . that's not the issue .. all the classes should be inside ONE BIG class ..also some logic is wrong ... pls help
jane1234jane1234
please give the  wrapper class and  how you are deserialing the data 
JaanuJaanu
Please give me the code to udate any custom setting variable also .. thanks a lot. or if you have code to for similar requirement.. pls give me..I will modify mine based on yours. I need to basically update the custom settings based on the values in json file using apex. (if values change, I don't have to change the code.. I just update the json data file). 

global class PrepareMySandbox implements SandboxPostCopy {
    global void runApexClass(SandboxContext context) {
        System.debug('Org ID: ' + context.organizationId());
        System.debug('Sandbox ID: ' + context.sandboxId());
        System.debug('Sandbox Name: ' + context.sandboxName());
 run();
    }
    public class JSON2Apex {
        public class APIUrls__c {
            public String Provider_Base_URL__c;
            public String Banking_Base_URL__c;
            public String PlatformOrganizationId__c;
        }
        public class CustomLabel {
            public String URL_ePA_Endpoint__c;
            public String URL_ePA_iFrame_Endpoint__c;
        }
        public Dev dev;
        public Dev test;
        public Dev uat;
        public class Dev {
            public CustomSetting CustomSetting;
            public CustomLabel CustomLabel;
            public RemoteSiteSettings RemoteSiteSettings;
        }
        public class API_token_URL__c {
            public String client_secret__c;
            public String outbound_end_point_url__c;
            public String token_url__c;
        }
        public class RemoteSiteSettings {
        }
        public class CustomSetting {
            public API_token_URL__c API_token_URL__c;
            public APIUrls__c APIUrls__c;
        }
        public static JSON2Apex parse(String cd) {
            StaticResource sr= [SELECT Id, body from StaticResource WHERE Name = 'XYZ'];
            cd = sr.body.toString();
            return (JSON2Apex) System.JSON.deserialize(cd, JSON2Apex.class);
        }
    }
    global static void run() {
       
    }
}
 
JaanuJaanu
OR ...Give me the code for one variable update using the same "implements SandboxPostCopy " .. so, that I will build other logic on top of that please. Thanks again.
JaanuJaanu
Ssethal .. pls let me know. Or anyone else pls.