0

So I am trying to create lightning component which changes my custom Object's (MyTask__c) Lookup(Contact) field from lightning:inputField which shows in my custom object page layout.

My custom object(MyTask__c haves Lookup(Contact) field.

I am getting Error message: List has no rows for assignment to SObject message in chrome console, even though I call

var action = component.get("c.changeTaskC");

action.setParams({"taskRecordId": component.get("v.recordId")});

action.setParams({"ContactId": component.find("firstContactId").get("v.value")});

as I should, the Apex Class still doesn't update MyTask__C record properly.

Here the code -

Lightning page :

<aura:component description="changeContact" implements="force:lightningQuickAction,force:hasRecordId" controller="changeContact">

        <lightning:recordEditForm objectApiName="MyTask__c" onsuccess="{!c.handleSuccess}">
            <lightning:inputField fieldName="Contact__c" aura:id="firstContactId" onchange="{!c.handleChange}"/>
            <div class="slds-m-top_medium">
                <lightning:button label="Choose Contact" type="submit" onclick="{!c.changeTasksContact}"/>
            </div>
        </lightning:recordEditForm>

</aura:component>

Lightning controller :

({
    handleChange : function(component, event, helper) {
        var lookupId = event.getParam("value")[0];
    },
    handleSuccess : function(component, event, helper) {
        var record = event.getParam("response");
        console.log('handleSucess function');
        var apiName = record.apiName; // returns "Case"
        var caseId = record.id; // returns the Id of the new record
    },

    changeTasksContact : function(component, event, helper) {
        helper.changeTasksContact_helper(component, event, helper);
    }
});

Lightning controller helper :

({

    changeTasksContact_helper : function(component, event, helper) {
        var action = component.get("c.changeTaskC");
        action.setParams({"taskRecordId": component.get("v.recordId")});
        action.setParams({"ContactId": component.find("firstContactId").get("v.value")});
        console.log('Task Id' + component.get("v.recordId"));
        console.log('Contact Id' + component.find("firstContactId").get("v.value"));

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // Alert the user with the value returned
                // from the server
                alert("From server: " + response.getReturnValue());

                // You would typically fire a event here to trigger
                // client-side notification that the server-side
                // action is complete
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " +
                            errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });

        $A.enqueueAction(action);

    }
});

Apex Controller :

public class changeContact {
    @AuraEnabled
    public string taskRecordId{get;set;}
    @AuraEnabled
    public string ContactId{get;set;}
    @AuraEnabled
    public static void changeTaskC(string taskRecordId, string ContactId){
        MyTask__c taskToUpdate = [SELECT Id,Name,Contact__c FROM MyTask__c WHERE Id=:taskRecordId];
            taskToUpdate.Contact__c = ContactId;
            update taskToUpdate;
    }

    public static void updateTaskC(){

    }
}

1 Answer 1

2

Pretty sure the below is incorrect:

action.setParams({"taskRecordId": component.get("v.recordId")});
action.setParams({"ContactId": component.find("firstContactId").get("v.value")});

It should instead be:

action.setParams({
    "taskRecordId": component.get("v.recordId"),
    "ContactId": component.find("firstContactId").get("v.value")
});
2
  • Could you explain why this action behave like that? And also thanks for answering this
    – Gediminas
    Jul 18, 2020 at 7:02
  • Has nothing to do with the action in particular. When you call setParams, it sets your entire parameter list to what gets passed in. So your second call just completely wipes out the first.
    – Adrian Larson
    Jul 18, 2020 at 7:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .