+ Start a Discussion
SFineSFine 

Unit Testing problem

I'm trying to build a unit test for a project my company is working on and we're mostly covered, but the only error that I can't figure out is the following:

 

System.DmlException: ConvertLead failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You do not have the required permission. To continue, you must have the 'Edit' permission on contacts : : []

 

Class.LeadConversion.convertLeadToNewOpp: line 34, column 16 Class.LeadConversion.testConvertLead: line 64, column 9 External entry point

 

We thought it might have to do with the Profile so we chose a user with System Administrator access but it still doesn't seem to work. Any thoughts? The code is below

 

 

public class LeadConversion {
    public static void addContactToOpportunity(Id opportunityId, Id contactId) {
        // make sure the contact doesn't already exist in the opp
        // even though SF allows dupes, we won't
        List<OpportunityContactRole> ex = [select Id from OpportunityContactRole where OpportunityId = :opportunityId and ContactId = :contactId];
        if (!ex.isEmpty()) return;

        OpportunityContactRole ct = new OpportunityContactRole();
        ct.OpportunityId = opportunityId;
        ct.ContactId = contactId;
        insert ct;
    }
    private static String getDefaultConvertStatus() {
        LeadStatus convertstatus = [select Id, MasterLabel from LeadStatus where IsConverted= true limit 1];
        return convertStatus.MasterLabel;
    }
    public static Database.Leadconvertresult convertLeadToExistingOpp(Id opportunityId, Id accountId, Id contactId, Id leadId) {
        Database.Leadconvert lc = new Database.Leadconvert();
        lc.setConvertedStatus(getDefaultConvertStatus());
        lc.setAccountId(accountId);
        lc.setLeadId(leadId);
        lc.setContactId(contactId);
        lc.setDoNotCreateOpportunity(true);
        return Database.convertLead(lc);
    }
    public static Database.Leadconvertresult convertLeadToNewOpp(String opportunityName, Id ownerId, Id accountId, Id contactId, Id leadId) {
        Database.Leadconvert lc = new Database.Leadconvert();
        lc.setConvertedStatus(getDefaultConvertStatus());
        lc.setAccountId(accountId);
        lc.setLeadId(leadId);
        lc.setContactId(contactId);
        lc.setOpportunityName(opportunityName);
        lc.setOwnerId(ownerId);
        return Database.convertLead(lc);
    }
    public static testMethod void testConvertLead() {
        Account ac = new Account(name = 'Test Account');
        insert ac;
        Opportunity op = new Opportunity(name = 'Test Account - Test Method opp', AccountId = ac.Id,StageName='Test',CloseDate=date.Today());
        Profile p=[select id from Profile where name='System Administrator' limit 1];
        user u=[select id from user where LastName='Fine' limit 1];
         op.ownerID=u.id;
        reporting_position__c rp=new reporting_Position__c();
        rp.assigned_User__c=u.id;
        Reporting_Position__c rp2=[select id, territory__c from Reporting_Position__c limit 1];
        rp.Territory__c=rp2.id;
        insert rp;
        op.reporting_Position__c=rp.id;
        insert op;
        Contact ct = new Contact(LastName = 'Test Last Name', accountid=ac.id);
        insert ct;
        Lead l = new Lead(LastName = 'Test Lead',Company='Test Company');
        insert l;
        convertLeadToExistingOpp(op.id, ac.Id, ct.Id, l.Id);
        delete l;
        delete ct;
        delete op;

        //User u2 = new User(LastName = 'some user', Username='test@test.com', Email='test@test.com', Alias='test', CommunityNickname='test', TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey]: [Username, Email, Alias, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey);
        //insert u2;
        User u2=[select id from user where LastName='Fine' and isActive=true limit 1];
        l = new Lead(LastName = 'Test Lead',Company='test company');
        insert l;
        convertLeadToNewOpp('test Opp', u2.Id, ac.Id, ct.Id, l.Id);
        delete ac;
    }
    public static testMethod void testAddContactToOpportunity() {
        Opportunity op = new Opportunity(name = 'Test Method opp', StageName='Test',CloseDate=date.Today());
        Profile p=[select id from Profile where name='System Administrator' limit 1];
        user u=[select id from user where LastName='Fine' limit 1];
        op.ownerID=u.id;
        reporting_position__c rp=new reporting_Position__c();
        rp.assigned_User__c=u.id;
        Reporting_Position__c rp2=[select id, territory__c from Reporting_Position__c limit 1];
        rp.Territory__c=rp2.id;
        insert rp;
        op.reporting_Position__c=rp.id;
        insert op;
        Contact ct = new Contact(LastName = 'Testing123');
        insert ct;
        addContactToOpportunity(op.Id, ct.Id);
        addContactToOpportunity(op.Id, ct.Id);
        delete ct;
        delete op;
    }
}

 

 

bob_buzzardbob_buzzard

When you say you chose a user with System Administrator access, can you clarify what this means in terms of the code?

 

The error indicates that the user running the test doesn't have edit permission on contacts, rather than the permission for the lead owner.

SFineSFine

Interesting theory and what I meant was the lead owner, thinking that was it.

 

But regardless I myself am a system administrator, so wouldn't that mean I should have edit permission?

bob_buzzardbob_buzzard

That should indeed be the case - as sysadmin you'd have modify all data permissions.  

 

The only thing that jumps out about your test case is that you've deleted the contact prior to using the id in the second leadconvert call:

 

 

   delete l;
        delete ct;
        delete op;

        //User u2 = new User(LastName = 'some user', Username='test@test.com', Email='test@test.com', Alias='test', CommunityNickname='test', TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey]: [Username, Email, Alias, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey);
        //insert u2;
        User u2=[select id from user where LastName='Fine' and isActive=true limit 1];
        l = new Lead(LastName = 'Test Lead',Company='test company');
        insert l;
        convertLeadToNewOpp('test Opp', u2.Id, ac.Id, ct.Id, l.Id);

This presumably will try to merge the lead information into a contact that doesn't exist.