0

I have a simple component that fetches a record and attempts to update it.

I am trying to update the "part count" and it shows that it's being updated client side and that the params are being sent to the database but it's not actually updating the record..

here is my code:

component:

<aura:attribute name="partInfo" type="Part_Inventory__c[]"/>

<form>
    <ui:inputText label="Enter Part #" aura:id="sInput"/>
    <lightning:button label="Search" onclick="{! c.searchPart }" />
</form>
Part Details:

<aura:iteration items="{!v.partInfo}" var="obj">
    <!-- Display names of all accounts -->
    Name: {!obj.Name}, Description: {!obj.Part_Description__c}, Count: {!obj.Part_Count__c}
    <ui:inputNumber label="{! 'Enter Count For Part:' +obj.Name}" value="{!obj.Part_Count__c}" aura:id="cInput"/>
    <lightning:button label="Enter Count" onclick="{! c.savePartCount }" />+
    Name: {!obj.Name}, Description: {!obj.Part_Description__c}, Count: {!obj.Part_Count__c}
</aura:iteration>

public with sharing class PartController {

@AuraEnabled
public static List<Part_Inventory__c> getPartInfo(String searchTerm){
           return [SELECT Name,Part_Description__c, Part_Count__c
            FROM Part_Inventory__c
            WHERE Part_Inventory__c.Name LIKE: searchTerm];
}


@AuraEnabled
public static Part_Inventory__c saveCount(Part_Inventory__c updatedPartCount){
        upsert updatedPartCount;
        return updatedPartCount;
}

}

({
searchPart : function(component, event, helper) {

    var searchFor=component.find("sInput");
    var searchValue=searchFor.get("v.value");
    var action= component.get("c.getPartInfo");
    action.setParams({"searchTerm": searchValue });

    // Add callback behavior for when response is received
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (component.isValid() && state === "SUCCESS") {
            console.log(response.getReturnValue());
            component.set("v.partInfo", response.getReturnValue());
        }
        else {
            console.log("Failed with state: " + state);          
        }
    });
    $A.enqueueAction(action);


},
 savePartCount: function (component, event, helper){

     var action = component.get("c.saveCount");
     var myPartInfo = component.get("v.partInfo");

     console.log(myPartInfo);

     action.setParams({ "updatedPartCount": myPartInfo });
     action.setCallback(this, function() {  console.log('SAVED.');  } );
     $A.enqueueAction(action);
    }

})

Some screenshots: enter image description here

enter image description here

now I have attempted to update the count to 15 from 10 and this is what the console says: Note- When I go back into the object view, the value is still 10.

  • 4
    Can you debug what is passed into the apex method for the updatedPartCount property to see what the apex method receives. – Eric Aug 23 '17 at 17:00
  • Add the same sort of response checking that you have in searchPart in savePartCount; some useful error information may be coming back but you are ignoring it and instead outputting a message that suggests the save has worked. Also output response.getError(). – Keith C Aug 23 '17 at 17:25
  • All you will get is an internal server error on the upsert if you don't wrap it in a try{}catch(){}. Add that and then console log the response. – Caspar Harmer Aug 23 '17 at 21:05
  • try changing your apex class method argument to String type and pass json format of record from Js controller. In apex method, parse the Json to the particular Object and use it. – Ashish Sharma Aug 24 '17 at 2:15
0

try updating your component number input as below

<ui:inputNumber updateon="keyup" label="{! 'Enter Count For Part:' +obj.Name}" value="{!obj.Part_Count__c}" aura:id="cInput"/>

consider changing your JavaScript controller code as below:

         var action = component.get("c.saveCount");
         var myPartInfo = component.get("v.partInfo");
         console.log(myPartInfo);
         action.setParams({ "updatedPartCount": JSON.stringify(myPartInfo)});
         action.setCallback(this, function(response) {
            if(response.getState() === "SUCCESS"){
               console.log('SAVED.');  
               console.log('updated record is'+JSON.parse(response.getReturnValue()));
            }
         });
         $A.enqueueAction(action);

and change your APEX method as below:

@AuraEnabled
public static String saveCount(String updatedPartCount){
   Part_Inventory__c updatedPartCount = (Part_Inventory__c) JSON.deserialize(updatedPartCount, Part_Inventory__c)
   upsert updatedPartCount;
   return JSON.serialize(updatedPartCount);
}
|improve this answer|||||

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.