I have a similar requirement, but I don't need to update custom metadata "Checkbox field" in real time. I just need to mock that in apex test class, so what's the correct way of doing this?
I believe that is possible, but I didn't implement it. I did ask to ChatGpt, you may tweak the code below: @isTest private class TestMetadataUtility { @isTest static void testUpsertRecord() { // Create test data List testData = new List{ new HasanStudies__Information__mdt( MasterLabel = 'Test Label', DeveloperName = 'TestDeveloperName', HasanStudies__Valid__c = true ) }; insert testData; // Call the method to be tested MetadataUtility.upsertRecord(false); // Verify the custom metadata record and value List updatedData = [SELECT DeveloperName, HasanStudies__Valid__c FROM HasanStudies__Information__mdt WHERE MasterLabel = 'Test Label']; System.assertEquals(1, updatedData.size()); System.assertEquals('TestDeveloperName', updatedData[0].DeveloperName); List customMetadataList = [SELECT FullName, Label, values FROM Metadata.CustomMetadata WHERE FullName = 'HasanStudies__Information__mdt.TestDeveloperName']; System.assertEquals(1, customMetadataList.size()); List customMetadataValues = customMetadataList[0].values; System.assertEquals(1, customMetadataValues.size()); System.assertEquals('HasanStudies__Valid__c', customMetadataValues[0].field); System.assertEquals(false, customMetadataValues[0].value); // Note: In a real scenario, you might need to add more assertions based on your requirements. } }
hey found it helpful my requirement is to iupdate existing record's checkbox but insted its creating a new record and updating its field what can i do?thanks in advance
Hey, I see that this is deploying the code for each update, and we cant give deployment access for more number of people. is there any other way to do it without deployment. Thank you for the video, this was helpful.
Hi @sanjayprabhat3468, Thank you for your comment. You can always manually create the records from the Custom Metadata Object. And, apart from via deployment, I am not aware of any other options as of now.
Really Helpful video 👍
Why I can't use Metadata.Operations when deploy it error 'Variable does not exist: Operations'
Write in full:
Metadata.Operations.enqueueDeployment(container, null);
is it possible to do a destructive change of metadata records?
Yes, you can try with this
content of destructiveChanges.xml
CustomMetadataType.CustomMetadataTypeRecord
CustomMetadata
I have a similar requirement, but I don't need to update custom metadata "Checkbox field" in real time. I just need to mock that in apex test class, so what's the correct way of doing this?
I believe that is possible, but I didn't implement it. I did ask to ChatGpt, you may tweak the code below:
@isTest
private class TestMetadataUtility {
@isTest
static void testUpsertRecord() {
// Create test data
List testData = new List{
new HasanStudies__Information__mdt(
MasterLabel = 'Test Label',
DeveloperName = 'TestDeveloperName',
HasanStudies__Valid__c = true
)
};
insert testData;
// Call the method to be tested
MetadataUtility.upsertRecord(false);
// Verify the custom metadata record and value
List updatedData = [SELECT DeveloperName, HasanStudies__Valid__c FROM HasanStudies__Information__mdt WHERE MasterLabel = 'Test Label'];
System.assertEquals(1, updatedData.size());
System.assertEquals('TestDeveloperName', updatedData[0].DeveloperName);
List customMetadataList = [SELECT FullName, Label, values FROM Metadata.CustomMetadata WHERE FullName = 'HasanStudies__Information__mdt.TestDeveloperName'];
System.assertEquals(1, customMetadataList.size());
List customMetadataValues = customMetadataList[0].values;
System.assertEquals(1, customMetadataValues.size());
System.assertEquals('HasanStudies__Valid__c', customMetadataValues[0].field);
System.assertEquals(false, customMetadataValues[0].value);
// Note: In a real scenario, you might need to add more assertions based on your requirements.
}
}
thanks for this video. I don't have vscode setup. Instead can I add MetadataUtility as an Apex class and then call it from developer console?
@i4ali1
Yes, you can do that, obviously. No need for VScode setup.
hey found it helpful my requirement is to iupdate existing record's checkbox but insted its creating a new record and updating its field what can i do?thanks in advance
Please check the API names; if there is a record already, it won't create a new one. However, you need to make sure your inputs are consistent.
Is it possible to do bulk update?
You will have to design codes for all the Custom Metadata Types individually.
Hey, I see that this is deploying the code for each update, and we cant give deployment access for more number of people. is there any other way to do it without deployment.
Thank you for the video, this was helpful.
Hi @sanjayprabhat3468,
Thank you for your comment.
You can always manually create the records from the Custom Metadata Object. And, apart from via deployment, I am not aware of any other options as of now.
@@HSCodehub cool thanks