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
Ridhima Saxena 20Ridhima Saxena 20 

custom field using metadata API

Hii Team,
I am new to salesforce and my requirement is that i have created custom object dynamically so i am creating custom fields as well but when my object is being created then it is not creating fields.here is my code

@AuraEnabled
 public static string createobject(String objname)
{
    string labelname = objname; //put parameter into string
MetadataService.MetadataPort service = new MetadataService.MetadataPort();  
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

List<MetadataService.Metadata> fields = new List<MetadataService.Metadata>();
MetadataService.CustomObject  customobject = new MetadataService.CustomObject();
objname = objname.trim();
objname = objname.replace(' ', '_');
    //remove space from lead and tail
customobject.fullName = objname +'__c';
customobject.label = labelname;
customobject.pluralLabel = labelname;
customObject.nameField = new MetadataService.CustomField();
customobject.nameField.type_x = 'Text';
customobject.nameField.label = 'Dashboard Name';
customobject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
fields.add(customobject);


//Here below i am getting custom field how can i resolve it help me
MetadataService.CustomField customField3 = new MetadataService.CustomField();
customField3.fullName = objname +'.MenuSequence__c';                                 //parameterized objname
customField3.label = 'Menu Sequence';
customField3.type_x = 'Number';
customField3.precision = 18;
customField3.scale = 1;
customField3.unique = false;
system.debug('Menu Sequence-' +customField3);   
    
MetadataService.CustomField customField4 = new MetadataService.CustomField();
customField4.fullName = objname +'.Active__c';
customField4.label = 'Active';
customField4.defaultvalue = 'false';
customField4.type_x = 'Checkbox';
fields.add(customField3);
fields.add(customField4);
 service.createMetadata(fields);
 return objname;   


Please check and tell me my error ASAP
ShirishaShirisha (Salesforce Developers) 
Hi Ridhima,

Greetings!

Can you please provide us with the error message to help you accordingly.Also,I would suggest you to enable the debug logs to narrow down the code execution to easily figure out the issue using the below link:

https://help.salesforce.com/apex/HTViewHelpDoc?id=code_add_users_debug_log.htm

Also,please check the sample code in the below thread:

https://developer.salesforce.com/forums/?id=9060G000000UVldQAG

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Ridhima Saxena 20Ridhima Saxena 20
Hii @shirisha my value are becoming null and not creating fields on the creation of object.
here my fullname become null
reply ASAP