0

I have a requirement to create and update custom meta data using apex , I'm passing the required values from my lwc , though i have found no proper example of handling the error since enqueueDeployment only returns the id of the deployment and not if the deployment was a success or a failure.The handleresult does have result parameter but it says this method has to void type and not return any result.Here is the code i found on the internet and am using to create the metadata, any inputs would be helpful.

public class CreateUpdateMetadataUtility implements Metadata.DeployCallback {
public void handleResult(Metadata.DeployResult result,
                             Metadata.DeployCallbackContext context) {
        if (result.status == Metadata.DeployStatus.Succeeded) {
           system.debug('the result of the insertion is'+result);
            //return result.status;
        } else {
            system.debug('the error is'+result.status);
            //return result.errorMessage;
           // mDebugUntruncated(result.toString());
        }
    }

    
    public static string 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);
        CreateUpdateMetadataUtility callback = new CreateUpdateMetadataUtility();
        Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);
        return jobId;
    }
   
} 

1 Answer 1

1

Your callback method handleResult will be called automatically after Salesforce succeeded or failed to do your custom metadata type changes. It is your job to act upon the success or failure in this method.

Changing custom metadata types based on an interaction with a LWC is unusual, because users often expect a result of their actions straightaway after a button click. (The typical use case for changing custom metadata types is post-deployment scripts, where this is less of an issue.) This won't work here.

What you could do is create a custom notification to inform the user about the success or failure of the custom metadata type change. You would implement this starting right where you have put your System.debug's until now, in the callback method.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .