You need to sign in to do that
Don't have an account?
Robert Davis 1
Account Trigger Test Class with Custom Metadata Type
I have a trigger on the Account that looks into a Custom Metadata Type and if the User Id is there they are allowed to delete the record otherwise they receive an error message.
The trigger acts as expected in testing my problem is I do not understand how to write the test code.
Account Trigger:
But I do not know how to fix:
Really would appreciate any help.
The trigger acts as expected in testing my problem is I do not understand how to write the test code.
Account Trigger:
trigger AccountTrigger2 on Account ( before delete) { Account_Trigger_Handler handler = new Account_Trigger_Handler(); if(Trigger.IsDelete) { handler.OnDelete_Account(Trigger.old); }//End if }//End classTrigger Handler:
public with sharing class Account_Trigger_Handler { public void OnDelete_Account(List<Account> acct) { //get user id for current user Id uid = UserInfo.getUserId(); system.debug('User id : '+uid); //create a map to put in all the users who are authorized to delete Map<Id,Account_Delete_User__mdt> mapAcctDeleteUser = new Map<Id, Account_Delete_User__mdt>(); //loop through the users who are authorized to delete and put them in the map we created just before this for(Account_Delete_User__mdt AcctDeleteUser : [SELECT id, UserId__c FROM Account_Delete_User__mdt]) { mapAcctDeleteUser.put(AcctDeleteUser.Userid__c, AcctDeleteUser); }//end for //loop through the records in the trigger and see if the current user is authorized to delete. for(Account a: acct) { if(mapAcctDeleteUser.keyset().contains(uid)== false) { a.addError('You are not authorized to remove Accounts from Salesforce. Please email Salesforce.admin@XXXXX.net to have record removed.'); }//end if }//end for }//end method }The following is the test class I have so far, but it gets the error: System.AsyncException: Metadata cannot be deployed from within a test
But I do not know how to fix:
@isTest public class Account_Trigger_HandlerTest { public static testMethod void OnDelete_AccountTest() { Profile p2 = [SELECT id FROM Profile WHERE Name ='Standard User']; User usr2 = new User (LastName ='Boris', FirstName ='Badenov', Email = 'boris.badenov@bullwinkleshow.com', Username = 'boris.badenov@bullwinkleshow.com', Alias = 'boris', Profileid = p2.Id, TimeZoneSidKey ='GMT' , LanguageLocaleKey = 'en_US', EmailEncodingKey = 'UTF-8', LocaleSidKey = 'en_US'); insert usr2; //--------------------------------------------------------------------- //https://ashwanisoni.wordpress.com/2017/05/02/create-or-update-custom-metadata-type-via-apex-code-salesforce-summer-17/ // Set up custom metadata to be created in the subscriber org. Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata(); customMetadata.fullName = 'Account_Delete_User'; customMetadata.label = 'User'; Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue(); customField.field = 'UserId__c'; customField.value = usr2.id; customMetadata.values.add(customField); Metadata.DeployContainer mdContainer = new Metadata.DeployContainer(); mdContainer.addMetadata(customMetadata); // Setup deploy callback, MyDeployCallback implements // the Metadata.DeployCallback interface (code for // this class not shown in this example) CustomMetadataCallback callback = new CustomMetadataCallback(); // Enqueue custom metadata deployment // jobId is the deployment ID Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback); //---------------------------------------------------------------------- Profile p = [SELECT id FROM Profile WHERE Name ='Standard User']; User usr1 = new User (LastName ='Fatale', FirstName ='Natasha', Email = 'natasha.fatele@bullwinkleshow.com', Username = 'natasha.fatele@bullwinkleshow.com', Alias = 'natas', Profileid = p.Id, TimeZoneSidKey ='GMT' , LanguageLocaleKey = 'en_US', EmailEncodingKey = 'UTF-8', LocaleSidKey = 'en_US'); insert usr1; Account acct2 = New Account(Name='Snagglepuss', Type='Prospect', SSN__c = '12385678', CreatedById = usr1.id); insert acct2; Test.startTest(); Database.DeleteResult result = Database.delete(acct2,false); System.assert(!result.isSuccess()); Test.stopTest(); } }The following is the callback function that the test code references:
public class CustomMetadataCallback implements Metadata.DeployCallback { public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext context) { if (result.status == Metadata.DeployStatus.Succeeded) { System.debug('success: '+ result); } else { // Deployment was not successful System.debug('fail: '+ result); } } //https://ashwanisoni.wordpress.com/2017/05/02/create-or-update-custom-metadata-type-via-apex-code-salesforce-summer-17/ }I am just missing the boat on how to fix this.
Really would appreciate any help.
Hi Robert
Custom metadata are a part of application configuration like your workflows,validation,object etc. These can be included in changesets,packages etc.
They aren't specifically your records for which you would need to add @IsTest(SeeAllData=true) which although is not a good practice .Apex tests can see them.You dont have to create records in your test methods.
For better understanding please check out the link mentioned below :
https://developer.salesforce.com/blogs/engineering/2015/05/testing-custom-metadata-types.html
Cheers
Insha
All Answers
Hi Robert
Custom metadata are a part of application configuration like your workflows,validation,object etc. These can be included in changesets,packages etc.
They aren't specifically your records for which you would need to add @IsTest(SeeAllData=true) which although is not a good practice .Apex tests can see them.You dont have to create records in your test methods.
For better understanding please check out the link mentioned below :
https://developer.salesforce.com/blogs/engineering/2015/05/testing-custom-metadata-types.html
Cheers
Insha
I finally got this to work. when I get it all done I will post for others. Thank you for the information.
Robert