function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jyotirupa DasJyotirupa Das 

Hi, I need help in writing test class for below method having custom metadata.

Hi ,
Can someone please help me regarding test method for below code.

 @AuraEnabled
    public static String SaveContactInquiry(String con,String inq, String lang) {
        Contact cont = (Contact) JSON.deserialize (con,Contact.class); 
        Case inquiry = (Case) JSON.deserialize (inq,Case.class); 
        Case cas;
        Id InqId;
        String ContactId ='';
        List<GC_Language_Codes__mdt> langCodeList = new List<GC_Language_Codes__mdt>();
        try{
            langCodeList= [Select DeveloperName, Label from GC_Language_Codes__mdt where DeveloperName = :lang];
            /* updated based on US 5158 for setting the language on contact and Inquiry pages*/
            //Country__c countryinfo = [SELECT Id, Name,Country_ISO_Code__c, Country_Code__c,Default_Language__c FROM Country__c WHERE Id =:inquiry.LookupCountry__c];
            System.debug('^^ langCodeList'+langCodeList);
            if(lang != null && lang != '')
                cont.Preferred_Language__c = langCodeList[0].Label;
            
           /* getting all inquiry queue and assigning to that queue*/
            QueueSObject QueueID = [Select Queue.Id, Queue.Name, Queue.Type from QueueSObject WHERE Queue.Type ='Queue' AND Queue.Name =: Label.GC_GMI_All_Inquiry  Limit 1];
            
            
            List<Contact> checkContact = [SELECT Id,FirstName, LastName,State_Province__c 
                                          FROM Contact 
                                          WHERE FirstName =: cont.FirstName
                                          AND LastName =: cont.LastName 
                                          AND Country__c =: cont.Country__c
                                          AND Zip_Code__c =: cont.Zip_Code__c];
            System.debug('^^ checkContact List'+checkContact+'QueueID'+QueueID);
            if(checkContact.size() > 0){
                inquiry.ContactId = checkContact[0].Id;
                ContactId = inquiry.ContactId;  
            }
            else{
                Database.DMLOptions dml = new Database.DMLOptions();
                dml.DuplicateRuleHeader.allowSave = true;
                dml.DuplicateRuleHeader.runAsCurrentUser = true;
                Database.SaveResult sr = Database.insert(cont,dml);
                //insert cont;
                inquiry.ContactId =cont.id;
                ContactId = cont.id;
            } 
            if (QueueID.id   != null){
                inquiry.OwnerId = QueueID.Queue.Id;
            }
            system.debug('^^ inquery obj to be inserted'+inquiry);
            insert inquiry;
            InqId = inquiry.Id;
            cas = [select CaseNumber from case where id=:InqId];
            System.debug('^^ case'+cas);
            return  cas.CaseNumber +'@'+ContactId ;
           }
        catch(Exception e){
            createSystemLogEntry(InqId,'create the case and Contact',e.getMessage(),'Veeva Vault');
            System.debug('^^ Within error of case creation'+ e.getMessage());
            throw new GC_TriggerHandlerException('Error');
            
        }
        
    }
    

Thanks
Suraj Tripathi 47Suraj Tripathi 47
Hi,
Greetings!

In custom metadata, you cannot perform simple DML command.
So, you have to dispatch changes.
For test class, please visit the below link-
https://www.sfdcpanther.com/create-update-custom-metadata-using-apex/

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi