• ALEX YAN 7
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
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());
      } 
   }
}
Hello, I want to add new picklist values  through Metadata API for a particular recordtype. but the existing picklist values are removed when I assign new values .Can someone tell me how to do .Thank you very much

    public static void updatePicklistByRT()
    {    
        
         MetadataService.MetadataPort service = createService();
         MetadataService.RecordType rt = new MetadataService.RecordType();
          rt.active = true;
         // rt.fullName = 'Product2.Model';
         // rt.label = 'Model';
         // rt.businessProcess='Model';
            rt.fullName = 'Case.IHOST';
          rt.label = 'IHOST';
          rt.businessProcess='IHOST';
          // Add pick list values
        metadataservice.PicklistValue two = new metadataservice.PicklistValue();
        two.fullName= 'second1rd';
        two.default_x=false;
        metadataservice.PicklistValue three = new metadataservice.PicklistValue();
         three.fullName= 'third1rd';
        three.default_x=false;
 
    MetadataService.RecordTypePicklistValue ohmVersions = new MetadataService.RecordTypePicklistValue();
 //  ohmVersions.Picklist = 'Product_Segment__c';
    ohmVersions.Picklist = 'casetest__c';
   
     ohmVersions.values = new MetadataService.PicklistValue[] { two,three};
      rt.PicklistValues = new MetadataService.RecordTypePicklistValue[] { ohmVersions };

    List<MetadataService.SaveResult> results =
            service.updateMetadata(
                new MetadataService.Metadata[] { rt });
      handleSaveResults( results[0]);
    }
    
Hello,I want to add new values to picklist ,I import all class  which from https://github.com/financialforcedev/apex-mdapi  into my org.
I write a class named AddSegment.cls and  a test class  ,when I run test class to create Field ,it show error:Attempt to de-reference a null object, can someone tell me what wrong and what should I do.Thank you very much.
User-added image
User-added image

 
Hello,I want to add new values to picklist ,I import all class  which from https://github.com/financialforcedev/apex-mdapi  into my org.
I write a class named AddSegment.cls and  a test class  ,when I run test class to create Field ,it show error:Attempt to de-reference a null object, can someone tell me what wrong and what should I do.Thank you very much.
User-added image
User-added image