• Rahul Kumar Dey
  • NEWBIE
  • 85 Points
  • Member since 2018
  • Developer
  • TCS


  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 36
    Replies
Hi All,

I haven't worked much on integrations so i would like to know what's the difference in these 2 syntaxes below in generating json, i personally feel they do the same thing but i would like to know the differences and any other pros and cons of both the approaches and when to use which approach.

Generating json :
Approach 1
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeFieldName('Patient satisfaction');
gen.writeStartArray();
for (Patient_Satisfaction__c  patientSatisfaction : PrRecord) {
gen.writeStartObject();
gen.writeObjectField('First name', patientSatisfaction.Reporter_First_Name__c);
gen.writeObjectField('Last name', patientSatisfaction.Reporter_Last_Name__c);
gen.writeObjectField('Phone', patientSatisfaction.Reporter_Phone__c);
gen.writeObjectField('description', patientSatisfaction.Description_of_Feedback__c);gen.writeEndObject();
     } 
gen.writeEndArray();
gen.writeObjectField('incident_group_id',7387);
gen.writeObjectField('Name', 'TestAPI');
gen.writeEndObject();
//// This code snippet generates a json array

Approach2:
String jsonstr='{"Name":"'+ Accname +'","Phone":"'+ phone +'"}';

 Http h2= new Http();
//setting headers and other stuff required for callouts
//
//
req2.setBody(jsonstr);

            HttpResponse res2=h2.send(req2);
            System.debug(res2+'###1213createresp');
            deserializeResponse deresp2=(deserializeResponse)System.JSON.deserialize(res2.getBody(),deserializeResponse.class);



 
Hi All,

I have created a rest api webservice to insert contentVersion into salesforce but now I want to return the list of files to external system using rest api webservice. And I don't want to send exact files associated with case instead I want to send url so, external user can easily access  files from this url (list of url).
Any help how to proceed?
And any sample code really appreciate...


Best Regards,
Rahul 
Hi guys, 
I want to convert and REST API web-service synchronous to queable-
Here is the code which is perfectly worked for upload attachment upto 5.5 MB 64 encoded file. And I see in the document we can upload upto 12MB if this is in asynchronous in nature. 
 
@RestResource(urlMapping='/insertCaseWithAttachmentRestService/*')
global with sharing class insertCaseWithAttachmentRestAPI {
    //In this method we insert case with multiple attached files
   @httpPost
    global static Id insertCase(caseRequest req){
        Case caseObj = new Case();
        caseObj.Product__c = req.pdt;
        caseObj.Type = req.type;
        caseObj.Status = req.status;
        caseObj.Origin = req.origin;
        caseObj.Subject = req.subject;
        insert caseObj;
        system.debug('CaseId->>>>'+caseObj.Id);
        
        List<Attachment> attList = new List<Attachment>();
      
        for(attachmentRequest att : req.attList){
            attList.add(new Attachment(Name=att.attachmentName, 
                                      Body=EncodingUtil.base64Decode(att.blobString),
                                      ParentId=caseObj.Id
                                      ));
        }
        insert attList;
        
        return caseObj.Id;  
    }
    //Wrapper class
    global class caseRequest{
        String pdt {get; set;}
        String type {get; set;}
        String status {get; set;}
        String origin {get; set;}
        String subject {get; set;}
        
        List<attachmentRequest> attList {get; set;}
    }
    global class attachmentRequest{
        String attachmentName {get; set;}
        String blobString {get; set;}
    }
}

Any help to convert this into queueable interface?

Thanks in advance
Hi guys,

I want to insert attachments and combine this attachments with custom object 'Grade__c' in related list using Rest API web service.
I have created webservice which insert Grade object records but unable to insert attachment at same record-

Here is my code -
@RestResource(urlMapping='/UploadAttachmentRestService/*')
global with sharing class UploadAttachmentRestAPI {
    //In this method we insert custom object(Grade__c) records with attachments, comes from response body
    @httpPost
    global static Id insertGrade(String dt, Integer score, String name){
        Grade__c gradeObj = new Grade__c();
        gradeObj.Date__c = dt;
        gradeObj.Score__c = score;
        gradeObj.Name = name;
        
        insert gradeObj;
        system.debug('Grade ID->>>>'+gradeObj.Id);
        return gradeObj.Id;
       
    }

request body that i pass from workbench -
{
   "dt":"2016-12-10", 
   "score":9876, 
   "name":"Test Grade"
}

And the 'Test Grade' will Inserted.

any solution for attachment part, much appreciate.

Regards,
Rahul 
Hi guys , can anyone share me the recruiting app .pdf file, I am unable to get this file because Salesforce site says me this document retaired... Email: deyrahul.7687@gmail.com
Thaks & Regards, 
Rahul
Hi All,

I have created a rest api webservice to insert contentVersion into salesforce but now I want to return the list of files to external system using rest api webservice. And I don't want to send exact files associated with case instead I want to send url so, external user can easily access  files from this url (list of url).
Any help how to proceed?
And any sample code really appreciate...


Best Regards,
Rahul 
Hi guys, 
I want to convert and REST API web-service synchronous to queable-
Here is the code which is perfectly worked for upload attachment upto 5.5 MB 64 encoded file. And I see in the document we can upload upto 12MB if this is in asynchronous in nature. 
 
@RestResource(urlMapping='/insertCaseWithAttachmentRestService/*')
global with sharing class insertCaseWithAttachmentRestAPI {
    //In this method we insert case with multiple attached files
   @httpPost
    global static Id insertCase(caseRequest req){
        Case caseObj = new Case();
        caseObj.Product__c = req.pdt;
        caseObj.Type = req.type;
        caseObj.Status = req.status;
        caseObj.Origin = req.origin;
        caseObj.Subject = req.subject;
        insert caseObj;
        system.debug('CaseId->>>>'+caseObj.Id);
        
        List<Attachment> attList = new List<Attachment>();
      
        for(attachmentRequest att : req.attList){
            attList.add(new Attachment(Name=att.attachmentName, 
                                      Body=EncodingUtil.base64Decode(att.blobString),
                                      ParentId=caseObj.Id
                                      ));
        }
        insert attList;
        
        return caseObj.Id;  
    }
    //Wrapper class
    global class caseRequest{
        String pdt {get; set;}
        String type {get; set;}
        String status {get; set;}
        String origin {get; set;}
        String subject {get; set;}
        
        List<attachmentRequest> attList {get; set;}
    }
    global class attachmentRequest{
        String attachmentName {get; set;}
        String blobString {get; set;}
    }
}

Any help to convert this into queueable interface?

Thanks in advance
Hi guys,

I want to insert attachments and combine this attachments with custom object 'Grade__c' in related list using Rest API web service.
I have created webservice which insert Grade object records but unable to insert attachment at same record-

Here is my code -
@RestResource(urlMapping='/UploadAttachmentRestService/*')
global with sharing class UploadAttachmentRestAPI {
    //In this method we insert custom object(Grade__c) records with attachments, comes from response body
    @httpPost
    global static Id insertGrade(String dt, Integer score, String name){
        Grade__c gradeObj = new Grade__c();
        gradeObj.Date__c = dt;
        gradeObj.Score__c = score;
        gradeObj.Name = name;
        
        insert gradeObj;
        system.debug('Grade ID->>>>'+gradeObj.Id);
        return gradeObj.Id;
       
    }

request body that i pass from workbench -
{
   "dt":"2016-12-10", 
   "score":9876, 
   "name":"Test Grade"
}

And the 'Test Grade' will Inserted.

any solution for attachment part, much appreciate.

Regards,
Rahul 
How to get all the SObjects fields and datatype in single table in an org
I'm not able to select my custom object from New Custom Object Tab - it's not in the list (Property). I may have not selected the proper option when creating the custom object. (Launch new custom tab wizard?) The existing solutions don’t appear to match the current salesforce. How can I add the custom tab now that the custom object is already created? The custom object has a lot of data/object dependencies that make re-creating it difficult at this point. I'm trying to complete the Salesforce Mobile App Customization >Create Object-Specific Quick Actions training module.
I am getting below error after creating lighting app in vs code

User-added image
  • April 10, 2020
  • Like
  • 0
Hi All,

I haven't worked much on integrations so i would like to know what's the difference in these 2 syntaxes below in generating json, i personally feel they do the same thing but i would like to know the differences and any other pros and cons of both the approaches and when to use which approach.

Generating json :
Approach 1
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeFieldName('Patient satisfaction');
gen.writeStartArray();
for (Patient_Satisfaction__c  patientSatisfaction : PrRecord) {
gen.writeStartObject();
gen.writeObjectField('First name', patientSatisfaction.Reporter_First_Name__c);
gen.writeObjectField('Last name', patientSatisfaction.Reporter_Last_Name__c);
gen.writeObjectField('Phone', patientSatisfaction.Reporter_Phone__c);
gen.writeObjectField('description', patientSatisfaction.Description_of_Feedback__c);gen.writeEndObject();
     } 
gen.writeEndArray();
gen.writeObjectField('incident_group_id',7387);
gen.writeObjectField('Name', 'TestAPI');
gen.writeEndObject();
//// This code snippet generates a json array

Approach2:
String jsonstr='{"Name":"'+ Accname +'","Phone":"'+ phone +'"}';

 Http h2= new Http();
//setting headers and other stuff required for callouts
//
//
req2.setBody(jsonstr);

            HttpResponse res2=h2.send(req2);
            System.debug(res2+'###1213createresp');
            deserializeResponse deresp2=(deserializeResponse)System.JSON.deserialize(res2.getBody(),deserializeResponse.class);



 
A previous employee was responsible for the support portal and had created an alert whenever a customer account created a case or an update occured with that case. It works for the owner of the account but the solutions architect needs to receive the email alert too. We have no idea how to do this; can someone please assist?
I'm new in apex I tried my best. Here is my requirement We are sending a Post request to External system and we want salesforce fields to go to external system along with external system required fields(Name and Incident_group_id). I'm not able to conctenate Salesforce fields along with external system required field in Json format. Not sure what is wrong in the code. I'm calling this code in trigger. sorry for messy code, still trying to figure out apex. It doesn't send Json object (Patient satisfaction) it just sends incident_group_id and Name(both external system fields)

public class RadarUpdate {
@future (callout=true)
public static void postcallout(string id) { 
List<Patient_Satisfaction__c> PrIds = new list<Patient_Satisfaction__c>();
List<Patient_Satisfaction__c> PrRecord  = [select id, Name, Reporter_First_Name__c, Reporter_Last_Name__c, 
Reporter_Phone__c, Description_of_Feedback__c from Patient_Satisfaction__c where id IN :PrIds ] ;
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeFieldName('Patient satisfaction');
gen.writeStartArray();
for (Patient_Satisfaction__c  patientSatisfaction : PrRecord) {
gen.writeStartObject();
gen.writeObjectField('First name', patientSatisfaction.Reporter_First_Name__c);
gen.writeObjectField('Last name', patientSatisfaction.Reporter_Last_Name__c);
gen.writeObjectField('Phone', patientSatisfaction.Reporter_Phone__c);
gen.writeObjectField('description', patientSatisfaction.Description_of_Feedback__c);gen.writeEndObject();
     } 
gen.writeEndArray();
gen.writeObjectField('incident_group_id',7387);
gen.writeObjectField('Name', 'TestAPI');
gen.writeEndObject();
String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);
  
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.radarfirst.com/incidents');
request.setMethod('POST');
request.setHeader('Content-Type','application/json;charset=UTF-8');
request.setHeader('User-agent', 'Salesforce-integration-client');
request.setHeader('Authorization','Bearer abc');
request.setBody(jsonS);
// Set the body as a JSON object
HttpResponse response = http.send(request);
if (response.getStatusCode() != 422) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}
 
Hello All,
                I want to create a test class for a class in which I used callouts.
Can anyone help me how to write test class for API?
my class is given below:-
public without sharing class ZoomCalloutService {
        //Get Request
    public static httpResponse getCallout(String httpMethod, String endpoint){
        httpRequest req = new httpRequest();
        req.setMethod(httpMethod);
        req.setEndpoint(endpoint);
        req.setHeader('Content-type', 'application/json');
        if(ZoomJWTAuthentication.authToken != Null){
            req.setHeader('Authorization','Bearer '+ZoomJWTAuthentication.authToken);
        }else{
            req.setHeader('Authorization','Bearer ' +getAccessToken());
        }
        System.debug(ZoomJWTAuthentication.authToken);    
        req.setTimeout(120000);
        httpResponse res = new http().send(req);
        if(res.getStatusCode() == 401){
            req.setHeader('Authorization','Bearer '+getAccessToken());
            httpResponse res1 = new http().send(req);
            return res1;
        }
        return res;
        
    }
    
    //Post Request
    public static httpResponse postCallout(String httpMethod, String endpoint, String body){
        httpRequest req = new httpRequest();
        req.setMethod(httpMethod);
        req.setEndpoint(endpoint);
        req.setBody(body);
        req.setHeader('Content-type', 'application/json');
        if(ZoomJWTAuthentication.authToken != Null){
            req.setHeader('Authorization','Bearer '+ZoomJWTAuthentication.authToken);
        }else{
            req.setHeader('Authorization','Bearer ' +getAccessToken());
        }
        System.debug(ZoomJWTAuthentication.authToken);    
        req.setTimeout(120000);
        httpResponse res = new http().send(req);
        if(res.getStatusCode() == 401){
            req.setHeader('Authorization','Bearer '+getAccessToken());
            httpResponse res1 = new http().send(req);
            return res1;
        }
        return res;
        
    }
    
    public static String getAccessToken(){
        return ZoomJWTAuthentication.createTokenFromJWTAuth();
    }
}
Can anyone help me with the test class?
Question about design patterns for Lighting Aura components.  I'm working on a feature that will have some nested components.  Like a main component, a couple components that manage lists, and a couple components that present an item for the list.

The main component will have a back-end Apex controller in Apex. 

Is the best patern to also declare some of the inner components to have their own Apex.  For example, list component would call their controller to get data.   

Or would be be better for the inner components to always use the controller for the main component, and if so, is there a example of a coding pattern for doing so?

I seem to remember a lecture on Lightning Aura components when they were new covering this topic and having examples but since it was a couple years ago, I don't recall where I saw it.
Hi,

As I am able to update on single records but not working when its on bulk.

here is my code///
public class UpdateActivityHistoryCallCount {
    
    
    //To update the Count of Activity History in Lead object
    
    public static void countActivityHistoryOnLead(list<Task> newTaskList,list<Task> oldTaskList){
        set<Id> LeadIds=new set<Id>();
        list<Lead> leadList=new list<Lead>();
        Map<Id, String> error = new Map<Id, String>();
        
        if(trigger.isInsert || trigger.isUnDelete || trigger.isUpdate){
            for(Task tsk:newTaskList){
                if(string.valueOf(tsk.WhoId).startsWith('00Q'))
                    LeadIds.add(tsk.WhoId);
                
            }
        }
        if(trigger.isDelete || trigger.isUpdate){
            for(Task tk:oldTaskList){
                if(string.valueOf(tk.WhoId).startsWith('00Q'))
                    LeadIds.add(tk.whoId);
                
            }
        }
        if(LeadIds.size()>0){
            for(Lead l:[select id,(select id,subject,Due_Date__c,CreatedDate
                                   from tasks where (subject='Call' and status='Completed'
                                                     and  Due_Date__c < Today) or (subject='Call' and status='Completed' and Due_Date__c =  null)  ) 
                        from lead where id in :LeadIds limit 50000])
                leadList.add(new lead(id=l.Id,Call_count__c=l.tasks.size()));
            
            
        }
        Database.SaveResult[] results= database.update (leadList,false);
        system.debug('@@@@' +results);
        
    }
    
}

Thanks