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
Shubham Bansal 45Shubham Bansal 45 

Need Help in calling a apex method from helper in Lightning component.

Hi all I have a strange requirement of calling a helper method in which an apex method is calling, the trick is that I have to call my helper method with a delay of 30 sec. 

But in some scenarios before the 30 sec, my component  Dom is getting destroyed and in the helper, I face the error of set params of undefined.

I am not sure it is because Dom is destroyed or any other reason.
ravi soniravi soni

Hi Shubham,
Please show me your code, I don't understand your issue.

Thank You

Shubham Bansal 45Shubham Bansal 45
Hii Veer,

I am working on a custom pre-chat form, and the requirement is that when the user enters some text in my subject field of pre-chat field a counter get started which count 30 sec if in between this 30-sec chat is not started then I have to create a record for the entered text in a custom record and if chat is started I have to do nothing.

And My problem is that suppose user to type in the subject field and counter get started then user click on start chat and before 30-sec user leave the chat in this case scope of my component is finish and I got an error setParams of undefined,


I hope you got my requirement.

The piece of code I am facing is:--

    handleStartButtonClick: function(cmp, evt, hlp) {
            var count=0;
        var reqirefield=[];
        reqirefield= cmp.get("v.requiredFields");
        console.log('@@@@@reqirefield@@@@'+reqirefield);
        var prechatFieldComponents = cmp.find("prechatField");
        var fields;
        console.log('@@@in helper222'+prechatFieldComponents);
        // Make an array of field objects for the library
        fields = hlp.createFieldsArray(prechatFieldComponents,cmp);
        console.log('@@@in fields'+JSON.stringify(fields));
        for(var i=0;i<reqirefield.length;i++){
            var requireFieldName=reqirefield[i];
            for(var j=0;j<fields.length;j++){
                if(fields[j].label ==requireFieldName ){
                    if((fields[j].value=="")||(fields[j].value==" ")){
                        count++;
                    }
                } 
            }  
        }
        console.log('@@@MyCount-->'+count);
        if(count>0){
            cmp.set("v.isError",true);
            //    location.href="#ttt";        
        }
        
        else{
            // If the pre-chat fields pass validation, start a chat
            if(cmp.find("prechatAPI").validateFields(fields).valid) {
                cmp.find("prechatAPI").startChat(fields);
                console.log('Enter in start chat----->')
                cmp.set('v.chatFlag',true);
                
            } else {
                console.warn("Prechat fields did not pass validation!");
            }
        }
        
    }







openArticle : function(component, event, helper) {
        var Ide = event.srcElement.id;
        component.set('v.Articleid',Ide);
        var urlName =  component.get("v.OrgURL")+'/'+Ide;        
        console.log('urlName'+urlName);
        var sObectEvent = $A.get("e.force:navigateToURL");
        console.log('Ide'+Ide);
        sObectEvent .setParams({
            "url": urlName
        });
        sObectEvent.fire();
        var sub=component.get('v.Subjectvalue');
                var ArtId = component.get('v.Articleid');
      
        
        window.setTimeout($A.getCallback(function(){ 
              var flg =component.get('v.chatFlag');
               console.log('Chat-Flag####Direct',component.get('v.chatFlag'));
              if(flg!=true){
                  flg=false;
              }
              console.log('Chat-Flag####',flg);
              if(flg!=true){
                helper.createSearchData(component,sub,ArtId);
              }

      }), 20000);
        
        
    }











Helper method:


 createSearchData:  function(component,sub,ArtId) {
        console.log('OpenArticle@@@createSearchData',sub+'3333'+ArtId);
       // var subjectdata = component.get('v.Subjectvalue');   
       // var artid = component.get('v.Articleid');
            var subjectdata = sub;   
        var artid = ArtId;
        console.log('OpenArticle@@@subjectdata',component.get('v.prechatFieldComponents'));
         console.log('OpenArticle@@@artid',artid);
        var call = component.get("c.insertSearchData");
        call.setParams({
            "SearchTerm":sub,
            "ArticleId":ArtId
        });
        call.setCallback(this, function(response) {
            if (response.getState() == "SUCCESS") {
                console.log('OpenArticle@@@');      
            }
        });
        $A.enqueueAction(call);
    }