1

I ma trying to upload a CSV containing loads of records for my custom object type. Each of these items has a delivery driver.

To make it easier for the end user to upload, the delivery drive will be specified as "Firstname Lastname" in the CSV rather than as there contact ID.

What I wa proposing was to split the string, and then add to the object as:

MyCustomObject.driver__c.FirstName = A
MyCustomObject.driver__c.LastName = B

Is there any easier way to do this? Also if the driver does not exist I want to be able to create it (by populating firstname and lastname).

1
  • What I a m trying to do is return and ID for a name, without using a query within a loop - incase I have a lot of records! Commented Dec 1, 2014 at 14:38

2 Answers 2

1

Easy:

  1. Create a field that is an externalID field
  2. Create a workflow rule - On insert and on any change - to populate the field with the concatenation of the first name and lastname fields
  3. When parsing the CSV populate the externalID field using the name field in the CSV

Then you can use upsert {RECORDS} {EXTERNALIDFIELD}

In addition, if the Name of the Custom Record is a text field, then if it is populated with the first and last name I believe you can use the Name field for the upsert externalID field.

0

Edit: The other answer provided is much cleaner, better and uses only standard functionality. If for some reason you have run out of external Ids my much more complicated approach my work for you, but I would instead raise a question with SalesForce to see if you can get the limit raised for your object.

Regarding your comment in the context of the question I believe what you need is to bulk query all possible matching "MyCustomObjects" and place them into a data construct, using a FirstName LastName key. Here is one possible way of achieving this goal:

public static Map<String, MyCustomObject__c> createNameToMyCustomObject(List<MyCustomObject__c> myCustomObjects){
    Set<String> firstNames = new Set<String>();
    Set<String> lastNames = new Set<String>();
    for(MyCustomObject__c myCustomObject:myCustomObjects){
        firstNames.add(myCustomObject.FirstName__c);
        lastNames.add(myCustomObject.LastName__c);
    }

    List<MyCustomObject__c> foundMyCustomObjects = [
        SELECT FirstName__c, LastName__c
        FROM MyCustomObject__c
        WHERE FirstName__c IN :firstNames AND
         AND LastName__c IN :lastNames
    ];

    Map<String, MyCustomObject__c> nameToMyCustomObject = new Map<String, MyCustomObject__c>();
    for(MyCustomObject__c foundMyCustomObject:foundMyCustomObjects){
        nameToMyCustomObject.put(createMyCustomObjectNameKey(foundMyCustomObject), foundMyCustomObject);
    }

    return nameToMyCustomObject;
}


public static String createMyCustomObjectNameKey(MyCustomObject__c myCustomObject){
    return myCustomObject.FirstName__c + '-' + myCustomObject.LastName__c;
}

You can then use the created map in conjunction with createMyCustomObjectNameKey method to check to see if the object exists in the database.

if(nameToMyCustomObject.containsKey(createMyCustomObjectNameKey(myCustomObject))){
    //object with first and last name combination exist in salesforce
} else{
    //object with first and last name combination does not exist in salesforce
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .