[SalesForce] how to clear value of force inputField in lightning

I have a custom component that creates a new Task record. I use force inputField that is bound to a lookup field in Task.

I want to clear it's values if the user clicks cancel or save.


<div class="slds-form-element__row">
                <div class="slds-form-element slds-size--2-of-2">
                    <label class="slds-form-element__label" for="lookup-69">Project</label>
                    <force:inputField value="{! v.newTaskList.Project_Number__c}" aura:id="projId" class="sl"/>                          
                  </div>
                </div>

that's the force:inputfield that automatically binds to the Project Number field which is a look up to a standard object.

This is how it looks like in the fornt end

After I save the record, and go back to this lookup and remove the selected option, i get the error below.

enter image description here

That's why I want to clear the inputfield after i save the record. However, if you can also let me know your ideas as to why I am getting this error, it would be a big help. the work around i have is to refresh the whole page which is not good.

Best Answer

After toying with force:inputField a bit, I found the following code to clear it's value.

// Clear binded value
var contractLine = component.get('v.contractLine');
contractLine.Product__c = null;
contractLine.Product__r = {'sobjectType': 'Product2'};
component.set('v.contractLine', contractLine);    

// Clear product lookup
var productLookup = component.find('Product').get('v.body')[0];
productLookup.updateValues();
Related Topic