0

I want to create a salesforce app. That have trigger on user creation. I need to make it optional by including a checkbox on user creation page.(i.e : the trigger have to start only when my custom checkbox is selected).

How to add custom checkbox in "User creation" page programatically using Apex ?Is it possible or not ?

I tried with MetaData API as :

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.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'User.custom_create_field__c';
customField.label = 'Custom created field';
customField.defaultvalue = 'false';
customField.sharingModel = 'ReadWrite';
customField.type_x = 'Checkbox';
fields.add(customField);

MetadataService.AsyncResult[] results = service.create(fields);\\trows session timeout error here

But the last line throws session timeout error. Am I going in correct way to achieve my need? If so how to solve session timeout error?

1 Answers1

0

Why do you want to do this programmatically? What's the problem with adding the checkbox declaratively to the object and to the layouts?

By adding the field to the object metadata as you're doing here, you'll still need to manually add it to layouts to ensure it's available for users to check or uncheck on your user creation page. Adding a new field to all custom layouts is not a trivial exercise, believe me!

However, if you still want to go ahead with this, you should try a different approach. It looks very much to me as though you've taken inspiration from Andy Fawcett here:

https://andyinthecloud.com/2013/10/27/introduction-to-calling-the-metadata-api-from-apex/

You should see the IMPORTANT UPDATE section. The async Metadata API methods were deprecated some time ago, this is the pattern to follow now:

https://andyinthecloud.com/2014/08/14/apex-metadata-api-streamlined-and-simplified-for-summer14/

dyson
  • 866
  • 6
  • 12