Tuesday, May 21, 2019

Developer Console query not working

Symptom


Error when running a query from the Developer Console: Sobject type 'ObjectName' is not supported

Cause


Tooling API checkbox is checked!

Resolution


Uncheck the check box.

Wednesday, March 27, 2019

Salesforce Full Copy Sandbox Redirects to Production After Refresh

Symptom


One of the following may occur in a full copy sandbox shortly after it's been refreshed from production:

  • The url changes to production
  • "Insufficient privileges" message appears
  • "Could not reach [production url]" message appears

Possible Cause

The Salesforce Console remembers the user's tabs so they are retained between sessions.

When a full copy sandbox is refreshed, the user tabs are copied from production.

When these tabs display, the page attempts to redirect to production to display the records associated with the saved tabs.

Resolution

Close all the console user tabs.

Thursday, May 31, 2018

Compile errors due to custom classes with the same names as standard objects

Symptom


Trigger or class won't compile with error "Variable does not exist: Name", where "Name" is a field on a standard object such as Account. 

e.g.

trigger myAccountTrigger on Account (before insert) {
    for(Account a:Trigger.New){
        system.debug(a.Name);
    }
}

Cause


There is a custom class with the same name as the standard object, the trigger is referring to this class and the specified attribute doesn't exist on the custom class.

e.g.

public class Account {
    //test class to object conflict
}



Resolution

The thing is, we don't want our code to refer to the class - we want it to refer to the standard object.

Obviously if we knew this problem was going to come up we would never have named the class "Account" in the first place. But after the fact it could be a lot of work to change the class name (and all references to it) to something like "AccountClass".

The easy way to achieve this is by fully qualifying the references to the standard object. This is done by prefixing the object name with "Schema." No need to change the class!

e.g.

trigger myAccountTrigger on Schema.Account (before insert) {
    for(Schema.Account a:Trigger.New){
        system.debug(a.Name);
    }
}

Monday, April 23, 2018

Notepad++ Search Results pane goes awol

Symptom

Search Results pane disappears and won't come back no matter how many times you press F7


Resolution

remove %appdata%\Notepad++ folder

Cause

Notepad++ had a spaz.

Reference


https://stackoverflow.com/questions/46632070/notepad-search-window-disappeared

Monday, May 22, 2017

Finding good standard external id fields

In a previous post I mentioned that you can spot the really good external id standard fields in workbench because they have idlookup=true and unique=true.

In workbench, select your standard object and use the Expand All button, then search for unique: true

Tuesday, March 7, 2017

Single statement insert of parent and child custom objects

An example for inserting parent-child records in a single statement is provided by Salesforce here.


The crux of it is to create a parent reference record and refer to it in the child record lookup instead of trying to refer to the parent record variable to be inserted, i.e.

Account accountReference = new Account(MyExtID__c='SAP111111');newOpportunity.Account = accountReference;

Account parentAccount = new Account(Name='Hallie', MyExtID__c='SAP111111');

Database.insert(new SObject[] { parentAccount, newOpportunity });


A couple of things I took some time to figure out were:

1. How to get this working with custom objects? e.g. if I have Parent__c and Child__c objects that are linked via custom field Child__c.Parent_Lookup__c

The equivalent of newOpportunity.Account in the example above would be Child__c.Parent_Lookup__r

I had an hour of brain fade wondering why Child__c.Parent_Lookup__c wouldn't work, but that would be the equivalent of newOpportunity.AccountId, not newOpportunity.Account.

2. I needed to do this bulkified.

I expect there is a syntax you could use to create an inline sobject list out of two non-sobject lists and insert in one line of code but I didn't invest the effort to get there and went with the following instead (heavily abridged pseudocode):

List<SObject> sobjList = new List();
for(loop criteria){
  Parent__c parentRec = new Parent__c(field values);
  sobjList.add(parentRec);
  Child__c childRec = new Parent__c(field values);
  sobjList.add(childRec);
}
Database.insert(sobjList);

Tuesday, September 20, 2016

Salesforce upserts, SOAP & standard object lookups

Requirement

Need to perform a Salesforce upsert via SOAP API using External IDs on standard objects.

e.g. you want to specify the record Owner or RecordType without using Salesforce IDs.

Getting set up


Don't bother trying to prototype using Data Loader. All the standard object external IDs are unavailable. AFAICT Workbench doesn't support them either.
  1. Generate the WSDL and strip out any comments and script
  2. Install SoapUI if you haven't already
  3. Create a new SOAP project using your WSDL
  4. Find the login method and set it up as below. Get rid of any question marks
  5. Press play. The response appears on the right hand side.
This example is for the Enterprise WSDL.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com">
   <soapenv:Header>
      <urn:LoginScopeHeader>
         <urn:organizationId></urn:organizationId>
         <!--Optional:-->
         <urn:portalId></urn:portalId>
      </urn:LoginScopeHeader>
   </soapenv:Header>
   <soapenv:Body>
      <urn:login>
         <urn:username>me@myorg.org.sandbox</urn:username>
         <urn:password>reallybadpassword</urn:password>
      </urn:login>
   </soapenv:Body>
</soapenv:Envelope>

Upsert time

Find the Upsert method, set it up as below and press play. Copy the session ID from the login response to it.

  • The server URL returned in the login response is the target for this request. Put it up in the url box at the top of the request window
  • Account doesn't have any standard external ID fields so a field called ExternalID__c has been created. You could use the ID field instead, but yeuch
  • The Account has a record type with Name "Record Type 1"
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:urn="urn:enterprise.soap.sforce.com"
    xmlns:urn1="urn:sobject.enterprise.soap.sforce.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header>
      <urn:SessionHeader>
        <sessionId>00Dm00000001xVa!ARQAQCyXrqpRsPctsGUj4AAHIKJJlBeOjxM9M.GOGMCjsYrNNAHJ6OTRIYxZLwbjXb.5bEZh66q09rhYecRDl4YNUc0_7ruO</sessionId>
      </urn:SessionHeader>
   </soapenv:Header>
   <soapenv:Body>
      <urn:upsert>
         <urn:externalIDFieldName>ExternalId__c</urn:externalIDFieldName>
         <urn:sObjects xsi:type="urn1:Account">
            <urn1:ExternalId__c>abcd1234</urn1:ExternalId__c>
            <urn1:Owner xsi:type="urn1:User">
               <urn1:Email>me@myorg.org</urn1:Email>
            </urn1:Owner>
            <urn1:RecordType xsi:type="urn1:RecordType">
               <urn1:Email>Record Type 1</urn1:Email>
            </urn1:RecordType>
         </urn:sObjects>
      </urn:upsert>
   </soapenv:Body>
</soapenv:Envelope>

Other things to note

To find out which standard object fields are available as external IDs, use the Workbench and look for idlookup:true" on a field.

If you're a freetard using the dead-ended version of SoapUI (5.2.1) then you'll need to enable TLS 1.2 as per instructions at the following link...(or so I've heard)

https://community.smartbear.com/t5/SoapUI-Open-Source/How-to-enable-TLS-1-2-in-SoapUI/td-p/96239/page/2

The correct instruction in this topic for Windows:

Place this in your SoapUI-X.Y.Z.vmoptionsf file (where X.Y.Z is the version number of SOAPUI you have)

-Dsoapui.https.protocols=TLSv1.2,SSLv3


The User object doesn't have any fields where both idlookup and unique are true. As a result, to do an upsert involving Users, the session user requires the incredibly broad View All Data rights, otherwise they will get this error: "INSUFFICIENT_ACCESS: Upsert requires view all data on a non-unique custom index". This despite UserNames being unique across the entire platform!

The RecordType Name field can be used as an external ID, but the DeveloperName field can't, which is odd given that the DeveloperName is recommended as the invariant reference for apex code.