0

I've got a component that essentially creates a lead. In another component I retrieve an account ID, and I wish to store this account ID (which is a lookup field on the lead). For some reason I just can't get the field to be populated - it might be some issues with callbacks or so, because I'm retrieving the correct Ids if I do a few console.logs but nothing gets populated

This is what my code looks like right now.

Markup (salesAppointment is an aggregrated object where I essentially store everything)

<aura:attribute name="salesAppointmentId" type="Id" access="private"  />
<aura:attribute name="salesAppointmentRecord" type="Sales_Appointment__c" />
<force:recordData aura:id="forceRecord"
                  recordId="{!v.salesAppointmentId}"
                  targetFields="{!v.salesAppointmentRecord}"
                  fields="Resource__c, Dealership__c, Lead__c, Start__c, End__c"
                  mode="EDIT" />

<!-- Lead creation -->
<force:recordData aura:id="accountRecordCreator"
                  layoutType="FULL"
                  targetRecord="{!v.newAccount}"
                  targetFields="{!v.simpleNewAccount}"
                  targetError="{!v.newAccountError}" />

(There are some lightning inputs here for the lead creation in the markup but not necessary for show)

Controller:

doInit: function(component, event, helper) {
    // Prepare a new record from template
    component.find("accountRecordCreator").getNewRecord(
        "Lead", // sObject type (objectApiName)
        null,      // recordTypeId
        false,     // skip cache?
        $A.getCallback(function() {
            var rec = component.get("v.newAccount");
            var error = component.get("v.newAccountError");
            if(error || (rec === null)) {
                console.log("Error initializing record template: " + error);
                return;
            }
            console.log("Record template initialized: " + rec.sobjectType);
        })
    );
    component.find("forceRecord").getNewRecord(
            "Sales_Appointment__c",
            null,
            false,
            $A.getCallback(function() {
                var rec = component.get("v.forceRecord");
                var error = component.get("v.recordError");
                if (error || (rec === null)) {
                    console.log("Error initializing record template: " + error);
                    return;
                }
            })
        );
},

Helper:

createLead: function(component, event, helper) {
    component.find("accountRecordCreator").saveRecord($A.getCallback(function(saveResult) {
        if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
            // record is saved successfully
            var customerAccountId = saveResult.recordId; // Gets the record ID of the lead created
            var appEvent = $A.get("e.c:CustomerSubmitForm");
            appEvent.setParams({
                customerAccountId: customerAccountId // customerAccountId in the CustomerSubmitForm event is now set to the ID that was just created
            });

            <!-- SOMEWHERE UP HERE, I ASSUME, IS WHERE I WANT TO STORE THE ACCOUNT ID THAT I'VE FETCHED FROM ANOTHER COMPONENT -->
            appEvent.fire(); // Fires the event to store customerAccountId in the Event "CustomerSubmitForm.evt"
            console.log(customerAccountId);
            helper.saveRecord(component, event, helper, customerAccountId);

        }
        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('Problem saving contact, error: ' + JSON.stringify(saveResult.error));
            var resultsToast = $A.get("e.force:showToast");
            resultToast.setParams({
                "title": "Error",
                "message": "Please complete the form"
            })
            resultsToast.fire();
        }
        else {
            console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
        }
    }));
},


saveRecord : function(component, event, helper, customerAccountId) {
    var resId = component.get("v.resourceId"); // Resource Id aka the car
    var accId = component.get("v.accountId"); // Account ID for the Dealership
    var leadId = component.get("v.customerAccountId"); // Id of the lead created above


    component.set("v.salesAppointmentRecord.Resource__c", resId);
    component.set("v.salesAppointmentRecord.Dealership__c", accId);
    component.set("v.salesAppointmentRecord.Lead__c", leadId);
    var tempRec = component.find("forceRecord");
    tempRec.saveRecord($A.getCallback(function(result) {
        console.log(result.state);
        var resultsToast = $A.get("e.force:showToast");
        if (result.state === "SUCCESS") {
            resultsToast.setParams({
                "title": "Saved",
                "message": "The record was saved."
            });
            resultsToast.fire();

            var salesAppointmentId = result.recordId;
            helper.navigateTo(component, salesAppointmentId);

        } else if (result.state === "ERROR") {
            console.log('Error: ' + JSON.stringify(result.error));
            resultsToast.setParams({
                "title": "Error",
                "message": "There was an error saving the record: " + JSON.stringify(result.error)
            });
            resultsToast.fire();
        } else {
            console.log('Unknown problem, state: ' + result.state + ', error: ' + JSON.stringify(result.error));
        }
    }));
},

In the helper, on line 11, I wrote a comment, I assume that's where I'd put the lookup field ID (that I have stored in an application event). When I do so however, it doesn't work.

If the code is a bit unclear, this is what happens: We got a Sales Appointment Object that stores a few values (including a Lead, Account and some custom object). The lead and sales appointment itself are created at the same time. I wish to populate a lookup field that resides on the Sales Appointment on the Lead as well.

Appreciate all help I can get. Been stuck on this forever..

0

You are passing lead id as a customerAccountId attribute of your helper.saveRecord() method. Still in this method you are referencing component attribute to pull lead id and I don't see this attribute in your markup or any place in code where you are setting value in it. I guess following update in your helper method should fix the issue:

var leadId = customerAccountId;

instead of

var leadId = component.get("v.customerAccountId");
  • var leadId = customerAccountId; is just a reference. A few lines above I'm declaring var leadId = component.get("v.customerAccountId"); // Id of the lead created above – erikvm May 29 '18 at 10:06
  • just to clarify, did I get it right that you don't have any value in leadId variable and you need it to set lookup on Sales Appointment? – Alexey Yezhkov May 29 '18 at 10:10
  • Yes, the lead itself is not created yet. In the helper, both sales appointment and lead is created at once (or well.. lead is first created and then on the sales appointment, that lead is populated). I'm wrapping the sales appointment record in a callback – erikvm May 29 '18 at 10:44
  • @erikvm, So what I mean is the following: in createLead method you get the id of new lead and put it into customerAccountId variable and passing it as parameter to saveRecord. In saveRecord method you are trying to pull this value using component.get("v.customerAccountId"); which is not correct as you don't store the id in component attribute, you can simply refer customerAccountId parameter – Alexey Yezhkov May 29 '18 at 12:00
  • Yes, that's correct. In the helper I have access to both Account ID and Lead ID. I wish it was as simple as going component.set("v.simpleNewAccount.Dealer_Name__c", accId); However, that's not working, even though that's exactly what needs to happen. Dealer Name is a lookup to Account, and the Account ID itself is stored in the "accId" variable – erikvm May 29 '18 at 12:01

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.