Lightning event: e.force:createRecord
Recently we had a necessity to create a custom dynamic lightning component for related list. In order to build the related list with additional features dynamically according to the indicated objects “parent” and “child”.
One part from additional functions which was necessary to implement was actually a clone on the level “row” and a button “+ Add Line”.
For these actions we decided to use lightning standard event $A.get("e.force:createRecord").
For the clone we transmitted default values to the attribute “defaultFieldValues”.
However, while testing we have spotted an issue with the object “OpportunityLineItem”.
The modal window for objects creation could be opened but in attempt to change the lookup for a product we had the following:
It appeared that one more parameter in the event $A.get("e.force:createRecord") - inContextOfRecordId.
inContextOfRecordId parameter is a required field for OpportunityLineItem, QuoteLineItem, OrderItem, ContractLineItem, WorkOrderLineItem entities.
Further we have to use inContextOfRecordId to pass the recordId when we want to create OpportunityLineItem by using force:createRecord.
Sample example for OpportunityLineItem creation:
createRecord : function (component, event, helper) {
var createRecordEvent = $A.get("e.force:createRecord");
createRecordEvent.setParams({
"entityApiName": "OpportunityLineItem" ,
"inContextOfRecordId": component.get("v.recordId"),
});
createRecordEvent.fire();
}
In case if someone else faces this kind of issues, at least here is the solution. We are not greedy and we are trying to share our findings and some solutions that we came to. Hope this article was very useful. If you have any additional questions, feel free to leave your comments to the article.