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
LaKeisha JordanLaKeisha Jordan 

Please help with the following Trigger Error below. Thanks

Error Received:

Apex script unhandled trigger exception by user/organization: 00570000004LeTu/00D300000006E35
 
Region_cLeadTrigger: execution of BeforeInsert
 
caused by: System.QueryException: List has no rows for assignment to SObject
 
Trigger.Region_cLeadTrigger: line 3, column 1




Below is the full Trigger:

trigger Region_cLeadTrigger on Lead (before insert) {

    staticResource sr = [
        select Body
        from StaticResource
        where Name = 'regionjson'
    ];
    String country = '';
    JSONParser parser = JSON.createParser(sr.Body.toString());
   
    try {
        if(Trigger.isBefore) {
            for (Lead l : trigger.New) { 
                System.debug('Inbound country is: ' + l.Country__c + ' Inbound Region is: ' + l.Region__c);
                if (l.Region__c == null) {
                    String region = findRegion(l.Country__c);
                    System.debug('Value of new region is: ' + region);
                    if (region != null) {
                        l.Region__c = region;
                    }
                }
            }
        }
    } catch (Exception ex) {
        System.debug('Unable to save region due to incorrect mapping error is: ' + ex);
    }
    private String findRegion(String uiCountry) {
        boolean rootFlag = false;
        String country;
        while (parser.nextToken() != null) {
            // root found
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Workbook')) {
                System.debug('Found root!');
                rootFlag = true;
                parser.nextToken();
            }         
            if (rootFlag)  {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    country = parser.getText();
                    while (parser.nextToken() != null) {
                        if (parser.getCurrentToken() == JSONToken.VALUE_STRING) {
                            if (uiCountry.equalsIgnoreCase(country)) {
                                return parser.getText();   
                            }
                            break;
                        }                      
                    }
                }
            }
        }
        return null;
    }
}
 
Leonardi KohLeonardi Koh
You might want to check if static resource mentioned in line 3 actually exist, the error message basically indicates that it didn't have anything available when it was trying to assign value for the staticResource sr.