4

In my apex controller I have the following method:

@AuraEnabled
public static Incident__c saveIncident(String name) {
    Incident__c incident = new Incident__c(Name = name);
    upsert incident;
    return incident;
}

What I would like to achieve is that instead of passing the "Name", I would pass a hash which will set all the attributes. I don't know which parameters are gonna be sent from the client side. Maybe just 'Name', maybe 'Name' and 'Description' and 'SomeCustomField__c', Is there a way I can upsert an instance with the incoming variables where I don't need to specify exactly which ones are coming in? I understand I need to use deserialize for the JSON, but after the deserialization, how do I set the data on to the instance? In Rails, I can do something like this:

opts = { Name: name, Id: "123gbbb" }
new Incident__c(opts)

How can I achieve this in Apex?

Thanks

UPDATE:

This is now my controller:

public with sharing class IncidentsController {
    @AuraEnabled
    public static Incident__c saveIncident(String jsonString) {
        IncidentDetails deserializeResults = (IncidentDetails)JSON.deserialize(jsonString, IncidentDetails.class);        
        Incident__c incident = new Incident__c();
        upsert incident;
        return incident;
    }

    public Class IncidentDetails {
       public String Name;
       public String Secondfield;
    }
}

Now the question is how do I pass deserializeResults into the incident variable so I can upsert it?

1
  • use JSON.deserialize in your apex code
    – cropredy
    Commented Jan 18, 2016 at 17:05

2 Answers 2

3

we use JSON.deserialize to convert JSON object passed from your aura component. Your apex controller will create one JSON parser class which will convert your JSON object into apex class object.

  1. Create a new Apex class inside your current controller

    public class saveIncidentDetails {
       @AuraEnabled
       public static Incident__c saveIncident(String passedJSON) {
    
         /**********Apex object transform class***********/
         IncidentDetails ins = (IncidentDetails)          
         JSON.deserialize(passedJSON,IncidentDetails.class);
    
         Incident__c incident = new Incident__c();
         if(ins.name!=null)
               incident.name=ins.name;
         if(ins.Secondfield!=null)
               incident.Secondfield=ins.Secondfield;
         upsert incident;
         return incident;
       }
    
    
         public Class IncidentDetails
         {
           public String Name;
           public String Secondfield;
         }
    
    }
    
  2. Aura component

        var action = component.get("c.saveIncident");
        var person = {name:"tom", IncidentDetails:"[email protected]"};
        person = JSON.stringify(person);
        action.setParams({
          "passedJSON" : person
        });
    
7
  • See my updated code. Commented Jan 19, 2016 at 7:54
  • you can access using deserializeResults.Name and deserializeResults .id respectively.
    – Himanshu
    Commented Jan 19, 2016 at 8:19
  • I understand that. But that would mean that I would have to do something like this: Incident__c incident = new Incident__c(Name = deserializeResults.Name) However, I don't know what will be the keys in the deserializedResults. Maybe it's Name and Id, maybe it's Name, Id, Descrtiption, etc... Commented Jan 19, 2016 at 9:38
  • can you explain more in your question about this.
    – Himanshu
    Commented Jan 19, 2016 at 13:19
  • I edited my original post. Commented Jan 19, 2016 at 13:30
1

Assuming the field names you are passing in your JSON match those in your custom object you can deserialise directly into an instance of it.

Account anAccount = (Account)JSON.deserialize('{"Name": "Account Name" }}', Account.class);

No need for any extra classes. So for your situation you just need to do the following:

@AuraEnabled
public static Incident__c saveIncident(String jsonString) {
    Incident__c incident = (Incident__c)JSON.deserialize(jsonString, Incident__c.class);
    upsert incident;
    return incident;
}

You must log in to answer this question.

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