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
SivaGSivaG 

custom webservice in saleforce


Hi,

I would like to create a custom web service in my Salesforce organization to share some data when external application sends a request. The external application sends the request with some identifier. I need to respond with the related status.

Identifier can have 1 status(one-to-one mapping) Identifier can have multiple children (one-to-many mapping) How can I send this back to external application?

In 2nd case, I need to send both child I'd and statuses (map).

Thanks
Kumar
Best Answer chosen by SivaG
Shashikant SharmaShashikant Sharma
You could create a Soap or Apex Rest Service

Soap Example - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_web_services_methods.htm
Rest Examplae - 
https://developer.salesforce.com/page/Creating_REST_APIs_using_Apex_REST

Thanks
Shashikant

All Answers

Shashikant SharmaShashikant Sharma
You could create a Soap or Apex Rest Service

Soap Example - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_web_services_methods.htm
Rest Examplae - 
https://developer.salesforce.com/page/Creating_REST_APIs_using_Apex_REST

Thanks
Shashikant
This was selected as the best answer
SivaGSivaG
Hi Shashikant,

I have used SOAP web service but my challenge here is that in case of one-to-many relationship 

ReqResWrapper 

I have created a wrapper class to handle the Request/Response but the challenge here is in case of one-to-many relationship I need to send multiple statuses with identifiers.

Example : 
If the "mid" contains a chain id then I need to check all the mid's under that chain and respond back with each individual status of mid

I need to send both MID & Status in this case. 

MID1 - Verified
MID2 - Not Verified
MID3 - Verified

Question  : How to handle this scenario in code?

In 1st case its one-to-one relationship. 

MID - Verified


 if(!respmap.isEmpty()) {
                   for(String mid : respmap.keySet()){
                          resp.status = mid + '-' + respmap.get(mid) + ',';   
                   }
               }

global class AccountVerification_WebService {
    
   //Define an object in apex that is exposed in apex web service
   global class ReqResWrapper {
      webservice String mid;    //hold MID of Merchant
      webservice String type;   //hold Verification type
      webservice String sourceApplication; //hold values 
      webservice String status;                           //hold values like New, Request Being Processed,Completed,Error etc,
      webservice String statuscode;                       //hold values like 1,2 or 3
      webservice String failureReason;                    //hold the failure reason of bounced mail
      webservice String resErrorCode;                     //hold corresponding error code while response being sent from salesforce
      webservice String resErrorMsg;                      //hold corresponding error message while response being sent from salesforce
      //webservice Map<String,String> mermap;
   }

   webservice static ReqResWrapper AccountVerification(ReqResWrapper req) {
     try{           
       System.debug('//////******notificationRequest******//////////'+req.toString());
       Map<String,String> fieldvaluemap = new Map<String,String>();
       Map<String,Id> AcctMap = new Map<String,Id>();
       Map<String,String> respmap = new Map<String,String>();
       fieldvaluemap=preparereqmap(req);
       ReqResWrapper resp = new ReqResWrapper();
       Boolean MIDsent=false;
       Boolean MIDmatched=false;
       //Searching based on MID and Email provided
       if(fieldvaluemap.get('mid')!=null && fieldvaluemap.get('mid').length()>0){
           MIDsent=true;
       }
       if(MIDsent)
       {           
           for(Account a :[SELECT ID,NAME,Merchant_MID__c,Chain_ID__c FROM Account where Merchant_MID__c !=  null and (Chain_ID__c = :fieldvaluemap.get('mid') OR Merchant_MID__c = :fieldvaluemap.get('mid'))]){
               AcctMap.put(a.Merchant_MID__c,a.Id);
           } 
           if(!AcctMap.isEmpty()){
               for(Post_Sale_Task__c pst : [Select Id,Status__c,Name FROM Post_Sale_Task__c where Merchant_MID__c IN :AcctMap.keySet() and Account__c IN :AcctMap.values() and Name = 'Welcome Call']){                   
                  If(pst.Status__c != null){
                      respmap.put(pst.Merchant_MID__c,pst.Status__c);
                  }
               }
               if(!respmap.isEmpty()) {
                   for(String mid : respmap.keySet()){
                          resp.status = mid + '-' + respmap.get(mid) + ',';   
                   }
               }
               else{
                    System.debug('MID Not Matched');
                    resp.resErrorCode='NR-ERROR-001';
                    resp.resErrorMsg=FDSnapErrorMessage__c.getInstance('NR-ERROR-001').ErrorMsg__c;               
                  }
           }  
       }
       else{
            System.debug('MID Not Sent');
            resp.resErrorCode='NR-ERROR-003';
            resp.resErrorMsg=FDSnapErrorMessage__c.getInstance('NR-ERROR-003').ErrorMsg__c;
       }       
         return resp;         
       }
       Catch(Exception e){
           System.debug('******************************************'+e);
           ReqResWrapper resp = new ReqResWrapper();
           resp.resErrorCode='NR-ERROR-005';
           resp.resErrorMsg=e.getMessage();
           System.debug('////////////-----notificationResponse----////' + resp);
           return resp;
       }
       
  }
  /** This method is used for converting the request received to a map of wrapper class variables and their values**/
    public static Map<String,String> preparereqmap(ReqResWrapper notificationreq){
        System.debug('////////////-----notificationreq----////'+notificationreq);
        Map<String,String> reqmap=new Map<String,String>();
        String strrequest = notificationreq.toString();
        
        Integer startint=strrequest.indexof('[');
        Integer endint=strrequest.indexof(']');
        
        strrequest = strrequest.substring(startint+1,endint);
        System.debug('////////////-----strrequest----////'+strrequest);
        for(String comasplit:strrequest.split(',')){
            Integer equalindex=comasplit.indexof('=');
            system.debug('++++'+comasplit);
            system.debug('**********************'+comasplit.substring(0,equalindex).replaceAll(' ',''));
            reqmap.put(comasplit.substring(0,equalindex).replaceAll(' ',''),comasplit.substring(equalindex+1,comasplit.length()).replace('null',''));
        }
        return reqmap;
    }  
  
}

Thanks
Kumar