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
ALEX YAN 7ALEX YAN 7 

apex job to add picklist values

Hello,I want to define a job to add picklist values every day,
when execute the job ,the field picklist values is not created,    there is something wrong?
The code is sample,

 // @future(callout = TRUE)
    public static void updatePicklistField( )
    {//TEST
        MetadataService.MetadataPort service = createService();
        MetadataService.CustomField customField = new MetadataService.CustomField();
        customField.fullName = 'Case.casetest__c'; // your object. fields name
         customField.label = 'casetest'; // label of field.
         customField.type_x = 'Picklist';  // type
      
        metadataservice.Picklist pt = new metadataservice.Picklist();
        pt.sorted= false;
        String day = string.valueOf(system.now().day());
         String hour = string.valueOf(system.now().hour());
        String minute = string.valueOf(system.now().minute()  );
        String second = string.valueOf(system.now().second());
        
        metadataservice.PicklistValue two = new metadataservice.PicklistValue();
        two.fullName= 'a'+second + '_' + minute + '_' + hour + '_' + day   ;// 'second1rd';
        two.default_x=false ;
        metadataservice.PicklistValue three = new metadataservice.PicklistValue();
        three.fullName= 'B'+second + '_' + minute + '_' + hour + '_' + day   ; //'third1rd';
        three.default_x=false ;
        pt.picklistValues = new list<metadataservice.PicklistValue>{two,three};
        customField.picklist = pt ;
          system.debug('add success ');
        List<MetadataService.SaveResult> results =
            service.updateMetadata(
                new MetadataService.Metadata[] { customField });
        handleSaveResults(results[0]);
         
    }
global class SyncProductSubSegment implements Schedulable  {
  
    global void execute(SchedulableContext SC) {
     
     try
     {
       //  AddSegment.updateSegmentValues();
        AddSegment.updatePicklistField();
        String day = string.valueOf(system.now().day());
        String month = string.valueOf(system.now().month());
        String hour = string.valueOf(system.now().hour());
        String minute = string.valueOf(system.now().minute() + 10);
        String second = string.valueOf(system.now().second());
        String year = string.valueOf(system.now().year());
        String strJobName = 'Job-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;
        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
        System.schedule(strJobName, strSchedule, new SyncProductSubSegment());
     }
        catch(Exception e)
      {
         
       system.debug('create Exception: ' + e.getMessage());
      } 
   }
}