0

I wrote a little code that creates metadata custom type:

public class CreateUpdateMetadataUtils implements Metadata.DeployCallback {

public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext context) {
    if (result.status == Metadata.DeployStatus.Succeeded) {
        System.debug(' success : '+ result);
    } else {
        System.debug(' fail : '+ result);
    }
}

public static void createUpdateMetadata(String fullName, String label, Map<String, Object> fieldWithValuesMap){
    Metadata.CustomMetadata customMetadata =  new Metadata.CustomMetadata();
    customMetadata.fullName = fullName;
    customMetadata.label = label;

    for(String key : fieldWithValuesMap.keySet()){
        Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
        customField.field = key;
        customField.value = fieldWithValuesMap.get(key); 
        customMetadata.values.add(customField);
    }

    Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
    mdContainer.addMetadata(customMetadata);
    CreateUpdateMetadataUtils callback = new CreateUpdateMetadataUtils();
    Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);
}}

I ran it via "execute anonymous window" with this code:

Map<String, Object> maptest = new Map<String, Object>();
maptest.put('Is_Admin__c',true);
maptest.put('Profile_Full_Name__c','Admin');
maptest.put('Profile_Name__c','Admin');
maptest.put('Updated__c',false);
CreateUpdateMetadataUtils.createUpdateMetadata('Profile_Setting.TEST_SARA','TEST_SARA',maptest);

but nothing happened! :(

here is my log:

45.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO 08:11:30.34 (34107709)|USER_INFO|[EXTERNAL]|0054J000000vzkk||(GMT+01:00) Greenwich Mean Time (Europe/Dublin)|GMT+01:00 08:11:30.34 (34278345)|EXECUTION_STARTED 08:11:30.34 (34293422)|CODE_UNIT_STARTED|[EXTERNAL]|DeployWorker.enqueueApexCallback 08:11:30.34 (42528076)|HEAP_ALLOCATE|[72]|Bytes:3 08:11:30.34 (42713894)|HEAP_ALLOCATE|[77]|Bytes:152 08:11:30.34 (42744856)|HEAP_ALLOCATE|[342]|Bytes:408 08:11:30.34 (42789823)|HEAP_ALLOCATE|[355]|Bytes:408 08:11:30.34 (42810224)|HEAP_ALLOCATE|[467]|Bytes:48 08:11:30.34 (42920776)|HEAP_ALLOCATE|[139]|Bytes:6 08:11:30.34 (42951473)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:552 08:11:30.34 (43021331)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:68 08:11:30.34 (43113163)|SYSTEM_METHOD_ENTRY|[2]|DeployResult.DeployResult() 08:11:30.34 (43154492)|STATEMENT_EXECUTE|[2] 08:11:30.34 (43224544)|SYSTEM_METHOD_EXIT|[2]|DeployResult 08:11:30.34 (55291093)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (55339884)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (55820141)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.id|”0Af4J00000FqesASAR”|0x78e908ab 08:11:30.34 (55925653)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:15 08:11:30.34 (55942250)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (55994577)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.canceledBy|”000000000000000″|0x78e908ab 08:11:30.34 (56669706)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (56719433)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.canceledByName|null|0x78e908ab 08:11:30.34 (56853642)|HEAP_ALLOCATE|[50]|Bytes:5 08:11:30.34 (56894877)|HEAP_ALLOCATE|[56]|Bytes:5 08:11:30.34 (56911997)|HEAP_ALLOCATE|[64]|Bytes:7 08:11:30.34 (56936537)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (56946082)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (56979132)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.checkOnly|false|0x78e908ab 08:11:30.34 (57204445)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (57217384)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (57290246)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.completedDate|”2019-05-26T07:11:31.000Z”|0x78e908ab 08:11:30.34 (57324429)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:15 08:11:30.34 (57346383)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (57368225)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.createdBy|”0054J000000vzkk”|0x78e908ab 08:11:30.34 (67049917)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:19 08:11:30.34 (67083647)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (67172841)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.createdByName|””|0x78e908ab 08:11:30.34 (67246867)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (67257491)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (67306616)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.createdDate|”2019-05-26T07:11:30.000Z”|0x78e908ab 08:11:30.34 (67370125)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:136 08:11:30.34 (67430216)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (67539123)|SYSTEM_METHOD_ENTRY|[3]|DeployDetails.DeployDetails() 08:11:30.34 (67551483)|STATEMENT_EXECUTE|[3] 08:11:30.34 (67590403)|SYSTEM_METHOD_EXIT|[3]|DeployDetails 08:11:30.34 (67816706)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (67835706)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (67844783)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (68091282)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:332 08:11:30.34 (68151821)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:38 08:11:30.34 (68191369)|SYSTEM_METHOD_ENTRY|[2]|DeployMessage.DeployMessage() 08:11:30.34 (68200836)|STATEMENT_EXECUTE|[2] 08:11:30.34 (68218117)|SYSTEM_METHOD_EXIT|[2]|DeployMessage 08:11:30.34 (68672711)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (68697115)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (68732582)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.changed|false|0x68e2f535 08:11:30.34 (68761561)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (68781518)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.columnNumber|null|0x68e2f535 08:11:30.34 (68804928)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:14 08:11:30.34 (68813089)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (68833354)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.componentType|”CustomMetadata”|0x68e2f535 08:11:30.34 (68858094)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (68864847)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (68882702)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.created|false|0x68e2f535 08:11:30.34 (68905681)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (68913919)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (68950400)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.createdDate|”2019-05-26T07:11:30.925Z”|0x68e2f535 08:11:30.34 (68975876)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (68984373)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69001618)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.deleted|false|0x68e2f535 08:11:30.34 (69062842)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:43 08:11:30.34 (69110441)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69136700)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.fileName|”customMetadata/Profi (23 more) …”|0x68e2f535 08:11:30.34 (69161895)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:25 08:11:30.34 (69167695)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69190054)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.fullName|”Profile_Setting.TEST (5 more) …”|0x68e2f535 08:11:30.34 (69352956)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69374415)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.id|null|0x68e2f535 08:11:30.34 (69400367)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69418714)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.lineNumber|null|0x68e2f535 08:11:30.34 (69439711)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:107 08:11:30.34 (69447569)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69469289)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.problem|”Profile_Setting__mdt (87 more) …”|0x68e2f535 08:11:30.34 (69492317)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12 08:11:30.34 (69498075)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69510841)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.problemType|0x574894ed|0x68e2f535 08:11:30.34 (69534607)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (69541909)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69559598)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.success|false|0x68e2f535 08:11:30.34 (69681284)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (69700517)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (69746798)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (69756043)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69781997)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.changed|true|0x78871517 08:11:30.34 (69797954)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69815777)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.columnNumber|null|0x78871517 08:11:30.34 (69831852)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69847262)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.componentType|null|0x78871517 08:11:30.34 (69863177)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (69870216)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69890956)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.created|false|0x78871517 08:11:30.34 (69909951)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69915514)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69945195)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.createdDate|”2019-05-26T07:11:30.935Z”|0x78871517 08:11:30.34 (69962588)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (69969791)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (69986241)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.deleted|false|0x78871517 08:11:30.34 (70002294)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:11 08:11:30.34 (70012878)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (70031594)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.fileName|”package.xml”|0x78871517 08:11:30.34 (70045837)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:11 08:11:30.34 (70053285)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (70071190)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.fullName|”package.xml”|0x78871517 08:11:30.34 (70085817)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (70101261)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.id|null|0x78871517 08:11:30.34 (70115408)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (70130783)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.lineNumber|null|0x78871517 08:11:30.34 (70146727)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (70162541)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.problem|null|0x78871517 08:11:30.34 (70179737)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (70195382)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.problemType|null|0x78871517 08:11:30.34 (70211070)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (70218258)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (70235092)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.success|true|0x78871517 08:11:30.34 (70247583)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (70253358)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (70317327)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 08:11:30.34 (70343061)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12 08:11:30.34 (70528339)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.componentFailures|[{“changed”:false,”componentType”:”CustomMetadata”,”created”:false,”createdDate”:”2019-05-26T07:11:30.925Z”,”deleted”:false,”fileName”:”customMetadata/Profi (23 more) …”,”fullName”:”Profile_Setting.TEST (5 more) …”,”problem”:”Profile_Setting__mdt (87 more) …”,”problemType”:”0x574894ed”,”success”:false}]|0x14ca6dfc 08:11:30.34 (70586949)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 08:11:30.34 (70597253)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12 08:11:30.34 (70678789)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.componentSuccesses|[{“changed”:true,”created”:false,”createdDate”:”2019-05-26T07:11:30.935Z”,”deleted”:false,”fileName”:”package.xml”,”fullName”:”package.xml”,”success”:true}]|0x14ca6dfc 08:11:30.34 (70716566)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12 08:11:30.34 (70725259)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72299784)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.messages|[{“changed”:false,”componentType”:”CustomMetadata”,”created”:false,”createdDate”:”2019-05-26T07:11:30.925Z”,”deleted”:false,”fileName”:”customMetadata/Profi (23 more) …”,”fullName”:”Profile_Setting.TEST (5 more) …”,”problem”:”Profile_Setting__mdt (87 more) …”,”problemType”:”0x574894ed”,”success”:false},{“changed”:true,”created”:false,”createdDate”:”2019-05-26T07:11:30.935Z”,”deleted”:false,”fileName”:”package.xml”,”fullName”:”package.xml”,”success”:true}]|0x78e908ab 08:11:30.34 (72407738)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12 08:11:30.34 (72471339)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72506008)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.details|0x14ca6dfc|0x78e908ab 08:11:30.34 (72555526)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (72565548)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72600083)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.done|true|0x78e908ab 08:11:30.34 (72628116)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72653708)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.errorMessage|null|0x78e908ab 08:11:30.34 (72678430)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72702057)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.errorStatusCode|null|0x78e908ab 08:11:30.34 (72734052)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (72743354)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72762704)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.ignoreWarnings|false|0x78e908ab 08:11:30.34 (72791163)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (72801543)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72830343)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.lastModifiedDate|”2019-05-26T07:11:31.000Z”|0x78e908ab 08:11:30.34 (72858592)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 08:11:30.34 (72865927)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72891464)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.numberComponentErrors|1|0x78e908ab 08:11:30.34 (72910092)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 08:11:30.34 (72917272)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72938121)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.numberComponentsDeployed|0|0x78e908ab 08:11:30.34 (72955239)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 08:11:30.34 (72962481)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (72979183)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.numberComponentsTotal|1|0x78e908ab 08:11:30.34 (72999752)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (73006889)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (73024332)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.rollbackOnError|true|0x78e908ab 08:11:30.34 (73046811)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:56 08:11:30.34 (73053618)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (73076546)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.startDate|”2019-05-26T07:11:30.000Z”|0x78e908ab 08:11:30.34 (73095803)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (73112566)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.stateDetail|null|0x78e908ab 08:11:30.34 (73131948)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12 08:11:30.34 (73139164)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (73155078)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.status|0x6b7e690d|0x78e908ab 08:11:30.34 (73175542)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (73183144)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (73199846)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this.success|false|0x78e908ab 08:11:30.34 (73639377)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5 08:11:30.34 (73722541)|SYSTEM_METHOD_ENTRY|[8]|DeployCallbackQueueable.DeployCallbackQueueable() 08:11:30.34 (73735823)|STATEMENT_EXECUTE|[8] 08:11:30.34 (73757367)|SYSTEM_METHOD_EXIT|[8]|DeployCallbackQueueable 08:11:30.34 (73776273)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12 08:11:30.34 (73786438)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (73794409)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:3 08:11:30.34 (73814407)|METHOD_ENTRY|1|01p4J000008ljBX|CreateUpdateMetadataUtils.CreateUpdateMetadataUtils() 08:11:30.34 (73829123)|STATEMENT_EXECUTE|1 08:11:30.34 (73854360)|STATEMENT_EXECUTE|1 08:11:30.34 (73860901)|METHOD_EXIT|1|CreateUpdateMetadataUtils 08:11:30.34 (73873218)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 08:11:30.34 (73880833)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96 08:11:30.34 (73954687)|VARIABLE_SCOPE_BEGIN|[16]|this|Metadata.DeployCallbackQueueable|true|false 08:11:30.34 (74093585)|VARIABLE_ASSIGNMENT|[16]|this|{}|0x67a1ed58 08:11:30.34 (74114116)|VARIABLE_SCOPE_BEGIN|[16]|callback|Metadata.DeployCallback|true|false 08:11:30.34 (74148773)|VARIABLE_ASSIGNMENT|[16]|callback|{}|0xcf3a730 08:11:30.34 (74166045)|VARIABLE_SCOPE_BEGIN|[16]|result|Metadata.DeployResult|true|false 08:11:30.34 (74825409)|VARIABLE_ASSIGNMENT|[16]|result|{“canceledBy”:”000000000000000″,”checkOnly”:false,”completedDate”:”2019-05-26T07:11:31Z”,”createdBy”:”0054J000000vzkk”,”createdByName”:””,”createdDate”:”2019-05-26T07:11:30Z”,”details”:”0x14ca6dfc”,”done”:true,”id”:”0Af4J00000FqesASAR”,”ignoreWarnings”:false,”lastModifiedDate”:”2019-05-26T07:11:31Z”,”messages”:”0x5b1f3fc5″,”numberComponentErrors”:1,”numberComponentsDeployed”:0,”numberComponentsTotal”:1,”rollbackOnError”:true,”startDate”:”2019-05-26T07:11:30Z”,”status”:”0x6b7e690d”,”success”:false}|0x78e908ab 08:11:30.34 (98910844)|CODE_UNIT_FINISHED|DeployWorker.enqueueApexCallback 08:11:30.34 (99671137)|EXECUTION_FINISHED

enter image description here

  • numberComponentErrors is 1, apparently the deployment failed – kurunve May 26 at 21:15
2

Using Apex Metadata API, one can create two metadata types: page layouts and the records of custom metadata types. You can create Custom Metadata Type records NOT the Custom Metadata Type Metadata.

Firstly, one has to go to Salesforce Setup to create the Custom Metadata Type with all its fields. Then using Apex MetaData API one can create the records of newly created Metadata Type. I created a Custom Metadata Type named Profile_Setting__mdt in my org before calling the Apex Metadata API.

To verify whether the Custom Metadata Type Records are created or not, Please go to Setup->Develop-> Custom Metadata Type. Then click on the Manage Records to see newly created records.

The Trailhead module https://trailhead.salesforce.com/content/learn/modules/apex_metadata_api explains the concept very clearly.

The problem with the original code provided by the user is not in the modular pattern defined by the Salesforce. The below code is easy to understand and modular. The user was not getting any output because he did not create the **Custom Metadata Type declaratively using Salesforce Setup

Here is the working pattern based on the Salesforce Trailhead module on Apex Metadata Type.

Class Name - createUpdateMetadata

        public class createUpdateMetadata{

    public Metadata.CustomMetadata createCustomMetadata(){
       Metadata.CustomMetadata customMetadata =  new Metadata.CustomMetadata();
       customMetadata.fullName = 'Profile_Setting__mdt.TEST_SARA'; 
       customMetadata.label = 'Master Label' ;

       Metadata.CustomMetadataValue customField1 = new Metadata.CustomMetadataValue();   
       customField1.field = 'Is_Admin_c__c';
       customField1.value = true;
       customMetadata.values.add(customField1);

       Metadata.CustomMetadataValue customField2 = new Metadata.CustomMetadataValue();  
       customField2.field = 'Profile_Full_Name_c__c' ;
       customField2.value = 'Admin';
       customMetadata.values.add(customField2);

       Metadata.CustomMetadataValue customField3 = new Metadata.CustomMetadataValue(); 
       customField3.field = 'Profile_Name_c__c' ;
       customField3.value = 'Admin';
       customMetadata.values.add(customField3); 

       Metadata.CustomMetadataValue customField4 = new Metadata.CustomMetadataValue(); 
       customField4.field = 'Updated_c__c' ;
       customField4.value = false;
       customMetadata.values.add(customField4);

       return customMetadata;
    }

}

Class Name-DeployCustomMetadata

public class DeployCustomMetadata {

    // Create metadata container 
    public Metadata.DeployContainer constructDeploymentRequest() {

        Metadata.DeployContainer container = new Metadata.DeployContainer();

        // Add components to container 
        Metadata.CustomMetadata cust = new createUpdateMetadata().createCustomMetadata();        

        container.addMetadata(cust);
        return container;
    }

    // Deploy metadata
    public void deploy(Metadata.DeployContainer container) {
        // Create callback. 
        PostInstallCallback callback = new PostInstallCallback();

        // Deploy the container with the new components. 
        Id asyncResultId = Metadata.Operations.enqueueDeployment(container, callback);
    }
}

Execute Anonymous Code

DeployCustomMetadata cc = new DeployCustomMetadata();
Metadata.DeployContainer container =cc.constructDeploymentRequest();
cc.deploy(container);
  • Prolay, could you edit your answer to provide some clarity on what differs between this and the poster's code - that is, what is the actual problem that is being solved here? – David Reed May 27 at 18:15
  • Sure. @David. I am going to explain it. – Prolay Chaudhury May 28 at 4:29
  • Thank you!! Looks like you forgot to add this class: public class PostInstallCallback implements Metadata.DeployCallback { public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext context) { if (result.status == Metadata.DeployStatus.Succeeded) { System.debug(' success : '+ result); } else { System.debug(' fail : '+ result); } } } Right? – user1478469 May 28 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.