Created
November 6, 2019 20:10
-
-
Save sfdcale/5dbb68c30baee2d4c66a913e238b578a to your computer and use it in GitHub Desktop.
This apex code is to create custom field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String sessionId = UserInfo.getOrganizationId()+''+UserInfo.getSessionId().SubString(15); | |
| List<String> objNames = new List<String>{'ConsumptionRate','ConsumptionSchedule','ProductConsumptionSchedule'}; | |
| for(String objectapiname: objNames){ | |
| String fieldapiname = 'External_Id';//replace with your field name | |
| String fieldlabel = 'External Id';//replace with your field label | |
| String fielddescription = 'External Id required for migration fro records between environments. Don\'t touch this field.'; | |
| String inlineHelpText = 'External Id required for migration fro records between environments. Don\'t touch this field.'; | |
| HttpRequest requestinside = new HttpRequest(); | |
| requestinside.setHeader('Authorization', 'Bearer ' + sessionId); | |
| requestinside.setHeader('Content-Type', 'application/json'); | |
| requestinside.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v41.0/tooling/sobjects/CustomField/'); | |
| requestinside.setMethod('POST'); | |
| String fieldDef = '{"Metadata" : '; | |
| String metadef = '"type" : "Number","description" : "'+fielddescription+'", "inlineHelpText" :"'+ inlineHelpText +'" ,"precision" : 18,"scale": 0,"label" : "'+fieldlabel+'","required" : false,"unique": true,"externalId": true'; | |
| fieldDef += '{'+metadef+'},'; | |
| fieldDef += '"FullName" : "'+objectapiname+'.'+fieldapiname+'__c"}'; | |
| system.debug(fieldDef); | |
| requestinside.setBody(fieldDef); | |
| Http httpObj = new Http(); | |
| HTTPResponse res = httpObj.send(requestinside); | |
| System.debug(res.getBody()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment