• Ashish Garg
  • NEWBIE
  • 45 Points
  • Member since 2016
  • 5x Salesforce Certified


  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 11
    Replies
I'm building a web service from my salesforce app and I'm running into some challenges formatting the JSON response that the service will generate. Specifically, the JSON contains backslash characters "\" that I want to remove. I've tried a few approaches; none of which have worked. Here are Apex snippets of the approaches I've tried:

Approach 1: return String
List<Location__c> locationList = Database.query(q);
String locationJSON = JSON.serialize(locationList);                 
locationJSON = locationJSON.replaceAll('"Physical_Address_City__c"', '"City"');
//Do more find/replace to update the rest of the keys...
return locationJSON;

With Approach 1 I am able to do a find/replace to format the keys in the JSON. But the JSON contains backslash characters "\" and does not contain a response header.

Results:
(no response header)
"[{\"City\":\"New York\"}]
 

Approach 2: use RestConext method
List<Location__c> locationList = Database.query(q); 
RestContext.response.addHeader('Content-Type', 'application/json'); 
RestContext.response.responseBody = Blob.valueOf(JSON.serialize(locationList));
With Approach 2, I am able to generate a JSON with a header and no backslashes "\" but I don't know how to rename the key values (for instance, Physical_Address_City__c --> City) using this approach. 

Results:
(response header)
"[{"Physical_Address_City__c":"New York"}]



Approach 3: Combination of Approach 1 and 2
List<Location__c> locationList = Database.query(q);        
String locationJSON = JSON.serialize(locationList);                 
locationJSON = locationJSON.replaceAll('"Physical_Address_City__c"', '"City"'); 
​//Do more find/replace to update the rest of the keys...
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = Blob.valueOf(JSON.serialize(locationJSON));

With Approach 3 I am able to generate a JSON with the response Header and transform the keys (Physical_Address_City__c --> City) but the JSON body still contains the backslash characters "\". And I'm serializing the JSON twice, which doesn't seem right.

Results:
(response header)
"[{\"City\":\"New York\"}]



Does anyone have thoughts on how to write this more elegantly and also remove the backslash characters? Thank you!

Brian
I'm building a web service from my salesforce app and I'm running into some challenges formatting the JSON response that the service will generate. Specifically, the JSON contains backslash characters "\" that I want to remove. I've tried a few approaches; none of which have worked. Here are Apex snippets of the approaches I've tried:

Approach 1: return String
List<Location__c> locationList = Database.query(q);
String locationJSON = JSON.serialize(locationList);                 
locationJSON = locationJSON.replaceAll('"Physical_Address_City__c"', '"City"');
//Do more find/replace to update the rest of the keys...
return locationJSON;

With Approach 1 I am able to do a find/replace to format the keys in the JSON. But the JSON contains backslash characters "\" and does not contain a response header.

Results:
(no response header)
"[{\"City\":\"New York\"}]
 

Approach 2: use RestConext method
List<Location__c> locationList = Database.query(q); 
RestContext.response.addHeader('Content-Type', 'application/json'); 
RestContext.response.responseBody = Blob.valueOf(JSON.serialize(locationList));
With Approach 2, I am able to generate a JSON with a header and no backslashes "\" but I don't know how to rename the key values (for instance, Physical_Address_City__c --> City) using this approach. 

Results:
(response header)
"[{"Physical_Address_City__c":"New York"}]



Approach 3: Combination of Approach 1 and 2
List<Location__c> locationList = Database.query(q);        
String locationJSON = JSON.serialize(locationList);                 
locationJSON = locationJSON.replaceAll('"Physical_Address_City__c"', '"City"'); 
​//Do more find/replace to update the rest of the keys...
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = Blob.valueOf(JSON.serialize(locationJSON));

With Approach 3 I am able to generate a JSON with the response Header and transform the keys (Physical_Address_City__c --> City) but the JSON body still contains the backslash characters "\". And I'm serializing the JSON twice, which doesn't seem right.

Results:
(response header)
"[{\"City\":\"New York\"}]



Does anyone have thoughts on how to write this more elegantly and also remove the backslash characters? Thank you!

Brian
I have written a trigger is workin fine for small size number of records.but during bulk processing records is not working.i know the problem is soql is placed inside for loop because hitting the governor limits.how to resolve this issue?
here my code is ::
public static void setAccountingLookup(List<SRV_Service__c> newServices,Map<Id,SRV_Service__c> mservices) {
    string strCustclassification; // to store the customer classification values
    string strProdclassification;// to store product classification values
    string strAcccustclassification; // to store accounting customer classification values
    string strAccprodclassification;  // to store accounting product classification values
    string strServiceId;  // to store the serviceid

    List<SRV_Service__c>lstservices = new List<SRV_Service__c>();
    List<SRV_Service__c> lstservicesUpdate =new List<SRV_Service__c>();
    List<ACT_Accounting__c> lstaccounting = new List<ACT_Accounting__c>();
    
    List<SRV_Service__c>lstservices1 = new List<SRV_Service__c>();
      lstservicesUpdate =[select Id,ServiceType__c,SimpleProduct__r.ProductTax__c,Accounting__c,CarSet__r.Order__r.Account.CustomerVATType__c,CarSet__r.Order__r.Account.RecordType.Name,CBServicesRepository__r.ProductClassification__c from SRV_Service__c where id =:mservices.keySet()];
    
    system.debug('## lstservicesUpdate is..::'+ lstservicesUpdate);
   
    SRV_Service__c service = new SRV_Service__c();
    for(SRV_Service__c serviceObj : lstservicesUpdate ){
        service=serviceObj;
        if(service.ServiceType__c == Label.SRV_lblOption){
            //custclass to store the customer classification values from account object
            strCustclassification = service.CarSet__r.Order__r.Account.CustomerVATType__c;
            //prodclass to store the product classification values dynamically set the value
            strProdclassification = Label.OPT_lblProdclassvalue;

        }else if(service.ServiceType__c == Label.SRV_lblCoachBuildertype){
        //custclass to store the customer classification values from account object
            strCustclassification  = service.CarSet__r.Order__r.Account.CustomerVATType__c;
        //prodclass to store the product classification values from CBService repostory object  
            strProdclassification =service.CBServicesRepository__r.ProductClassification__c;

        }else if(service.ServiceType__c == Label.SRV_lblsimpleproduct){
            //custclass to store the customer classification values from account object
            strCustclassification = service.CarSet__r.Order__r.Account.CustomerVATType__c;
            //prodclass to store the product classification values from simple product object
            strProdclassification = service.SimpleProduct__r.ProductTax__c;
        }            

        
        system.debug('## prodclass is..::'+ strProdclassification);
        lstaccounting = [select id,CustomerTax__c,ProductTax__c from ACT_Accounting__c where CustomerTax__c=:strCustclassification AND ProductTax__c =:strProdclassification LIMIT 1];
              
        
        if(!lstaccounting.isEmpty()){
            service.Accounting__c= lstaccounting[0].Id; 
            system.debug('&&&&&&&&&&&&&&&'+service.Accounting__c);
           
        }

    }
    try {
          system.debug('## lstservicesUpdate is..::'+ service);
            update service;
        }catch (Exception ex) {
            system.debug('## Exception occured while updating Service Object:: ##'+ ex);
        }
   
    }  //end of method 
    
    
    }

 
I'm in the Trailhead challenge; "Create a formula field that determines the number of days between today and the last activity date for a case's account."

I have created the Case Field "Days Since Last Update"
and completed the formula field as;  TODAY()  -   Account.LastActivityDate

But the error message I get back is:
"There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Amount_bud_Non_Currency_geted__c]: [Amount_bud_Non_Currency_geted__c]"

What's wrong?
This code grabs only one of the Opportunity Line Item names and puts it into a field.
I need to add a loop to grab all Opportunity Line Items for an Opportunity
 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
var record = new sforce.SObject("Opportunity");
record.Id = '{!Opportunity.Id}';
var retriveOpptyLineItems = sforce.connection.query("Select PricebookEntry.Product2.Name From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}'");

record.Sample_Product_Name_1__c=retriveOpptyLineItems.records[0].PricebookEntry.Product2.Name; 

sforce.connection.update([record]); 
window.location.reload();

 
Hi guys,

Is it possible to add a table to an email template?
  • August 07, 2016
  • Like
  • 0
This "should" work by placing everything into one field (Sample_Product_Name_1). but it throws an "Invalid or Unexpected token" error
 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}

var record = new sforce.SObject("Opportunity");
record.oli ='{Select Amount,CloseDate,Name, 
     (Select PricebookEntry.Product2Id, 
             TotalPrice, 
             UnitPrice, 
             ListPrice 
      From OpportunityLineItems) 
   From Opportunity};

record.Id = '{!Opportunity.Id}';
record.Sample_Product_Name_1__c= record.oli;
sforce.connection.update([record]); window.location.reload();

 
I have successfully "pushed" Apex Class and Visualforce(VF) code changes via the Change Set 
mechanism between different Sandboxes, tested and everything is working fine.  

The closest I have managed to get to deploying Apex and VF changes to Production was uploading an Outbound Change Set to Production, and clicking 'VALIDATE' to the inbound Change Set in Production. The 'VALIDATE' operation worked fine reported no errors. 

(I managed to overcome the issue(without help) with Apex Test Class coverage, as it is a requirement that tests run >74% if attempting to deploy Apex code to Production ). I have tested rigourously numerous Outbound Change Sets and Inbound Change sets involving Apex and VF between Sandboxes with no issues.

The only step I need to take now is to DEPLOY the Apex and VF code to Production. Is it safe to assume that if I have tested the Apex and VF rigorously in the source Sandbox Org, uploaded the Outbound CS(Containing Apex and VF) to Production, VALIDATE the Inbound CS (with no errors/issues to report) in Production, then the actual DEPLOY operation to Production should work without a problem as the deployment is an all-or-nothing(atomic) operation?

Can anyone please reassure me, need to perform the Release soon, this is quite urgent!

Thanks!
 
Hello,

When I am executing my Apex tests, I get this error - can someone please help with this?

Thank you!

User-added image
  • August 05, 2016
  • Like
  • 0