-1

I am running the test class below and it keeps on giving me the error.Would appreciate your help on this: Here the error message

Error Message

System.DmlException: Update failed. First exception on row 0 with id 0031800000ImG3NAAV; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName] Stack Trace Class.Api_DealerProfile_0.changeProfile: line 180, column 1 Class.Api_Http_DealerProfile_0.setProfile: line 39, column 1 Class.Api_Test_DealerProfile.goodHttpPost: line 136, column 1 Class.Api_Test_DealerProfile.myUnitTest: line 18, column 1

Below is my test class.

/**
*  Test class for creating, modifying, and retrieving profile information.
*/

@isTest
private class Api_Test_DealerProfile {

    private static String user1Email;
    private static Id user1Id;
    private static String user2Email;
    private static Id user2Id;

    static testMethod void myUnitTest() {
        simpleInsert();
        nullGet();
        badHttpGet();
        goodHttpGet1();
        badHttpPost();
        goodHttpPost();
        goodHttpDelete();
        badHttpDelete();
        //insertAndChangeAddress();
        updateNoContactRecord();
        // readdressNoContactRecord();
    }

    private static void simpleInsert() {
        Integer r = Crypto.getRandomInteger();
        String rs = String.valueOf(r);
        String email = 'jb-'+rs+'@some.org';
        Id id;

        User owner = [select Id, Name from User where Name = 'OEM Sales' limit 1];

        id = Api_Http_RegisterDealer_0.postNewCustomer('JoeBloe','Chrysler','12345',email,'1212 some place',
                                                       'Bouler','CO','12121','US','3033539876','Joe','Bloe'
                                                      );
        System.assertNotEquals(null, id);
        user1Email = email;
        user1Id = id;

        Account acct = [select Id, OwnerId from Account where Id = :user1Id limit 1];
        System.assertEquals(owner.Id, acct.OwnerId);

        // Similar user, different email address
        r = Crypto.getRandomInteger();
        rs = String.valueOf(r);
        email = 'jb-'+rs+'@some.org';
        id = Api_Http_RegisterDealer_0.postNewCustomer('JoeBloe',null,null,email,'1212 some place'
                                                       ,'Bouler','CO','12121','US','3033539876','Joe','Bloe'
                                                      );
        System.assertNotEquals(null, id);
        System.assertNotEquals(user1Id, id);
        user2Email = email;
        user2Id = id;

        // this should fail, since it creates a record with a duplicate email address.
        try {
            id = Api_Http_RegisterDealer_0.postNewCustomer('JoeBloe','Chrysler','12345',email,'1212 some place'
                                                           ,'Bouler','CO','12121','US','3033539876','Joe','Bloe'
                                                          );
        } catch (DmlException ex) {
            System.assert(ex.getMessage().contains('DUPLICATE_VALUE'));
        }
    }

    private static void nullGet() {
        Api_DealerProfile_0 profile = new Api_DealerProfile_0(null);
        System.assertNotEquals(profile, null);
        System.assertNotEquals(profile.error, null);
        System.assert(profile.error.contains('No email address'));

    }

    private static void badHttpGet() {
        Api_DealerProfile_0 profile;
        String email;

        email = EncodingUtil.urlEncode('bad email', 'UTF-8');

        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/Api/DealerProfile_0/' + email;
        profile = Api_Http_DealerProfile_0.get();

        System.assertNotEquals(profile.error, null);
        System.assert(profile.error.contains('not found'));
        System.assertEquals(profile.success, false);
    }

    private static void goodHttpGet1() {
        String email = EncodingUtil.urlEncode(user1Email, 'utf-8');
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/Api/DealerProfile_0/' + email;

        Api_DealerProfile_0 profile = Api_Http_DealerProfile_0.get();
        System.assertEquals(profile.error, null);
        System.assertEquals(profile.success, true);
        System.assertEquals(profile.name,'JoeBloe');
        System.assertEquals(profile.contactFirstName, 'Joe');
        System.assertEquals(profile.contactLastname, 'Bloe');
        System.assertEquals(profile.crmId, user1Id);

        // do it again, this time searching by ID
        String u1Id = EncodingUtil.urlEncode(user1Id, 'utf-8');
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/Api/DealerProfile_0/' + u1Id;

        profile = Api_Http_DealerProfile_0.get();
        System.assertEquals(profile.error, null);
        System.assertEquals(profile.success, true);
        System.assertEquals(profile.name, 'JoeBloe');
        System.assertEquals(profile.contactFirstName, 'Joe');
        System.assertEquals(profile.contactLastname, 'Bloe');
        System.assertEquals(profile.crmId, user1Id);

    }


    private static void badHttpPost() {
        String email = EncodingUtil.urlEncode('nobody@some.org', 'utf-8');
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/Api/DealerProfile_0/' + email;

        Api_Result_0 result = Api_Http_DealerProfile_0.setProfile(
            'JaneDoh',
            '1121 Some Other Place','Denver', 'CO', '12345', 'US', 
            '7075551212','Jane','Doh'
        );

        System.assertNotEquals(result, null);
        System.assertNotEquals(result.error, null);
        System.assertEquals(result.success, false);
    }

    private static void goodHttpPost() {
        String email = EncodingUtil.urlEncode(user1Email, 'utf-8');
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/Api/DealerProfile_0/' + email;

        Api_Result_0 result = Api_Http_DealerProfile_0.setProfile(
            'JaneDoh', 
            '1121 Some Other Place','Denver', 'CO', '12345', 'US', 
            '7075551212','Jane',null
        );

        System.assertNotEquals(result, null);
        System.assertEquals(result.error, null);
        System.assertEquals(result.success, true);

        Account acct = [select Id, Name from Account where Id = :user1Id limit 1];
        System.assertEquals(acct.Name, 'JaneDoh');

        // do it again, this time with contactlastname
        result = Api_Http_DealerProfile_0.setProfile(
            'JaneDoh', 
            '1121 Some Other Place','Denver', 'CO', '12345', 'US', 
            '7075551212','Jane','Doh'
        );

        System.assertNotEquals(result, null);
        System.assertEquals(result.error, null);
        System.assertEquals(result.success, true);

        acct = [select Id, Name from Account where Id = :user1Id limit 1];
        System.assertEquals(acct.Name, 'JanDoh');

        // get the new profile, and check the values    
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/Api/DealerProfile_0/' + email;

        Api_DealerProfile_0 profile = Api_Http_DealerProfile_0.get();
        System.assertEquals(profile.error, null);
        System.assertEquals(profile.success, true);
        System.assertEquals(profile.name, 'JaneDoh');
        System.assertEquals(profile.address, '1121 Some Other Place');
        System.assertEquals(profile.city, 'Denver');
        System.assertEquals(profile.state, 'CO');
        System.assertEquals(profile.zip, '12345');
        System.assertEquals(profile.country, 'US');
        System.assertEquals(profile.phone, '7075551212');
        System.assertEquals(profile.contactFirstName, 'Jane');
        System.assertEquals(profile.contactLastName, 'Doh');

    }

    private static void goodHttpDelete() {
        // delete user1 by email
        String email = EncodingUtil.urlEncode(user1Email, 'utf-8');
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/Api/DealerProfile_0/' + email;    
        Api_Result_0 response = Api_Http_DealerProfile_0.remove();
        System.assert(response.success);
        System.assertEquals(null, response.error);

        try {
            Account acct = [select Id from Account where Dealer_Email__c = :user1Email];
            System.assert(false, 'Expected exception was not thrown.');
        } catch (QueryException qex) {

        } catch (Exception ex) {
            System.assert(false, ex.getMessage());
        }

        // delete user2 by Id
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/Api/DealerProfile_0/' + user2Id;
        response = Api_Http_DealerProfile_0.remove();
        System.assert(response.success);
        System.assertEquals(null, response.error);

        try {
            Account acct = [select Id from Account where Id = :user2Id];
            System.assert(false, 'Expected exception was not thrown.');
        } catch (QueryException qex) {

        } catch (Exception ex) {
            System.assert(false, ex.getMessage());
        }
    }

    private static void badHttpDelete() {
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = 'Api/DealerProfile_0/reallyreallybadid';
        Api_Result_0 response = Api_Http_DealerProfile_0.remove();
        System.assertEquals(false, response.success, 'Bad delete should have failed.');
        System.assertNotEquals(null, response.error);
    }


    private static void updateNoContactRecord() {
        String email = 'elder@pliny.gr';
        RecordType customerType = [Select Id, Name, sObjectType 
                                   from RecordType
                                   where sObjectType = 'Account' and Name='Company OEM'
                                   limit 1
                                  ];

        Account acct = new Account();

        acct.RecordTypeId = customerType.Id;      
        acct.Name = 'Joe Shmoe';
        acct.ShippingStreet = '2234 Mercury Way';
        acct.ShippingCity = 'Santa Rosa';
        acct.ShippingState = 'CA';
        acct.ShippingCountry = 'US';
        acct.ShippingPostalCode = '94928';

        acct.Dealer_Email__c = email;
        acct.Type = 'Customer';

        insert acct;

        String encEmail = EncodingUtil.urlEncode(email, 'utf-8');
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/Api/DealerProfile_0/' + encEmail;

        Api_Result_0 result = Api_Http_DealerProfile_0.setProfile(
            'JaneShmoe',
            '1121 Some Other Place','Denver', 'CO', '12345', 'US', 
            '7075551212', 'Jane','Shmoe'
        );

        Contact cont = [select Id, Email, Name, FirstName, LastName, MailingStreet, MailingCity, MailingState, 
                        MailingCountry, MailingPostalCode from Contact where email = :email limit 1];
        System.assertNotEquals(null, cont);
        System.assertEquals(email, cont.Email);
        System.assertEquals('Shmoe', cont.LastName);
    }
}
  • 4
    Seems like a pretty descriptive error to me – EricSSH Apr 28 '16 at 19:01
  • Not to mention you need to remove the myunittest method and make each individual method marked with @isTest ir add testmethod to the definition of the method. Doing it like you are puts ALL of the methods within the same transaction and all in one set of governor limits – Eric Apr 29 '16 at 5:33
2

It looks like the error comes from:

    Api_Result_0 result = Api_Http_DealerProfile_0.setProfile(
        'JaneDoh', 
        '1121 Some Other Place','Denver', 'CO', '12345', 'US', 
        '7075551212','Jane',null
    );

I believe that the last parameter, null, is actually meant to go into a Last Name field, which is required for contacts, leads, and users (and other "people" type records).

  • This is what i get now after changing the last name field: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName] Stack Trace Class.Api_DealerProfile_0.createMatchingContactRecord: line 215, column 1 Class.Api_DealerProfile_0.changeProfile: line 185, column 1 Class.Api_Http_DealerProfile_0.setProfile: line 39, column 1 Class.Api_Test_DealerProfile.updateNoContactRecord: line 253, column 1 Class.Api_Test_DealerProfile.myUnitTest: line 22, column 1 – Bookeraward2 Apr 28 '16 at 20:26
  • @Bookeraward2 There might be a defect in your code if you provided a last name and yet you still get the same error. You really just need to read the code backwards from the error until you figure out why LastName is blank. Without all of the code inbetween your unit test and the actual error, there's not much more we can do to help you out. – sfdcfox Apr 28 '16 at 20:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.