2

I hope someone can lend some advice on how to go about this issue. I believe it to be more of a strategy question.

After my code as been written and tested, we found a new field that was needed to be added to my SOQL query. The issue I am having: the field is a layer deep meaning I have to query: Signor_Contact_ID__r.SSN_Encrypted__C . And because of this I am getting back a JSON string that I would need to parse to get the SSN value out. I need to pass this JSON string to an external server.

My JSON string was clean before so I was able to just pull out index 0 and then serialize it. Like so:

    jk = new AccountWrapper(compName[0]);
    NewJSONString = JSON.serialize(jk); 
    //The results for NewJSONString are: {"SignorZipCode":"Test","SignorStreetAddress":"Test","SignorState":"Test","SignorSSN":null,"SignorLastName":"ASGAR","SignorHomePhone":"Test","SignorFirstName":"Test","SignorDateOfBirth":null,"SignorCity":"Test","PhysicalAddressZip":"Test","PhysicalAddressState":"Test","PhysicalAddressCity":"Test","PhysicalAddress1":"Test","FedTaxId":"Test","DoingBusinessAs":null,"CompanyPhone":"Test","CompanyLegalName":"Test"}

I have tried JSON2Apex.herokuapp and it gives me back 150 lines of code that ends up breaking a lot of other features. I have tried to pull out the SSN by looping through the list (before I serialize it) and storing the SSN in a variable. But I am not sure how to add the value on to the end of the list to be serialized later.

This is what the query should look like with the additional field:

compName = SELECT Signor_Contact_ID__r.SSN_Encrypted__C, DBA__c, Signor_Date_of_Birth__c, Phone__c, Fed_Tax_ID__c,Legal_Name__c,Physical_Address_1__c,Physical_City__c,Physical_State__c,Physical_Zip__c,Signor_City__c,Signor_First_Name__c,Signor_Home_Phone__c,Signor_Last_Name__c,Signor_State__c,Signor_Street_Address__c,Signor_Zip_Code__c 
                FROM Doc_Formulas__c WHERE Opportunity_Name__c = :oppId

The JSON string I get back looks like:

[
  {
    "attributes":
    {
      "type":"Doc_Formulas__c",
      "url":"test"
    },
      "Phone__c":"test",
      "Fed_Tax_ID__c":"test",
      "Legal_Name__c":"Test",
      "Physical_Address_1__c":"test",
      "Physical_City__c":"test",
      "Physical_State__c":"test",
      "Physical_Zip__c":"test",
      "Signor_City__c":"test",
      "Signor_First_Name__c":"test",
      "Signor_Home_Phone__c":"test",
      "Signor_Last_Name__c":"test",
      "Signor_Contact_ID__c":"test",
      "Signor_State__c":"test",
      "Signor_Street_Address__c":"test",
      "Signor_Zip_Code__c":"test",
      "Id":"test",
      "SSN_Encrypted__c":"test",
      "Signor_Contact_ID__r":
      {
        "attributes":
        {
          "type":"Contact",
          "url":"test"
        },
          "SSN_Encrypted__c":"test",
          "Id":"test",
          "RecordTypeId":"test"
        }
      }
    ]

And the error message I am getting when I try to run the query with the additional field is: Invalid field Signor_Contact_ID__r.SSN_Encrypted__C for Doc_Formulas__c

Which is strange to me because when I run the query in the Query Editor, I get back the correct response.

So my question is: how do I add this new field to the SOQL query, in a way that I am able to simply serialize the response, without having to parse the JSON String?

I know this is wordy, and might be too much to ask via internet. But if any one followed and has any insight on the next steps, I would really appreciate it!

Please let me know if you need any clarification.

1
  • Is there a particular reason why your external system needs the JSON to be more flat? I imagine that your external system has a built-in JSON parser, and that accessing the SSN information would simply be something like objFromJSON.Signor_Contact_ID__r.SSN_Encrypted__c
    – Derek F
    Oct 21, 2016 at 16:38

2 Answers 2

1

Without additional context, I think the simplest answer here is to use a formula field to pull in the encrypted SSN field from the Contact into your Doc_Formulas__c object.

The actual formula (for the formula field) would simply be Signor_Contact_ID__r.SSN_Encrypted__c.

If this new formula field were called Signor_SSN_Encrypted__c, simply including that field in your query would accomplish what you're describing.

Your JSON would then look like

{
    "attributes":
        {
           "type":"Doc_Formulas__c",
           "url":"test"
        },
    //other fields omitted
    "Signor_SSN_Encrypted__c": "testEncryptedSSN From Signor_Contact_Id record here"
}
1
  • unfortunately with our encrypted SSN's, we can not create formula fields. I wish it was that easy :(
    – Olivia
    Oct 21, 2016 at 19:28
0

I found a work around. I looped through the list and pulled out the SSN. I saved the SSN into a variable and then inserted the variable into a wrapper class. Like so:

public class AccountWrapper
{
    public String SignorLastName;
    public String SignorFirstName;
    public String **SignorSSN**;

    AccountWrapper(SObject sob)
    {
        this.SignorLastName =(String)sob.get('Signor_Last_Name__c');
        this.SignorFirstName = (String)sob.get('Signor_First_Name__c');
        **this.SignorSSN = signorSSN1;**
    }
} 

I then pulled out the 0 index, serialized it, and passed the data to the server.

jk = new AccountWrapper(compName[0]);  
NewJSONString = JSON.serialize(jk); 

So far it seems to work!

You must log in to answer this question.

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