1

I have been thinking for too long, but there is no substantial progress on this issue, so please ask everyone, what are the good suggestions and solutions.

This is my LWC.JS file

import { LightningElement, track} from 'lwc';
import getAccount from '@salesforce/apex/cpcs_CustomerServicesController.getAccount';
import HideHeader from '@salesforce/resourceUrl/HideHeader';
import { loadStyle} from 'lightning/platformResourceLoader';

export default class CustomerService_LWC extends LightningElement {

    @track record = [];
    @track labelRecord = [];
    @track accInfo = [];

    connectedCallback() {
        // initialize component
        loadStyle(this, HideHeader)
        window.addEventListener("message", (event) =>{

            if(event.origin === 'https://qasp.yhcrm.cn'){
                var dataVal1 = event.origin;
                var dataVal2 = event.data;
                console.log('get event successfully1!' + dataVal1);
                console.log('get event successfully2!' + JSON.stringify(dataVal2));
                // Handle the message
            }else{
                console.log('***11231');
                return;
            }

        }, false);
    }

    page1Btn(){
        getAccount({accId: '1234'})
            .then(result => {
                this.accInfo = result;
            })
            .catch(error => {
                this.error = error;
            });
    }
}

This is my Apex file

public with sharing class cpcs_CustomerServicesController {
    // get label
    @AuraEnabled(cacheable=true)
    public static List<Account> getAccount(String accId) {
        System.debug(LoggingLevel.INFO, 'accId = ' + accId);
        return [SELECT ID,Name,U_Gender__c,U_Mobile__c,U_City__c,U_SubscribeSceneDCode__c,U_Description__c,
                        U_QrSceneStr__c,Type,U_BusinessDCode__c,U_QrScene__c,
                        U_Source__c,U_BusinessName__c,U_SyncStatus__c,U_SyncInfo__c,
                        U_RegDate__c,U_FollowUpTime__c
                        FROM Account LIMIT 1];
    }
}

This is my LWC.HTML file

enter image description here

Question: How to pass dataVal2 to getAccount(accId :dataVal2 ) in the [page1Btn]JS method and call the [page1Btn] method.

1
  1. You need to pass the dataVal2 by using this. which makes it class variable and will be persistent

Pls check the code below: (@track dataVal2; is not really needed dataVal2 is not referenced in HTML)

export default class CustomerService_LWC extends LightningElement {

    @track record = [];
    @track labelRecord = [];
    @track accInfo = [];
    @track dataVal2;
    connectedCallback() {
        // initialize component
        loadStyle(this, HideHeader)
        window.addEventListener("message", (event) => {

            if (event.origin === 'https://qasp.yhcrm.cn') {
                this.dataVal1 = event.origin;
                this.dataVal2 = event.data;
                console.log('get event successfully1!' + this.dataVal1);
                console.log('get event successfully2!' + JSON.stringify(this.dataVal2));
                // Handle the message
            } else {
                console.log('***11231');
                return;
            }

        }, false);
    }

    page1Btn() {
        getAccount({ accId: this.dataVal2 })
            .then(result => {
                this.accInfo = result;
            })
            .catch(error => {
                this.error = error;
            });
    }
}
0

My current implementation is to call the 'page1Btn' method again in the callback, but I'm not sure if this is the wrong approach.

import { LightningElement, track} from 'lwc';
import getAccount from '@salesforce/apex/cpcs_CustomerServicesController.getAccount';
import HideHeader from '@salesforce/resourceUrl/HideHeader';
import { loadStyle} from 'lightning/platformResourceLoader';

export default class CustomerService_LWC extends LightningElement {

    @track record = [];
    @track labelRecord = [];
    @track accInfo = [];
    @this dataVal2;

    connectedCallback() {
        // initialize component
        loadStyle(this, HideHeader)
        window.addEventListener("message", (event) =>{

            if(event.origin === 'https://qasp.yhcrm.cn'){
                var dataVal1 = event.origin;
                this.dataVal2 = event.data;

                getAccount({accId: this.dataVal2})
                    .then(result => {
                        this.accInfo = result;
                    })
                    .catch(error => {
                        this.error = error;
                    });
                        // Handle the message
            }else{
                console.log('***11231');
                return;
            }

        }, false);
    }

    page1Btn(){
        getAccount({accId: this.dataVal2})
            .then(result => {
                this.accInfo = result;
            })
            .catch(error => {
                this.error = error;
            });
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.