-1

I have a requirement whenever Opportunity Stage is moved to "closed won" I need to create a Task and assign it to Queue. I am getting regular requirement from users to create a Task with different Subject name. Instead of hard coding the subject I am trying to use Custom Metadata and store the values and retrieve it using the getInstance method, but I am unable to understand how can we assign the values in trigger. Below is the sample code and Custom Metadata.

public class UpdateOpportunity{
    public static void checkOpportunityStage(List<Opportunity> opportunityList){
        if(opportunityList.size() > 0){
            List<Task> taskList = new List<Task>();
            Task_Settings__mdt TaskDomain = Task_Settings__mdt.getInstance('Manage Product Service');
            System.debug('----->'+TaskDomain);
            for(Opportunity opp:opportunityList){
                if(opp.StageName == 'Closed Won'){
                    Task task1 = new Task();
                    task1.Subject = 'Follow Up Test Task';
                    task1.WhatId = opp.Id;
                    taskList.add(task1);                      
                }
            }
            if(taskList.size() > 0){
                insert taskList;
            }
        }
    }
}

enter image description here

2
  • Is there a way that we can use Map and retrieve it using the Key ?
    – SFDCSai
    Jun 15, 2022 at 15:17
  • why not use a flow ? A simple after create/update flow is just so much simpler. And if you need to retrieve values, you could use a custom label, or pull the values from your MDT.
    – Saariko
    Jun 29, 2022 at 10:21

2 Answers 2

2

Let's rewrite this to be cleaner and use getInstance() properly

public class UpdateOpportunity{
    public static void checkOpportunityStage(List<Opportunity> opportunityList){
        
      List<Task> taskList = new List<Task>();
      Task_Settings__mdt TaskDomain = Task_Settings__mdt
          .getInstance('Manage_Product_Service'); // Must be developerName of MDT row
            
      for(Opportunity opp:opportunityList){ // no need to check for empty
        if(opp.StageName == 'Closed Won'){
          taskList.add(new Task(
             Subject = taskDomain.Subject__c, // field in MDT w/ subjectName
             WhatId = opp.Id));
             
        }
      }
      insert taskList; // no need to check for empty, won't burn DML
      
    }
}

Key points

  • getInstance(val) returns a single row based on its DeveloperName column
  • If you have dynamic subjects based on some condition in the Opportunity, you'll need to fetch all relevant MDT rows (SOQL or multiple getInstance() and assign to Task.Subject accordingly
  • Avoid testing for empty lists if the ensuing code either just falls through (a for loop) or is DML of a collection (DML of empty lists does not count against limits). This makes methods more compact.
  • Learn to use the new Sobject method with field value setting style. Makes code easier to read
1

You need to put this statement to set the subject

task1.Subject = TaskDomain.FieldName__c;

You must log in to answer this question.

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