0

I have just used changesets to move an Apex Class & Trigger into a production testing sandbox. When I create a new contact in my development sandbox, I can click a checkbox which then calls the Apex class and creates a new user on my targeted endpoint. This is working in development but now, after deploying a changeset, I'm getting a First error: Script-thrown exception in the testing production sandbox > Apex Job Que.

Is this related to not having accurate test classes? I'm totally new to salesforce development, I understand you need to have a test class for production but this is still in a production sandbox.

Is this is related to test classes? if anyone can help that would be a lifesaver! Thanks

Error - First error: Script-thrown exception

Trigger

trigger LIFT_ContactTrigger on Contact (after insert, after update) {
    if(!System.isFuture() && !System.isBatch()){
        list<contact> lstContact = new list<contact> ();
        for(Contact c : trigger.new){
            if(c.Send_to_Onboarding_Portal__c){
                lstContact.add(c);
            }
        }
        if(!lstContact.isEmpty()){
            DataBase.executeBatch(new LIFT_ContactBatchCreation(lstContact),1);
        }
    }
}

CLASS

/* TODO - Add description of class, author & date */
/* TODO - Add TEST class */
global class LIFT_ContactBatchCreation implements Database.Batchable<sObject>, Database.AllowsCallouts {
    private List<Contact> lstContact;
    public LIFT_ContactBatchCreation(List<Contact> lsacc) {
        System.debug('constructor batch class, ' + lsacc.size());
        this.lstContact = lsacc;
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        System.debug('start batch class, ' + lstContact.size());
        String soqlQuery = 'SELECT Account.Branch_Id__c, Account.Parent_Account_Name__c, Account.Date_became_Customer__c, Account.Go_Live_Date__c, Account.Name, FirstName, LastName, Email, Id From Contact where Id IN :lstContact';
        return Database.getQueryLocator(soqlQuery);
    }
    global void execute(Database.BatchableContext bc, List<Contact> scope){
        System.debug('execute batch class, ' + scope.size());
        for (Contact contact : scope) {
            try {
                System.debug(contact.Account.Name);
                System.debug(contact.Account.Branch_Id__c);
                String accountName = contact.Account.Name;
                String branchId = contact.Account.Branch_Id__c;
                String firstName = contact.FirstName;
                String lastName = contact.LastName;
                String email = contact.Email;
                Date goLiveDate = contact.Account.Go_Live_Date__c;
                Date startDate = contact.Account.Date_became_Customer__c;
                String parentAccountName = contact.Account.Parent_Account_Name__c;
                //TODO - move to separate class for the callout
                Http http = new Http();
                HttpRequest request = new HttpRequest();
                //TODO - make named credential more generic so can easily be deployed between environments
                request.setEndpoint('callout:Glofox_Portal/api/branches/fromSalesforceDemo');
                request.setMethod('POST');
                request.setHeader('Content-Type', 'application/json;charset=UTF-8');
                // Set the body as a JSON object
                request.setBody('{"branchId":"'+branchId+'", "parentAccountName":"'+parentAccountName+'", "name":"'+accountName+'", "goLiveDate":"'+goLiveDate+'","startDate":"'+startDate+'" ,"admins":[{"firstName":"'+FirstName+'","lastName":"'+LastName+'","email":"'+email+'"}]}');
                HttpResponse response = http.send(request);
                System.debug(response.getBody());
                // Parse the JSON response
                if (response.getStatusCode() != 200) {
                    //TODO - insert error handling and/or retyr logic here
                    //TODO - log exception in Salesforce including error code and reason
                    System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
                } else {
                    //TODO - update status field in Salesforce on success and update externalId
                    System.debug(response.getBody());
                }
            }
            catch (Exception e) {
                System.debug('Error-' + e.getMessage());
                //Log exception
            }
        }
        update scope;
    }
    global void finish(Database.BatchableContext bc){
    }
}

TEST Class

@isTest
private class Test_LIFT_ContactTrigger {
    static testMethod void validateLIFT_ContactTrigger() {
        
        Contact c = new Contact();
        c.FirstName = 'can';
        c.LastName = 'jones';
        c.email = 'c.jones@user.com';
        
        Account a = new Account();
        a.Name = 'test account';
        a.Branch_ID__c = '2234234234234234324';
        a.Date_became_Customer__c = system.today();
        a.Go_Live_Date__c = system.today();
        
        Account p = new Account();
        p.Name = 'test partent';
        insert p;
        a.ParentId = p.Id;
        
        insert a;
        c.AccountId = a.Id;
        
        
        
        
        Test.startTest();
        c.Send_to_Onboarding_Portal__c = true;
        insert c;
        
        
        Test.stopTest();
        
        c = [SELECT FirstName FROM Contact WHERE Id =:c.Id];
        System.debug('Name: ' + c.FirstName);
        System.assertEquals('can', c.FirstName);
    }
    
}


3
  • Hi, and welcome to SFSE! What's the exception or message you get? There's not much to go off of without more information. Please read about How to Ask, then edit your question to include any relevant details that may help us help you. – sfdcfox Jul 7 '20 at 14:09
  • Hi @sfdcfox, I have updated the question the error is 'First error: Script-thrown exception' which is visible in the Apex Jobs > status detail. any ideas? – con Jul 7 '20 at 14:13
  • Could be related to the tests. From developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… : "By default, test methods don’t support HTTP callouts, so tests that perform callouts fail. Enable HTTP callout testing by instructing Apex to generate mock responses in tests." – jwatts1980 Jul 7 '20 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.