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 

Where to locate SOQL? The Trigger created is receiving errors and I do not know how to repair. Please help. Thanks.

Here is the Trigger:

trigger Region_cLeadTrigger on Lead (before insert) {

    staticResource sr = [
        select Body
        from StaticResource
        where Name = 'CountryRegionJSONII'
    ];
    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;
    }
}


Error Message:

Reason: Apex trigger Region_cLeadTrigger caused an unexpected exception, contact your administrator: Region_cLeadTrigger: execution of BeforeInsert
 
caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.Region_cLeadTrigger: line 3, column 1



 
Prashant Pandey07Prashant Pandey07
You can set the debug log and check the error..
 staticResource sr = [
        select Body
        from StaticResource
        where Name = 'CountryRegionJSONII'
    ];

The above query may not be returning anything make sure you have a static resource file with CountryRegionJSONII.
LaKeisha JordanLaKeisha Jordan
Static Resource has the file with the correct name. The Trigger is also complicating entering a Lead manually in Salesforce as well. I need help.