Create custome field in salesforce through apex code
@AuraEnabled(cacheable = true)
public static void generateField( String label , String apiname ,String url , String sesId) {
String metadata = '{"Metadata" : {"type" : "Text"'+
',"description" :"New Field From toolingApi"'+
', "inlineHelpText" : "","precision" : null,"label" : "'+label+'"'+
',"length" : 255,"required" : false},'+
'"FullName" : "Account.'+apiname+'"}';
String responseBody = createField(metadata,url,sesId);
}
public static String createField(String metadata,String url , String sesId) {
System.debug('metadata : ' + metadata);
System.debug('url : ' + url);
System.debug('sessionId : ' + sesId);
HttpRequest request = new HttpRequest();
request.setHeader('Authorization', 'Bearer ' + sesId);
request.setHeader('Content-Type', 'application/json');
request.setEndpoint(url+'/services/data/v46.0/tooling/sobjects/CustomField');
request.setMethod('POST');
request.setBody(metadata);
Http http = new Http();
HTTPResponse res = http.send(request);
System.debug('res: ' + res.getBody());
System.debug('Status: ' + res.getStatusCode());
if (res.getStatusCode() == 201) {
System.debug('Custom field created successfully.');
} else {
System.debug('Custom field creation failed. Response: ' + res.getBody());
}
return '';
}
Comments
Post a Comment