+ Start a Discussion

How to add picklist value to a picklist field for a Record Type from Apex Class without using visualforce page


I have a custom object which stores Picklist Field API name,Object Name,picklist value to insert and the record type to which the picklist value get added.Then I have a scheduler class(Apex) which tries to update the picklist field and record type.But I'm facing problem as UpdateMetadata() call is asynchronous so I need to wait till the time the picklist field is updated,there after I can update the Record Type but I don't find any Sleep() type of method in apex.Also using a loop to wait does't seem to be a good Solution.

I also tried to achieve the same be updating the picklist as well as record type at a same time but It failed.

I am updating the salesforce metadata from within salesforce and cannot use visualforce for this.Using visualforce User would be require Modify All Data permission which canot be provided.

Please Help.


There is one solution do sleep in apex.
You have to do http callout in apex for that see below example function for that.

Put below function in your class code and call it directly there is a Integer Parameter which is in Seconds.
means how many second you want to sleep the code..
Ex: classname.sleep(10);

public static void sleep(Integer sleepSeconds) {
    Long startTS = System.currentTimeMillis();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://1.cuzillion.com/bin/resource.cgi?sleep=' + sleepSeconds);
    Http http = new Http();
    HTTPResponse res = http.send(req);
    Long duration = System.currentTimeMillis() - startTS;
    System.debug('Duration: ' + duration + 'ms');

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
Using this repeatedly is resulting in callout limit exception.Is there any other way?