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
Steve CairneySteve Cairney 

Update records via webservice

Hi, I am working with another developer so we can push sObjects to their service. I have successfully written a class that sends the data over.

We are now at stage 2, which is working on how to allow thier service to update fields on our existing sObjects. Currently the fields on the sObject are NULL, but when thier counterparts in the eternal system are populated, the external system is to contact our SF Org and update the fields if needed.

Now right now we are trying to work out how exactly to do this. It's my job to ensure that any classes we need are created in Salesforce.

What do we need to do in order to achieve this? Do I need to write a class (and testclass) that envokes the update of fields?

I have listed the class I created below, these are invoked when a button is pressed on one of the sObjects. Do I need to do something similar again?
 
global with sharing class ItsApprovedWebServices {
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Description : Web Service to login to ItsApproved
    // Called From : "Test Submit Booking" botton on ItsApproved Booking Page
    // Returns     : Authorization Token (for use in subsequent web service calls to Its Approved)
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    webservice static String Login(String userName) {
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        
        req.setEndpoint('http://site.net/api/values/login?username=username');
        
        
        req.setMethod('GET');
        req.setTimeout(60000);
        
        res = http.send(req);
        
        
        return res.getHeader('Authorization-Token');
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Description : Web Service to create representation of Booking in JSON format
    // Called From : "Test Submit Booking" botton on ItsApproved Booking Page
    // Returns     : Complete Booking as a JSON object
    //             : 08.03.16 KJF Was returning "ID" field in CustomerBookingId (now returns "Name")
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    webservice static String GetBookingDetails(Id idIn) {
        JSONGenerator jsonGenerator = JSON.createGenerator(true);
        
        ItsApproved_Booking__c booking = [SELECT Id, Name, Booking_Title__c, Customer_Reference__c, Incharge_Date__c FROM ItsApproved_Booking__c WHERE Id = :idIn];
        
        jsonGenerator.writeStartObject(); // Booking      
        
        jsonGenerator.writeStringField('CompanyId', '042D7C6D-00E4-4C81-901A-BC8F8B545441'); // Live Company Id
        jsonGenerator.writeNumberField('UserId', 96); // Live User Id
        jsonGenerator.writeStringField('BookingTitle', booking.Booking_Title__c);
        jsonGenerator.writeStringField('CustomerReference', booking.Customer_Reference__c);
        jsonGenerator.writeStringField('CustomerBookingId', booking.Name);
        jsonGenerator.writeDateTimeField('InchargeDate', booking.Incharge_Date__c);
        
        List<ItsApproved_Product__c> products = [SELECT Id, IAProduct__c, Booking_Reference__c FROM ItsApproved_Product__c WHERE Booking_Reference__c = :idIn];
        
        jsonGenerator.writeFieldName('Products');
        jsonGenerator.writeStartArray(); // Products
        for(ItsApproved_Product__c p: products)
        {
            List<IA_Product__c> IAProducts = [SELECT Id, Code__c FROM IA_Product__c WHERE Id = :p.IAProduct__c];
            
            jsonGenerator.writeStartObject(); // Products
            jsonGenerator.writeStringField('ProductionCode', IAProducts[0].Code__c);           
            jsonGenerator.writeStringField('CustomerProductReference', IAProducts[0].Code__c);           
            
            List<ItsApproved_Design__c> designs = [SELECT Id, Artwork_Title__c FROM ItsApproved_Design__c WHERE Product_Reference__c = :p.Id];
            
            jsonGenerator.writeFieldName('Designs');
            jsonGenerator.writeStartArray(); // Designs
            for(ItsApproved_Design__c d: designs)
            {
                jsonGenerator.writeStartObject();
                jsonGenerator.writeStringField('CustomerDesignId', d.Id);           
                jsonGenerator.writeStringField('DesignName', d.Artwork_Title__c);  
                
                List<ItsApproved_Delivery__c> deliveries = [SELECT Id, IADepot__c, Quantity__c FROM ItsApproved_Delivery__c WHERE ItsApproved_Design__c = :d.Id];
                
                jsonGenerator.writeFieldName('Deliveries');
                jsonGenerator.writeStartArray(); // Deliveries
                for(ItsApproved_Delivery__c del: deliveries)
                {
                    List<IADepot__c> IADepots = [SELECT Id, Depot_Code__c FROM IADepot__c WHERE Id = :del.IADepot__c];
                    
                    jsonGenerator.writeStartObject(); // Deliveries
                    jsonGenerator.writeStringField('DeliveryId', del.Id);           
                    jsonGenerator.writeStringField('DepotCode', IADepots[0].Depot_Code__c);
                    jsonGenerator.writeNumberField('Quantity', del.Quantity__c);
                    jsonGenerator.writeEndObject(); // Deliveries
                }
                jsonGenerator.writeEndArray();  // Deliveries
                
                jsonGenerator.writeEndObject(); // Designs
            }
            jsonGenerator.writeEndArray(); // Designs
            
            jsonGenerator.writeEndObject(); // Products
        }
        jsonGenerator.writeEndArray(); // Products
        
        jsonGenerator.writeEndObject(); // Booking
        
        return jsonGenerator.getAsString();        
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Description : Web Service to send complete Booking Details to Its Approved
    // Called From : "Test Submit Booking" botton on ItsApproved Booking Page
    // Returns     : 
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    webservice static String PostBooking(String json, String authorizationToken)
    {   
        // Now send the data to Its Approved
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        
        
        req.setEndpoint('http://site.net/api/values/PostBooking');
        
        
        req.setMethod('POST');
        req.setHeader('Content-Type','application/json');
        req.setHeader('Authorization-Token', authorizationToken);
        req.setBody(json);
        req.setTimeout(60000);
        
        
        res = http.send(req);
        
        
        return res.getBody();
        
    }
}