• Kiril Vodenicharov 7
  • NEWBIE
  • 20 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies

I want to update before and after a record when it have certain status criteria which is met. Lets say WHEN Quote have met: Merchant Approved status to create new Order.

public inherited sharing class CPQ_QuoteTriggerHandler extends Common_TriggerHandler implements Common_TriggerInterface
{
      public void beforeUpdate(Map<Id, SObject> mapNewItems, Map<Id, SObject> mapOldItems) {
        // Call the trigger.
    }
}

TriggerHelper

public static void merchantApproved(List<SBQQ__Quote__c> quoteList, Map<Id, SBQQ__Quote__c> oldMap)
    {
        Set<Id> quoteIds = new Set<Id>();
        for(SBQQ__Quote__c q : quoteList)
        {
            quoteIds.add(q.SBQQ__Status__c);
        }
        
        oldMap = new Map<Id, SBQQ__Quote__c>([SELECT Id, SBQQ__Status__c FROM SBQQ__Quote__c WHERE Id IN :quoteIds]);

        for(SBQQ__Quote__c q : quoteList)
        {
            if(oldMap.get(q.Id).SBQQ__Status__c == q.SBQQ__Status__c && q.SBQQ__Status__c == 'Merchant Approved')
            {
                Order order = new Order
                    (
                        AccountId = q.Id,
                        Name = q.Name
                	);
            }
        }
    }

I have problem with my unit test class. 

@RestResource(urlMapping='/magnolia/account/*')
global with sharing class MCH_MagnoliaAccountRest {


    @HttpGet
    global static List<AccountWrapper> getAccount(){//what do you need to get ? Single row ?
        String idsParam = RestContext.request.params.get('ids');
        List<String> ids = new List<String>();
        ids = idsParam?.split(',');
        List<AccountWrapper> accountsResult = new List<AccountWrapper>();
        for(Account a : [Select 
                         	 Id,
                             mch_MCH_Login_ID__pc,
                             Type,
                             Name,
                             Description,
                             PersonMailingStreet,
                             PersonMailingPostalCode,
                             PersonMailingCountry,
                             Phone,
                             PersonEmail,
                             Website,
                             Industry,
                             mch_interests__c
                         From Account
                         WHERE Id IN :ids
                         Limit 1]){
            accountsResult.add(
                wrapAccount(a)
            );
        }
        return accountsResult;
    }
    @HttpPost
    global static String insertAccount(List<AccountWrapper> accounts){
        List<Account> accountList = new List<Account>();
        for(AccountWrapper aw : accounts){
            accountList.add(
                aw.getAccount()
            );
        }
        if(!accountList.isEmpty()){
            dmlErrorHandling(
                Database.insert(accountList, false)
            );
        }
        return 'ok';
    }
    @HttpPatch
    global static String updateAccount(List<AccountWrapper> accounts){
        List<Account> accountList = new List<Account>();
        for(AccountWrapper aw : accounts){
            accountList.add(
                aw.getAccount()
            );
        }
        if(!accountList.isEmpty()){
            dmlErrorHandling(
                Database.update(accountList, false)
            );
        }
        return 'ok';
    }
    @HttpPut//what do you need to delete ? Single row ?
    global static String deleteAccount(List<AccountWrapper> accounts){
        List<Account> accountList = new List<Account>();
        for(AccountWrapper aw : accounts){
            accountList.add(
                aw.getAccount()
            );
        }
        if(!accountList.isEmpty()){
            dmlErrorHandling(
                Database.delete(accountList, false)
            );
        }
        return 'ok';
    }

Unit test code

@isTest
public class MCH_MagnoliaAccountRestTest 
{   
    @isTest
    static Account TestGetAccount()
    {
        Account account = McH_MockupFactory.createAccount();
        if(true)
        {
            insert account;
        }
        return account;
    }
    
    @isTest 
    static void InsertAccount()
    {
        Account account = McH_MockupFactory.createAccount();
        insert account;
    }
    
    @isTest 
    static void UpdateAccount()
    {
        List<MCH_MagnoliaAccountRest.AccountWrapper> aw = new List<MCH_MagnoliaAccountRest.AccountWrapper>();
        
        Account account = McH_MockupFactory.createAccount();
        insert account;
    }
    
    @isTest 
    static void deleteAccount()
    {   
        List<MCH_MagnoliaAccountRest.AccountWrapper> aw = new List<MCH_MagnoliaAccountRest.AccountWrapper>();
        
        Account account = McH_MockupFactory.createAccount();
        insert account;
        account = MCH_MagnoliaAccountRest.deleteAccount(aw); // Here I have the error.
        delete account;
    }
}

Need help to finish it.
trigger PopulateKeyFieldCount on Lead (after insert) 
{
    Integer countOfKeyFields = 0;
    
    Set<String> keyFields = new Set<String>{'FirstName', 'LastName', 'Email', 'Phone', 'Website', 'Title'};
    String fieldsWithTest = '';
    
    List<Task> taskList = new List<Task>();
    
    for(Lead leadRecord : Trigger.New)
    {
        countOfKeyFields = 0;
        
        if(leadRecord.Key_Fields_Populated_c__c != null)
        {
            countOfKeyFields = leadRecord.FirstName != null ? countOfKeyFields + 1: countOfKeyFields;
            countOfKeyFields = leadRecord.LastName != null ? countOfKeyFields + 1: countOfKeyFields;
            countOfKeyFields = leadRecord.Email != null ? countOfKeyFields + 1: countOfKeyFields;
        }
        
        if(countOfKeyFields == 3)
        {
            for(String fieldName : keyFields)
            {
                Task task = new Task
                    (
                        Subject = 'Verify the ' + fieldName,
                        Priority = 'Hight',
                        Status = 'Complete',
                        Type = 'Action',
                        ownerId = leadRecord.OwnerId
                    );
                
                taskList.add(task);
            }
        }
        
        
    }
    
    if(taskList != null && !taskList.isEmpty())
    {
        insert taskList;
    }
}

This is a trigger which populates a custom Key Fields. If the trigger had at least 3 Lead key fields it will create a Task.
How to write a trigger that creates 10 identical Opportunities whenever an Account with more than 99 employees is created. Make sure all Opportunities are associated with the Account. Use any values for the fields on the Opportunities and to work with Map collection.
trigger PopulateKeyFieldCount on Lead (after insert) 
{
    Integer countOfKeyFields = 0;
    
    Set<String> keyFields = new Set<String>{'FirstName', 'LastName', 'Email', 'Phone', 'Website', 'Title'};
    String fieldsWithTest = '';
    
    List<Task> taskList = new List<Task>();
    
    for(Lead leadRecord : Trigger.New)
    {
        countOfKeyFields = 0;
        
        if(leadRecord.Key_Fields_Populated_c__c != null)
        {
            countOfKeyFields = leadRecord.FirstName != null ? countOfKeyFields + 1: countOfKeyFields;
            countOfKeyFields = leadRecord.LastName != null ? countOfKeyFields + 1: countOfKeyFields;
            countOfKeyFields = leadRecord.Email != null ? countOfKeyFields + 1: countOfKeyFields;
        }
        
        if(countOfKeyFields == 3)
        {
            for(String fieldName : keyFields)
            {
                Task task = new Task
                    (
                        Subject = 'Verify the ' + fieldName,
                        Priority = 'Hight',
                        Status = 'Complete',
                        Type = 'Action',
                        ownerId = leadRecord.OwnerId
                    );
                
                taskList.add(task);
            }
        }
        
        
    }
    
    if(taskList != null && !taskList.isEmpty())
    {
        insert taskList;
    }
}

This is a trigger which populates a custom Key Fields. If the trigger had at least 3 Lead key fields it will create a Task.
How to write a trigger that creates 10 identical Opportunities whenever an Account with more than 99 employees is created. Make sure all Opportunities are associated with the Account. Use any values for the fields on the Opportunities and to work with Map collection.