+ Start a Discussion
Olga Kim 5Olga Kim 5 

Post Data to A Web Service Using REST

Hi
I have a requirement: My Salesforce org should send data to a web service whenever the status (field) of the tenant (object) changes from active to non-active.
The message that I post should contain the ID of the Tenant that not active anymore. I can't figure out how to add the ID of this tenant.

Please help me resolve this puzzle.


This is my code:
global class FOBCallouts1{
@Future (Callout=true)
public static void tenantUpdate(list<tenant__c> TenList, map <id,tenant__c> oldMap)
{ // list<tenant__c> TenList=new list<tenant__c>();
// map <id,tenant__c> oldMap=new map<id, tenant__c>(); for(tenant__c objten:TenList)
{ if(objten.status__c=='Non-active' && oldMap.get(objten.Id).status__c =='Active'){ Http http = new Http();
HttpRequest request = new HttpRequest(); request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('POST');
Tenant__c tenant=[SELECT Id FROM Tenant__c]; 
String JsonString=JSON.serialize(objten.id);
String body = 'Please deactivate FOB for following tenants(ID)'; request.setBody(body+JsonString); System.debug(request.getBody()); } } } }
 
PRAKASH JADA 13PRAKASH JADA 13
Hi,


This is one way to get the user Id.

/*
* Author: Prakash Jada
* Created Date: March 29, 2020
*/

global class FOBCallouts1{
    // Global variable declaration
    Private static final String TAG = 'TAG-FOBCallouts1 :: ';
    private static String status = 'Non-active';
    
    //== Method to validate the Tenant status
    public static void tenantUpdate(list<tenant__c> TenList, map <id,tenant__c> oldMap) { 
        
        Set<String> nameSet = new Set<String>();
        Set<Id> userIdSet     = new Set<Id>();
        
        // Loop to iterate over the list of tenants
        for(tenant__c objten : tenList) {
            // Condition to verify the Status of the tenant
            if(oldMap.get(objten.Id).Status__c != objten.Status__c && status.equalsIgnoreCase(objten.Status__c)) {
                nameSet.add(objten.Name__c);
            }
        }// END FOR
        System.debug(TAG + 'Name Set size : ' +nameSet.size());
        
        // Condition to check for the name set
        if(nameSet != null && nameSet.size() > 0) {
            // Query to fetch the User details
            List<User> users = [SELECT ID, Name FROM User WHERE Name = : nameSet];
            System.debug(TAG + 'User List size : ' +users.size());
            
            // Condition to check for the user details
            if(users != null && users.size() > 0) {
                
                // Loop to iterate over the List of users
                for(User user : users) {
                    userIdSet.add(user.Id);
                }// END FOR
                System.debug(TAG + 'User Id Set Size : ' +userIdSet.size());
            }// END IF
            
            // Condition to check for the User Id set
            if(userIdSet != null && userIdSet.size() > 0) {
                FOBCallouts1.doCallout(userIdSet);
            }
        }
        
    }
    
    @future(callout = true)
    private static void doCallout(Set<Id> userIds) {
        
        // Loop to iterate over the User Id's
        for(Id userId : userIds) {
            // Callouts
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
            request.setMethod('POST');
            String JsonString=JSON.serialize(userId);
            String body = 'Please deactivate FOB for following tenants(ID)';
            request.setBody(body+JsonString);
            System.debug(TAG + 'Verifying the request body : ' +request.getBody());
            
        }// END FOR
    }
}


1. I created a name custom field and used it against the User object to get the User Id. I hope this helps.

Thanks,
Prakash. J