DEV Community

Rachel Soderberg
Rachel Soderberg

Posted on • Updated on

Retrieving and Updating Salesforce Object Fields With a Web Service

Welcome to Part 4 of my Salesforce Outbound Messages series! If you do not already have a connected WCF Web Service and Salesforce Outbound Message ready to go, you can click the circles above to follow through the rest of my series before starting here! It is not essential that your service and outbound message are identical to mine as long as they are communicating with one another across your network (or through a SOAP request).

At this point, you've already built your outbound message and web service which means you've already generated and created your service with the object WSDL. Unfortunately it's been determined that you need several more fields than your outbound message is currently passing on to your service. There are two options: 1. Add the fields to your outbound message and update your service's WSDL, or 2. Get your Salesforce object programmatically and update that query statement. I'm going to teach you how to do the second one because it's easier, faster, and much more flexible for the long-term.

Querying A Salesforce Object

First we need to query Salesforce to get fields from our object. We're going to use the provided SforceService class to create a binding, then we'll pass it a SOQL query using some information we already have from the outbound message (in my case, the RMA Id is the only field I sent).

RMA Outbound Message

As you can see below, I use the notification to get the RMA__c custom object(s) from Salesforce, storing the RMA Id so I can access other fields on the RMA that triggered the outbound message. The query looks long, but that's because my service is using a lot of RMA fields - this query should hold whatever fields you'll be accessing or updating in your own service.

private SforceService binding;

RMA__cNotification[] incomingRMAs = notifications1.Notification;
foreach (var rma in incomingRMAs)
        var id = rma.sObject.Id;

RMA__c myRMAObject = (RMA__c)QueryGenericObject($"SELECT Id, Name, RMA_Number__c, Case_Number__c, " +
                        $"Status__c, Description__c, Owner.Name, " +
                        $"Shipping_Address__r.Name, Shipping_Address__r.BillingCity, " +
                        $"Shipping_Address__r.BillingState, " +
                        $"Shipping_Address__r.BillingPostalCode, Company_Name__r.Name, " +
                        $"Shipping_Method__c, Customer_Name__r.FirstName, " +
                        $"Customer_Name__r.LastName, Customer_Name__r.Phone, " +
                        $"Reason_Code__c FROM RMA__c WHERE Id = '{id}'");
Enter fullscreen mode Exit fullscreen mode

Once I have created the query, I use my binding to query Salesforce and return the result of type QueryResult. This result is converted to an sObject (a Salesforce generic object) by the QueryGenericObject method and returned to finally be converted to my RMA__c custom object at the point where I first sent Salesforce my query in the example code above.

private sObject QueryGenericObject(string query)
    sObject mysObject = RunSalesforceQuery(query).records.FirstOrDefault();

    return mysObject;

internal QueryResult RunSalesforceQuery(string query)
    QueryResult result = new QueryResult();
    result = binding.query(query);

    return result;
Enter fullscreen mode Exit fullscreen mode

Your object variable should now be populated with the fields you specified in the query. Now that that's done, making changes to your object fields in Salesforce is incredibly easy! I have access to the RMA Status, Description, Reason Code, Owner Name, Shipping info, and Customer info and can easily add more fields if I need them later on. Note: You will get an error if you attempt to access or modify a field that is not in the query.

Updating Salesforce Object Fields

Updating object fields is incredibly easy once you've got a converted object from your SOQL query. Simply initialize a new object of your object's type and add the fields you're updating. You must include the object Id, set to itself, for system to know which object is meant to receive the updates. Do not set it to something new.

RMA__c updateRMA = new RMA__c
    Id = myRMAObject.Id,
    Sales_Order__c = salesOrderId,
    Status__c = "Created",
    Description__c = "This RMA Order has been processed successfully!"
    Submitted_By__c = myUserObject.Id
Enter fullscreen mode Exit fullscreen mode

The fields I updated above include two text fields: Sales Order and Description, one picklist field: Status, and a lookup field with a User relationship: Submitted By. Updating text fields are easy, simply pass the string you'd like it to reflect. Picklists are similar but you must make sure the string you pass matches the picklist selection exactly. Updating a lookup field is slightly more complex as you will need to pass in the Id of the object you want it to reflect - in this case the relationship is with the User object so I needed to pass the Id of the User I want to update the field with.

After the updates you will need to follow this initialization with a binding.update call in order to save the updates:

SaveResult[] saveResult = binding.update(new sObject[] { updateRMA });
Enter fullscreen mode Exit fullscreen mode

If you refresh your object in Salesforce, you should see your changes happen immediately after the binding.update() call.

Top comments (1)

kritikgarg profile image
kritikgarg • Edited

πŸ’‘πŸ‘ Excellent article, Rachel! Your clear and concise explanations make it easy to understand the process of retrieving and updating Salesforce object fields with a web service.It is definitely a helpful resource for those looking to integrate Salesforce with other applications. Keep up the great work! πŸ’»πŸ™Œ

πŸ‘‰Additionally, I came across a fascinating article on Salesforce Objects which I found to be an excellent resource for anyone looking to learn about Salesforce objects. It covers everything you need to know, and I highly recommend it! πŸš€πŸ’»