0

I have objects that I need to fill from a web service that I get from some source.

I wondering, If I want to check if a picklist value that transferred from this webservice is a valid value in the picklist.

I know a way that I can use : Contact.fields.Exams.getDescribe().getpicklistvalues();

and than with a for loop - I can push the value into a Map and check it. The problem - I have a lot of picklist fields inside the Contact and other objects as well..

Is there a better way to do so, instead go over each picklist and with for loop insert it values into the Map?

Example:

Map<String,String> newMap = new Map<String,String>();

for(Schema.PicklistEntry s : Contact.fields.Country__c.getDescribe().getpicklistvalues() ){
     newMap.put('Country_',s.getValue());
}


for(Schema.PicklistEntry s : Contact.fields.Gender__c.getDescribe().getpicklistvalues() ){
     newMap.put('Gender',s.getValue());
}

for(Schema.PicklistEntry s : Contact.fields.Checks.getDescribe().getpicklistvalues() ){
     newMap.put('Checks',s.getValue());
}

And there even more picklists.. And if I get a wrong value - I have a DML Exception :

INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST

Thanks!

3
  • 1
    Why not just let it fail? Do your DML with Database.insert() or Database.update() so you can inspect the errors, and return them to the remote caller?
    – David Reed
    Commented Dec 11, 2018 at 11:50
  • I don't want it to fail because it not depands on the other logic there and I want that the other logic will continue.. But you just gave me an Idea forgot about this option - Thanks.
    – Salvation
    Commented Dec 11, 2018 at 12:42
  • Sure. I'll post an answer with a little more detail.
    – David Reed
    Commented Dec 11, 2018 at 12:50

1 Answer 1

1

I suggest that a more efficient pattern, rather than trying to validate all of the picklist options exhaustively through code, is to allow Salesforce to perform that validation by initiating the DML operation, allowing it to fail, and then inspecting the errors.

You do this by using Database methods to do your DML, such as Database.insert() and Database.update(), and providing the second parameter as false:

Database.insert(inboundRecords, false);

This provides you with an array of Database.SaveResult objects that you can iterate over. From the linked documentation:

Database.SaveResult[] srList = Database.insert(acctList, false);

// Iterate through each returned result

for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account ID: ' + sr.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}

You could implement logic along these lines to return a collection of results to the caller of your service, indicating to them for each inbound record whether it was saved correctly and providing either the record's Id or the relevant error generated by Salesforce.

0

You must log in to answer this question.

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