0

I have a lightning component from which I pass some values to a controller. I want to pass the object as a variable and update a field on this object.

How do I make the object variable? See my example below:

Lightning

var action = component.get("c.updateRecord");
action.setParams({
    "objId": getCurrentRecord,
     "obj": object,
    "selection" : selectedValues,
    "fieldName" : field
});

Apex

@AuraEnabled
    public static object updateRecord(object obj, id objId , string selection, string fieldName){
        system.debug('obj-->' + obj);

        object objNew = obj;
        objNew objupdate = [SELECT id, fieldName FROM objNew WHERE id = :objId][0];
        objupdate.+fieldName = :selection;

        update objupdate;
        return objupdate;
    }
}

Update

@AuraEnabled
    public static string updateRecord(string objNew, id objId , string selection, string fieldName){
        sObject sObj = Schema.getGlobalDescribe().get(objNew).newSObject() ;
        sObj objupdate = [SELECT id, fieldName FROM sObj WHERE id = :objId];

        objupdate.+fieldName = :selection;

        update objupdate;
        return objupdate;
    }
  • what does this meant you are passing sObjectName or Object ` " object objNew = obj; objNew objupdate = [SELECT id, fieldName FROM objNew WHERE id = :objId][0];` it doesn't make sense to me – vignesh Aug 22 '18 at 13:04
  • @vignesh I want to pass the object as a string variable and update the corresponding Id. See my update – Thomas Aug 22 '18 at 13:34
1

You have two options here.

  • Create a object attribute in Lightning and use that to update the values.

<aura:attribute name="expense" type="Expense__c" default="{ 'sobjectType': 'Expense__c' }"/> and then get it in controller.

var expenses = component.get("v.expense");

var action = component.get("c.saveExpense");
  action.setParams({ 
      "expense": expense
  });
  if (callback) {
      action.setCallback(this, callback);
  }
  $A.enqueueAction(action);

Apex class

@AuraEnabled
public static Expense__c saveExpense(Expense__c expense) {
    // Perform isUpdateable() check here 
    upsert expense;
    return expense;
}
  • Second, if you are passing object API name in string, Then in apex class create new instance and use that

    sObject sObj = Schema.getGlobalDescribe().get(objNew).newSObject() ; sobj.put();//put the data //perform DML.

  • I want to pas the object indeed as a string. I tried this but get Invalid type see:public static string updateRecord(string objNew, id objId , string selection, string fieldName){ sObject sObj = Schema.getGlobalDescribe().get(objNew).newSObject() ; sObj objupdate = [SELECT id, fieldName FROM sObj WHERE id = :objId]; – Thomas Aug 22 '18 at 13:26
  • @Thomas you need dynamic SOQL here. It will work. – Tushar Sharma Aug 23 '18 at 4:23
0

Define a attribute in your component of type sobject whichever you want:-

<aura:attribute name="newContact" type="Contact"
        default="{ 'sobjectType': 'Contact' }" />

Then in your javaScript controller, call your method and set the parameter as follows:-

var saveContactAction = component.get("c.saveContactWithAccount");
            saveContactAction.setParams({
                "contact": component.get("v.newContact"), //you were doing wrong here
                "accountId": component.get("v.recordId")
            });

And in your apex controller you can get the parameter:-

 @AuraEnabled
    public static Contact saveContactWithAccount(Contact contact, Id accountId) {
        contact.AccountId = accountId;
        upsert contact;
        return contact;
    }

Hope it helps you.

  • This makes the object static. I want to have a variable object which I pas as a parameter trough the lightning design, see my update – Thomas Aug 22 '18 at 13:29

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.