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?

  • use JSON.deserialize in your apex code – cropredy Jan 18 '16 at 17:05
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:"hangs@gmail.com"};
        person = JSON.stringify(person);
        action.setParams({
          "passedJSON" : person
        });
    
  • See my updated code. – eyal83 Jan 19 '16 at 7:54
  • you can access using deserializeResults.Name and deserializeResults .id respectively. – Himanshu Jan 19 '16 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... – eyal83 Jan 19 '16 at 9:38
  • can you explain more in your question about this. – Himanshu Jan 19 '16 at 13:19
  • I edited my original post. – eyal83 Jan 19 '16 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;
}

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.