0

Trying to insert a record with string parameters, but facing an issue with string exception

Logs show out for debug logs:

DEBUG|@@@@:::String 1:::TESTING 3

DEBUG|@@@@:::String 2:::["0011H000023s57sQAA"]

@AuraEnabled

public static string insertRcd(String str1,String str2)
 {

    System.debug('@@@@:::String 1:::'+str1);

    System.debug('@@@@:::String 2:::'+str2); // record Id of Account - using JSON.stringify value is passed.

    BM_Suite__c obj = new BM_Suite__c();

        obj.Name = str1;         

        obj.Customer__c = Id.valueOf(str2);

        insert obj;

    return obj.id;         

}

3 Answers 3

2

str2 is not the string representation of an Id.

["0011H000023s57sQAA"]

It looks like this is the string representation of a one-length string array containing an Id.

Your JavaScript controller is sending the data incorrectly; you shouldn't need to stringify() an Id value. Instead, just access the first element of the array on the client-side controller, and pass that element directly to your Apex server call.

6
  • That is correct, from an array I selected a value. But how to access it in client side. @api selectedvalue = event.deatil; // passing this as parameter with stringify()
    – Akash
    Commented Jul 13, 2020 at 4:22
  • Get the first value (presumably) from the array, and send that to Apex.
    – David Reed
    Commented Jul 13, 2020 at 4:24
  • Can you provide an example how to use in Client side ? based on @api selectedvalue
    – Akash
    Commented Jul 13, 2020 at 4:27
  • An example of what? You just need to access an array element.
    – David Reed
    Commented Jul 13, 2020 at 4:31
  • YES in client side.
    – Akash
    Commented Jul 13, 2020 at 4:34
0

In your code, str seems to be [“0011H000023s57sQAA”]. Remove the brackets. You might be sending a list of Ids from your component.

2
  • I m selecting a record from list of values, it is maintains same format in JS. Can I get a solution on it.
    – Akash
    Commented Jul 13, 2020 at 4:21
  • I didn't quite understand your scenario. @Akash Commented Jul 26, 2020 at 14:27
0

This is how I could get assign value be deserialize the incoming value.

List lst = (List) System.JSON.deserialize(str2, List.class);

obj.Customer__c = lst[0];

You must log in to answer this question.

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