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
DeepikareddyDeepikareddy 

how to pass a image in the rest callout in apex input file

i need to pass the data and image in the rest callout ? any help?
Best Answer chosen by Deepikareddy
Deepali KulshresthaDeepali Kulshrestha
Hi Deepika
 
Greetings to you!

you must create a rest class as given below:
@RestResource(urlMapping='/Webinar')
global class ObjectDataWithImage {
    @HttpGet
    global static void doGet() 
    {  
        try{
            List<CustomObjectWrapper> CustomObjectWrapperList = new  List<CustomObjectWrapper>();
            List<CustomObject__c> CustomObjectList = new List<CustomObject__c>();
            List<Attachment> AttachmentList = new List<Attachment>();
            
            Set<Id> CustomObjectIdsSet = new Set<Id>();
            
            CustomObjectList = [SELECT Id FROM CustomObject__c 
                                LIMIT 50000];
            
            if(CustomObjectList.size() > 0 ){
                for(CustomObject__c customObj : CustomObjectList){
                    CustomObjectIdsSet.add(customObj.Id);
                }
                AttachmentList = [SELECT Id, 
                                  Body, 
                                  Name,
                                  ParentId,
                                  ContentType
                                  FROM Attachment 
                                  WHERE ParentId IN: CustomObjectIdsSet 
                                  AND ContentType in ('image/png', 'image/jpeg', 'image/gif') 
                                  LIMIT 50000];
                
                for(integer i =0 ; i < CustomObjectList.size() ; i++){
                    CustomObjectWrapper CustomObjectWrapperObj = new CustomObjectWrapper();
                    CustomObjectWrapperObj.CustomObjectDetail = CustomObjectList[i];
                    if(AttachmentList.size() > 0){
                        for(integer j =0 ; j < AttachmentList.size() ; j++){
                            if(AttachmentList[j] != null && CustomObjectList[i].Id == AttachmentList[j].ParentId){
                                CustomObjectWrapperObj.Webinarimagebase64 = 'data:image/png;base64,';
                                CustomObjectWrapperObj.Webinarimagebase64 += EncodingUtil.base64Encode(AttachmentList[j].Body);
                                break;
                            }
                        } 
                    }
                    CustomObjectWrapperList.add(CustomObjectWrapperObj);   
                }
            }
            System.debug('CustomObjectWrapperList-->'+CustomObjectWrapperList);
             if(CustomObjectWrapperList.size() > 0 ){
                RestContext.response.statusCode = 200;
                RestContext.response.addHeader('Content-Type', 'text/plain');
                RestContext.response.responseBody = Blob.valueOf(JSON.serializePretty(CustomObjectWrapperList)) ;
            }
        }catch(Exception e){
            System.debug('Exception : '+e.getMessage()+' at Line : '+e.getLineNumber());
        }
    }
    
    global class CustomObjectWrapper{
        CustomObject__c CustomObjectDetail{set;get;}
        String CustomObjectimagebase64{set;get;}
    } 
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
 

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Deepika,

Greetings to you!

Please refer to the below link which might help you further with the above requirement. It will give you some idea.

https://salesforce.stackexchange.com/questions/109129/upload-a-file-from-vf-into-salesforce-rest-api

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Deepali KulshresthaDeepali Kulshrestha
Hi Deepika
 
Greetings to you!

you must create a rest class as given below:
@RestResource(urlMapping='/Webinar')
global class ObjectDataWithImage {
    @HttpGet
    global static void doGet() 
    {  
        try{
            List<CustomObjectWrapper> CustomObjectWrapperList = new  List<CustomObjectWrapper>();
            List<CustomObject__c> CustomObjectList = new List<CustomObject__c>();
            List<Attachment> AttachmentList = new List<Attachment>();
            
            Set<Id> CustomObjectIdsSet = new Set<Id>();
            
            CustomObjectList = [SELECT Id FROM CustomObject__c 
                                LIMIT 50000];
            
            if(CustomObjectList.size() > 0 ){
                for(CustomObject__c customObj : CustomObjectList){
                    CustomObjectIdsSet.add(customObj.Id);
                }
                AttachmentList = [SELECT Id, 
                                  Body, 
                                  Name,
                                  ParentId,
                                  ContentType
                                  FROM Attachment 
                                  WHERE ParentId IN: CustomObjectIdsSet 
                                  AND ContentType in ('image/png', 'image/jpeg', 'image/gif') 
                                  LIMIT 50000];
                
                for(integer i =0 ; i < CustomObjectList.size() ; i++){
                    CustomObjectWrapper CustomObjectWrapperObj = new CustomObjectWrapper();
                    CustomObjectWrapperObj.CustomObjectDetail = CustomObjectList[i];
                    if(AttachmentList.size() > 0){
                        for(integer j =0 ; j < AttachmentList.size() ; j++){
                            if(AttachmentList[j] != null && CustomObjectList[i].Id == AttachmentList[j].ParentId){
                                CustomObjectWrapperObj.Webinarimagebase64 = 'data:image/png;base64,';
                                CustomObjectWrapperObj.Webinarimagebase64 += EncodingUtil.base64Encode(AttachmentList[j].Body);
                                break;
                            }
                        } 
                    }
                    CustomObjectWrapperList.add(CustomObjectWrapperObj);   
                }
            }
            System.debug('CustomObjectWrapperList-->'+CustomObjectWrapperList);
             if(CustomObjectWrapperList.size() > 0 ){
                RestContext.response.statusCode = 200;
                RestContext.response.addHeader('Content-Type', 'text/plain');
                RestContext.response.responseBody = Blob.valueOf(JSON.serializePretty(CustomObjectWrapperList)) ;
            }
        }catch(Exception e){
            System.debug('Exception : '+e.getMessage()+' at Line : '+e.getLineNumber());
        }
    }
    
    global class CustomObjectWrapper{
        CustomObject__c CustomObjectDetail{set;get;}
        String CustomObjectimagebase64{set;get;}
    } 
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
 
This was selected as the best answer
DeepikareddyDeepikareddy
Hi , iam going to send the data and image  in the json format for the External apis .. how to send in json request in post method ., and in get method i should have to preview again.

thank you