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
Gurleen KaurGurleen Kaur 

Unable to set Component attribute value in Helper class

Hi,

I am facing an issue in assigning attribute value for a component in saveResult method, inside helper class:
Component:
 <aura:attribute name="isOwnerDefined" type="String" default=""/>

<force:recordData aura:id="CntctTypeRecordCreator"
                          targetRecord="{!v.newContact}"
                          targetFields="{!v.CntctTypeResultFields}"
                          layoutType="FULL"
                          mode="EDIT" />
JS Controller :
component.find("CntctTypeRecordCreator").saveRecord($A.getCallback(function(saveResult) {
                if(saveResult.state === "SUCCESS" || saveResult.state === "DRAFT"){
                   for(var key in component.get("v.cntctTypesMap")){
                        if(component.get("v.selectedOption") == key){
                            helper.setContactTypeMAppingRec(component,key,saveResult.recordId);
                            break;
                        }
                    }
             }))
Helper:
setContactTypeMAppingRec : function(component,mappingName,recordId) {
        component.set("v.newCntctMappingField.Contact__c",recordId);
        component.set("v.newCntctMappingField.ContactType__c",component.get("v.cntctTypesMap")[mappingName]);
        component.set("v.newCntctMappingField.Name",mappingName);
        component.find("cnTypeRecordLoader").saveRecord($A.getCallback(function(saveResultContact) {
            if(saveResultContact.state === "SUCCESS" || saveResultContact.state === "DRAFT"){
                component.set("v.isOwnerDefined",'myTest');     // I have assigned string value to component attribute
                console.log('====' + component.get("v.isOwnerDefined")); // But here it returns "undefined"
                console.log('Contact Type mapping Saved!!');
            }
            else if (saveResultContact.state === "INCOMPLETE") 
                console.log('Server could not be reached. Check your internet connection!');
            else if (saveResultContact.state === "ERROR") 
                console.log('Exception Found!');
            else
                console.log('Error while loading Current Processor Picklist. Please contact your System Administrator.');
        }))
    }


Any Help?
YogeshMoreYogeshMore
Hi,

Try this one, just I have changed single quote to double quote.

component.set("v.isOwnerDefined", "myTest");  
Gurleen KaurGurleen Kaur
I tried this as well, but its not working!!
GauravGargGauravGarg
Hi Gurleen,

Are you getting any errors? Please use Lightning Inspector tool to inpect the value of attritbute.

Thanks,
Gaurav
Skype: gaurav62990
Gurleen KaurGurleen Kaur
Hi,

Just a catch here. I tried checking the validity of the component using component.isValid() just before setting attribute component.set("v.isOwnerDefined",'myTest' ) at line 19, the component.isValid()  is returning false.
But when I try assigning the component attribute value just on Line 15 it is printing correct value.

So, would you know why the validity of the component is getting lost on Line 19?.

Helper:
setContactTypeMAppingRec : function(component,mappingName,recordId) {
        component.set("v.newCntctMappingField.Contact__c",recordId);
        component.set("v.newCntctMappingField.ContactType__c",component.get("v.cntctTypesMap")[mappingName]);
        component.set("v.newCntctMappingField.Name",mappingName);
     Line 15==>  /*component.set("v.isOwnerDefined",'myTest');     // I have assigned string value to component attribute
                console.log('====' + component.get("v.isOwnerDefined")); // Here it prints the correct value
        */

        component.find("cnTypeRecordLoader").saveRecord($A.getCallback(function(saveResultContact) {
            if(saveResultContact.state === "SUCCESS" || saveResultContact.state === "DRAFT"){
                      Line 19==> if(component.isValid()){   // It returns "False "           
                               
component.set("v.isOwnerDefined",'myTest');     
                               console.log('====' + component.get("v.isOwnerDefined"));
                        }
                        console.log('Contact Type mapping Saved!!');
            }
            else if (saveResultContact.state === "INCOMPLETE") 
                console.log('Server could not be reached. Check your internet connection!');
            else if (saveResultContact.state === "ERROR") 
                console.log('Exception Found!');
            else
                console.log('Error while loading Current Processor Picklist. Please contact your System Administrator.');
        }))
    } 
GauravGargGauravGarg
Hi Gurleen,

Please read component validity resource of Salesforce, this might provide you some insight. 

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_cmp_isvalid.htm

Thanks,
Gaurav
Gurleen KaurGurleen Kaur
The use cases are different. In my case I have to play around a valid component. If it is going inValid  there has to be some chnage in Logic. So i am facing difficulty how to tweak the logic so that I dont get invalid component.
GauravGargGauravGarg
Are there any page rendering-reloading?
Gurleen KaurGurleen Kaur
I am using Lightning Data service to insert 2 child records when parent record gets inserted. And It seems that the LDS is asynchronous. Maybe thats why the component gets invalid. So is there any way where I can insert 2 child fecords once parent record gets inserted without loosing the validity of the component?
GauravGargGauravGarg

Yes, that might be the issue over here. You can write trigger on parent object to perform this (Classic model never fails to work)

For more details, please go through this link (http://sfdcmonkey.com/2017/04/30/add-multiple-child-records-lightning-component/)

Thanks,

Gaurav
skype: gaurav62990

 

Gurleen KaurGurleen Kaur
So, In my case can't I use Lightning Data service to perform required DML opeartions? Do I need to call Apex class to perform DML logic?
GauravGargGauravGarg
Can you try below steps:
  • Update Component with *component.set("v.isOwnerDefined",'myTest');
  • Call another method in Helper.JS to update / create child record. 
  • Then check component validity and if valid show the result on page. 
I am not sure will this work, but give a try. 

Thanks,
Gaurav
Gurleen KaurGurleen Kaur
No Luck!