1

I am trying to create multiple opportunity products and quotelineitems for respective opportunities and quote. I have already inserted the opportuity and quote using lightning data service. But I dont know how to create list of records using lightning data service. Is this feature available in salesforce.

Lightning Component Code -

<aura:attribute name="oppLineItems" type="OpportunityLineItem[]"/>
<aura:attribute name="newOpportunity" type="Object" />
<aura:attribute name="simpleNewOpportunity" type="Object"/>
<aura:attribute name="oppError" type="String"/>
<aura:attribute name="newQuote" type="Object" />
<aura:attribute name="simpleNewQuote" type="Object"/>
<aura:attribute name="quoteError" type="String"/>
<force:recordData aura:id="opportunityRecordCreator" 
                  layoutType="FULL"
                  targetRecord="{!v.newOpportunity}"
                  targetFields="{!v.simpleNewOpportunity}"
                  targetError="{!v.oppError}" 
                  mode="EDIT"
                  fields="Name, CloseDate, StageName/>

<force:recordData aura:id="quoteRecordCreator" 
                  layoutType="FULL"
                  targetRecord="{!v.newQuote}"
                  targetFields="{!v.simpleNewQuote}"
                  targetError="{!v.quoteError}" 
                  mode="EDIT"
                  fields="Id, Name, ExpirationDate, Discount, GrandTotal />

Lightning Controller Code -

doInit: function (component, event, helper) {
    // Prepare a new record from template for Opportunity
    component.find("opportunityRecordCreator").getNewRecord(
        "Opportunity", // sObject type (objectApiName)
        null,      // recordTypeId
        false,     // skip cache?
        $A.getCallback(function() {
            var rec = component.get("v.newOpportunity");
            var error = component.get("v.oppError");
            if(error || (rec === null)) {
                console.log("Error initializing opportunity: " + error);
                return;
            }
        })
    );

    // Prepare a new record from template for Quote
    component.find("quoteRecordCreator").getNewRecord(
        "Quote", // sObject type (objectApiName)
        null,      // recordTypeId
        false,     // skip cache?
        $A.getCallback(function() {
            var rec = component.get("v.newQuote");
            var error = component.get("v.quoteError");
            if(error || (rec === null)) {
                console.log("Error initializing Quote: " + error);
                return;
            }
        })
    );
},
handleSaveOpportunity: function (component, event, helper) {

    //Save opportunity
    component.find("opportunityRecordCreator").saveRecord(function(saveResult) {
        if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
            console.log('Record is saved successfully');
            var resultsToast = $A.get("e.force:showToast");
            resultsToast.setParams({
                "title": "Saved",
                "message": "The Opportunity record was saved."
            });
            resultsToast.fire();

            component.set('v.simpleNewQuote.OpportunityId', component.get('v.simpleNewOpportunity.Id'));
            console.log("OppId is>" + component.get('v.simpleNewOpportunity.Id'));
            console.log("New Quote Opportunity Id set>>>> ");
            helper.showProd(component);
        } else if (saveResult.state === "INCOMPLETE") {
            // handle the incomplete state
            console.log("User is offline, device doesn't support drafts.");
        } else if (saveResult.state === "ERROR") {
            // handle the error state
            console.log(JSON.stringify(saveResult.error));
            component.set("v.oppError", JSON.stringify(saveResult.error));
        } else {
            console.log(JSON.stringify(saveResult.error));
        }
    });
},

handleSaveQuote: function (component, event, helper) {
    //Initialize picklist values
    component.set("v.simpleNewQuote.M_S_Reason__c", component.find('M_S_Reason__c').get('v.value'));

    //Save Quote
    component.find("quoteRecordCreator").saveRecord(function(saveResult) {
        if (saveResult.state === "SUCCESS"||saveResult.state === "DRAFT") {
            console.log('Quote is saved successfully');
            var resultsToast = $A.get("e.force:showToast");
            resultsToast.setParams({
                "title": "Saved",
                "message": "The Quote record was saved."
            });
            resultsToast.fire();
            helper.showQLineItem(component);
        } else if (saveResult.state === "INCOMPLETE") {
            // handle the incomplete state
            console.log("User is offline, device doesn't support drafts.");
        } else if (saveResult.state === "ERROR") {
            // handle the error state
            console.log(JSON.stringify(saveResult.error));
            component.set("v.oppError", JSON.stringify(saveResult.error));
        } else {
            console.log(JSON.stringify(saveResult.error));
        }
    });

},

Is it possible to do the same for list of opportunity line items and quoteline items????

1

Working on list of records is not yet possible in Lightning data service. The article states

Support for working with collections of records or for querying for a record by anything other than the record ID isn’t available. If you must support higher-level operations or multiple operations in one transaction, use standard @AuraEnabled Apex methods.

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.