Salesforce Apex Development: Upsert Custom MetaData Records Using Apex Class

Поделиться
HTML-код
  • Опубликовано: 26 янв 2025

Комментарии • 17

  • @poonamjadhav8284
    @poonamjadhav8284 10 дней назад

    Really Helpful video 👍

  • @sathaponwongsiri203
    @sathaponwongsiri203 9 месяцев назад

    Why I can't use Metadata.Operations when deploy it error 'Variable does not exist: Operations'

    • @HSCodehub
      @HSCodehub  9 месяцев назад

      Write in full:
      Metadata.Operations.enqueueDeployment(container, null);

  • @nicolasezequiel
    @nicolasezequiel 4 месяца назад

    is it possible to do a destructive change of metadata records?

    • @HSCodehub
      @HSCodehub  4 месяца назад

      Yes, you can try with this
      content of destructiveChanges.xml

      CustomMetadataType.CustomMetadataTypeRecord
      CustomMetadata

  • @pulkitmalhotra9917
    @pulkitmalhotra9917 Год назад

    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?

    • @HSCodehub
      @HSCodehub  Год назад +1

      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.
      }
      }

  • @i4ali1
    @i4ali1 Год назад

    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?

    • @HSCodehub
      @HSCodehub  Год назад

      @i4ali1
      Yes, you can do that, obviously. No need for VScode setup.

  • @Shri0900
    @Shri0900 9 месяцев назад

    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

    • @HSCodehub
      @HSCodehub  9 месяцев назад

      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.

  • @sudarshanvr4800
    @sudarshanvr4800 Год назад

    Is it possible to do bulk update?

    • @HSCodehub
      @HSCodehub  Год назад

      You will have to design codes for all the Custom Metadata Types individually.

  • @sanjay3468z
    @sanjay3468z Год назад

    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.

    • @HSCodehub
      @HSCodehub  Год назад

      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.

    • @sanjay3468z
      @sanjay3468z Год назад

      @@HSCodehub cool thanks