function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajanRajan 

Getting error in VF page while parsing JSON

Hi Friends,
My JSOn is - {"InvalidData"{"userType":null,"status":"success","sessionToken":null,"name":null,"message":"success","mappingId":null,"emailId":null,"data":null,"code":1000}}

But VF is returning - {"InvalidData":{"userType":null,"status":"success","sessionToken":null,"name":null,"message":"success","mappingId":null,"emailId":null,"data":null,"code":1000}}


So how to remove this &quot so that I can get the correct JSON as per my class.

My method is as below:
****************************
@RemoteAction
    global static String inValid(integer dataId,boolean contactValid, boolean productValid,
                                 boolean actionValid,String other){
      
       integer contact = 0;
       integer action = 0;
       integer product = 0;
       if(contactValid == true){
        contact = 1;                
       }
       if(productValid == true){
        product = 1;
       }
       if(actionValid == true){
        action = 1;
       }

       SalesAIRecommendations.Item response = new SalesAIRecommendations.Item();
        Recommendation__c recommendation = [SELECT Id,Recom_External_Id__c,Feedback__c FROM Recommendation__c where Recom_External_Id__c =: dataId];
                                            
        if(recommendation != null){
            system.debug('valid recommendation Id');
            recommendation.Feedback__c = other;
            update recommendation;
          
            Invalid_Recommendation__c invRec = new Invalid_Recommendation__c();
            invRec.Recommendation_ID__c = recommendation.Recom_External_Id__c;
            invRec.productChange__c = product;
            invRec.Next_Best_Action__c = action;
            invRec.contactId__c = contact;
            invRec.Others__c = recommendation.Feedback__c;
            insert invRec;
            response.status='success';
            response.code=1000;
            response.message = 'success';
          
            system.debug('{"InvalidData":' + JSON.serialize(response) + '}');
            return '{"InvalidData":' + JSON.serialize(response) + '}';
        }
        else{
            system.debug('invalid recommendation Id');   
            response.status='failure';
            response.code=1001;
            response.message = 'failure';
            system.debug('******:'  + '{"InvalidData":' + JSON.serialize(response) + '}');
            return '{"InvalidData":' + JSON.serialize(response) + '}';
                        
              }
    }
 
Best Answer chosen by Rajan
Nayana KNayana K
<script type="text/javascript">
function getRemoteAccount() {
    var accountName = document.getElementById('acctSearch').value;

    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.MyController.getAccount}', 
        accountName, 
        function(result, event){
            if (event.status) {
                document.getElementById('acctId').innerHTML = result.Id
                document.getElementById('acctName').innerHTML = result.Name;
            } else if (event.type === 'exception') {
                document.getElementById("responseErrors").innerHTML = event.message;
            } else {
                document.getElementById("responseErrors").innerHTML = event.message;
            }
        }, 
        {escape: true}
    );
}
</script>

Your VF remoting code in VF will be smilar(Syntax wise) to above code. 

If you make {escape: false} then, auto html encoding will be disabled and VF will return correct JSON.

All Answers

Nayana KNayana K
<script type="text/javascript">
function getRemoteAccount() {
    var accountName = document.getElementById('acctSearch').value;

    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.MyController.getAccount}', 
        accountName, 
        function(result, event){
            if (event.status) {
                document.getElementById('acctId').innerHTML = result.Id
                document.getElementById('acctName').innerHTML = result.Name;
            } else if (event.type === 'exception') {
                document.getElementById("responseErrors").innerHTML = event.message;
            } else {
                document.getElementById("responseErrors").innerHTML = event.message;
            }
        }, 
        {escape: true}
    );
}
</script>

Your VF remoting code in VF will be smilar(Syntax wise) to above code. 

If you make {escape: false} then, auto html encoding will be disabled and VF will return correct JSON.
This was selected as the best answer
sravan velamasravan velama
Hi Rajan,

Try to replace the &quot; with "

For example :
The JSON = [ { &quot;id&quot;: 123, &quot;text&quot;: &quot;Consumer&quot;, &quot;parent&quot;: &quot;#&quot; } ]
Then, use JSON.replace(/&quot;/g,'"'));
system.debug('Result :' + JSON);
You can notice that all the &quot; are replaced.

Thanks,
Sravan
 
RajanRajan
Thank you @Nayana
You have solved this issue. It was coming because escape was true so I changed it to false as {escape: false}.
Thanks alot for resolving this issue because I was trying from last 1 day.
RajanRajan
Hi Nayana,
Can you please help for one requirement.
I have to design a custom button for converting records in opportunity.
I have a table in which I am displaying data from Account, contact and 3 custom objects like attached pic. Whenever I'll select any record from this table using checkbox and click on that button then it should create opportunity. Any suggestion or sample code will be helpful.

User-added image
Nayana KNayana K
I am assuming you have used wrapper class list to display this table.

https://pritamshekhawat.wordpress.com/tag/wrapper-class/

Check above link, you may get some idea.
RajanRajan
Yes, I'm using wrapper class for this
Arpit BajpaiArpit Bajpai
Hello @Nayana K

I am using the {escape: false} but now i am getting the below json in VF remote.

newCustomer={\"salutation\":\"Mr\",\"firstName\":\"Arpit\",\"lastName\":\"Test\",\"primaryPhone\":\"1234\",\"department\":\"Management\",\"companyName\":\"test\",\"vatNumber\":\"1234\",\"language\":\"en_US\",\"currencyCode\":\"EUR\",\"billingAddress\":{\"address1\":\"test\",\"postalCode\":\"1234\",\"city\":\"test\",\"stateCode\":\"AL\",\"countryCode\":\"US\"},\"username\":\"ARBAJPAI\",\"termsAndConditionFlag\":\"on\",\" \":\"\",\"state\":\"AL\",\"country\":\"US\",\"terms\":true,\"newsLetter\":false,\"companyType\":\"ISP\"}}

any help is appreciated how to resolve this issue