• JonathanFox UK
  • NEWBIE
  • 20 Points
  • Member since 2020
  • Developer
  • Art of Cloud


  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies
Hi there,

I know it is possible to call a function on a child LWC by anotating the function with @api and using a querySelector to find the child BUT is it possible to do similar for a LWC which is not nested and not in the hierarchy?
Hi all,

I use the Metadata API to update a metadata record similar to the example in this blog -> https://www.sfdcpanther.com/create-update-custom-metadata-using-apex/

However, to test one of my apex methods, I pass in some new details such as preferences for the end-user to change via a LWC rather than going into set up > Custom Metadata.

This works fine however to write a test method for this method, I am unsure how I can get code coverage as we are unable to deploy metadata in a test method.

My method of which I wish to get coverage is below:
 
@AuraEnabled
    public static void updateConfig(String companyName, String publicKey, Boolean initialSearchPref, Boolean resubmitSearchPref, String CRA1, String CRA2){
        List<String> metaDataFullNameList = ConfigController.getMetaDataFullNameList();

        Metadata.CustomMetadata customMetadataRecord = new Metadata.CustomMetadata();
        customMetadataRecord.fullName = metaDataFullNameList[0];
        System.debug('customMetadataRecord.fullName > ' + customMetadataRecord.fullName);

        if(publicKey != null){
            Metadata.CustomMetadataValue updateMetadataField1 = new Metadata.CustomMetadataValue();
            updateMetadataField1.field = 'Public_Key__c';
            updateMetadataField1.value = publicKey;
            customMetadataRecord.values.add(updateMetadataField1);
        }
        
        Metadata.CustomMetadataValue updateMetadataField2 = new Metadata.CustomMetadataValue();
        updateMetadataField2.field = 'Automatically_Submit_Search__c';
        updateMetadataField2.value = initialSearchPref;
        customMetadataRecord.values.add(updateMetadataField2);

        Metadata.CustomMetadataValue updateMetadataField3 = new Metadata.CustomMetadataValue();
        updateMetadataField3.field = 'Automatically_Resend_Referred_Searches__c';
        updateMetadataField3.value = resubmitSearchPref;
        customMetadataRecord.values.add(updateMetadataField3);

        if(CRA1 != null){
            Metadata.CustomMetadataValue updateMetadataField4 = new Metadata.CustomMetadataValue();
            updateMetadataField4.field = 'CRA_1__c';
            updateMetadataField4.value = CRA1;
            customMetadataRecord.values.add(updateMetadataField4);
        }
        

        if(CRA2 != null){
            Metadata.CustomMetadataValue updateMetadataField5 = new Metadata.CustomMetadataValue();
            updateMetadataField5.field = 'CRA_2__c';
            updateMetadataField5.value = CRA2;
            customMetadataRecord.values.add(updateMetadataField5);
    
        }
       
        if(companyName != null){
            Metadata.CustomMetadataValue updateMetadataField6 = new Metadata.CustomMetadataValue();
            updateMetadataField6.field = 'Company_Name__c';
            updateMetadataField6.value = companyName;
            customMetadataRecord.values.add(updateMetadataField6);
        }
        

        customMetadataRecord.label = 'Live';
        System.debug('customMetadataRecord > ' + customMetadataRecord);

        Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
        mdContainer.addMetadata(customMetadataRecord);
        System.debug('mdContainer > ' + mdContainer);

       ConfigMetaDataCallback callback = new ConfigMetaDataCallback();
 
        // Enqueue custom metadata deployment
        Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);
        System.debug('jobId > ' + jobId);
    }

 
My LWC calls a batch job class which inserts contacts. This works fine, however I am now trying to display a progress bar or spinner to show the status of the batch job.

I am trying to pass the Job Id into an Apex method which queries and returns the status.

As I want this fluid, it needs to 'refresh' and call frequently until the job is done in order to show the progress bar.

My Method is failing to execute. -> getBatchJobStatus

I also occasionally get a [Violation] 'setInterval' handler took xx ms.

Below is my .js
 
import { LightningElement, track, wire } from 'lwc';
import initiateContactBatch from '@salesforce/apex/BatchCreateContactController.initiateContactBatch';
import getBatchJobStatus from '@salesforce/apex/BatchCreateContactController.getBatchJobStatus';
export default class BombBurstContacts extends LightningElement {

    

    @track numberInput = 0;
    @track jobID = null;
    @track createError;
    @track error;
    @track record;
    @track processing = false;
    @track jobPercentage;
    @track totalJobItems;
    @track jobItemsProcessed;
    @track status;
    @track progress = 500;  
  
    onChange(event){
        this.numberInput = event.target.value;
        console.log('this.numberInput' + this.numberInput);
    }
    
    createRecords(){
        
        initiateContactBatch({ numberOfRecords : this.numberInput })
            .then(result => {
                this.jobID = result;
                this.processing = true;
                console.log('this.jobID' + this.jobID);
                console.log('this.processing' + this.processing);

                this.connectedCallback();
                
            })
            .catch(error => {
                this.createError = error;
                console.log('this.createError' + this.createError);
                
            });
    }

    connectedCallback() {  
        if(this.jobID != null){
            this._interval = setInterval(() => {  
                
                
               //this.getStatus();

               this.getBatchJobStatus({ jobID : this.jobID})
                .then(result => {
                    this.record = result;
                    console.log('this.record' + this.record);
                })
                .catch(error => {
                    this.error = error;
                    console.log('this.error' + this.error);
                });
                
                this.progress = this.progress + 25000;
                console.log('this.progress' + this.progress);
                
                if ( this.progress === 200000 ) {  
                    clearInterval(this._interval);  
                }  
            }, this.progress);  
        }
  
    }  

    // getStatus(){
    //     getBatchJobStatus({ jobID : jobID})
    //     .then(result => {
    //         this.record = result;
    //         console.log('this.record' + this.record);
    //     })
    //     .catch(error => {
    //         this.error = error;
    //         console.log('this.error' + this.error);
    //     });
    // }


    
    
}

 
Hi All,

Here I have a Batch Apex class which uses an API to get some random contacts. This all works perfectly fine and I am happy with it,

I wanted to trigger this with a user input via a LWC. I created BatchCreateContactController to do this.

Calling BatchCreateContactController from the Developer sonsole works, but my LWC won't call it. I suspect it is something to do with the LWC JS file. Unsure.
Hi all,

I use the Metadata API to update a metadata record similar to the example in this blog -> https://www.sfdcpanther.com/create-update-custom-metadata-using-apex/

However, to test one of my apex methods, I pass in some new details such as preferences for the end-user to change via a LWC rather than going into set up > Custom Metadata.

This works fine however to write a test method for this method, I am unsure how I can get code coverage as we are unable to deploy metadata in a test method.

My method of which I wish to get coverage is below:
 
@AuraEnabled
    public static void updateConfig(String companyName, String publicKey, Boolean initialSearchPref, Boolean resubmitSearchPref, String CRA1, String CRA2){
        List<String> metaDataFullNameList = ConfigController.getMetaDataFullNameList();

        Metadata.CustomMetadata customMetadataRecord = new Metadata.CustomMetadata();
        customMetadataRecord.fullName = metaDataFullNameList[0];
        System.debug('customMetadataRecord.fullName > ' + customMetadataRecord.fullName);

        if(publicKey != null){
            Metadata.CustomMetadataValue updateMetadataField1 = new Metadata.CustomMetadataValue();
            updateMetadataField1.field = 'Public_Key__c';
            updateMetadataField1.value = publicKey;
            customMetadataRecord.values.add(updateMetadataField1);
        }
        
        Metadata.CustomMetadataValue updateMetadataField2 = new Metadata.CustomMetadataValue();
        updateMetadataField2.field = 'Automatically_Submit_Search__c';
        updateMetadataField2.value = initialSearchPref;
        customMetadataRecord.values.add(updateMetadataField2);

        Metadata.CustomMetadataValue updateMetadataField3 = new Metadata.CustomMetadataValue();
        updateMetadataField3.field = 'Automatically_Resend_Referred_Searches__c';
        updateMetadataField3.value = resubmitSearchPref;
        customMetadataRecord.values.add(updateMetadataField3);

        if(CRA1 != null){
            Metadata.CustomMetadataValue updateMetadataField4 = new Metadata.CustomMetadataValue();
            updateMetadataField4.field = 'CRA_1__c';
            updateMetadataField4.value = CRA1;
            customMetadataRecord.values.add(updateMetadataField4);
        }
        

        if(CRA2 != null){
            Metadata.CustomMetadataValue updateMetadataField5 = new Metadata.CustomMetadataValue();
            updateMetadataField5.field = 'CRA_2__c';
            updateMetadataField5.value = CRA2;
            customMetadataRecord.values.add(updateMetadataField5);
    
        }
       
        if(companyName != null){
            Metadata.CustomMetadataValue updateMetadataField6 = new Metadata.CustomMetadataValue();
            updateMetadataField6.field = 'Company_Name__c';
            updateMetadataField6.value = companyName;
            customMetadataRecord.values.add(updateMetadataField6);
        }
        

        customMetadataRecord.label = 'Live';
        System.debug('customMetadataRecord > ' + customMetadataRecord);

        Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
        mdContainer.addMetadata(customMetadataRecord);
        System.debug('mdContainer > ' + mdContainer);

       ConfigMetaDataCallback callback = new ConfigMetaDataCallback();
 
        // Enqueue custom metadata deployment
        Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);
        System.debug('jobId > ' + jobId);
    }

 
Hi All,

Here I have a Batch Apex class which uses an API to get some random contacts. This all works perfectly fine and I am happy with it,

I wanted to trigger this with a user input via a LWC. I created BatchCreateContactController to do this.

Calling BatchCreateContactController from the Developer sonsole works, but my LWC won't call it. I suspect it is something to do with the LWC JS file. Unsure.