+ Start a Discussion
ismyhcismyhc 

Creating custom fields for opportunity in test method?

I have one more test at the moment that I am trying to setup correctly. I think the issue is that the opportunity relies on a few custom fields and Im not quite sure exactly how I would create those in my test?

 

I can provide more info if nessesary.

 

Any insight would be much appreciated!

 

System.DmlException: Update failed. First exception on row 0 with id 006U0000007zk5DIAQ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, aiMatch.aimatchOpportunityGrabTrigger: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.aiMatch.aimatchOpportunityGrabTrigger: line 22, column 1: []

 

private static testmethod void aimatchOpportunityGrabTest() {
        
        Profile p = [select id from profile where name='System Administrator']; 
        User u = new User(alias = 'standt', email='sysadmin@testorg.com', 
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id, 
            timezonesidkey='America/Los_Angeles', username='aimatchtest@testorg.com');


        System.runAs(u) {
            
            List<Opportunity> listOpportunity = new List<Opportunity>();
                    
            // Create Opportunity test data
            Opportunity o = new Opportunity (name = 'test', campaign_created_in_aimatch__c = true, ownerid = '005U0000000gvRnIAI',
                                                        stagename = 'Renew', closedate = system.today(), aimatch_advertiser__c = 'a00U00000016hLW',
                                                        campaign_start_date__c = Date.today(), campaign_end_date__c = Date.today() + 30);
            insert o;                                                                                                  
            
            test.startTest();
                listOpportunity.add(o);
                update listOpportunity;
            test.stopTest();    
        
        }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
ismyhcismyhc

Okay, figured out a better way.

 

aimatch_Advertiser__c a = new aimatch_Advertiser__c (aimatch_AdvertiserId__c = '1', name = 'test');
insert a;   

ID sf_aimatch_AdvertiserId = a.Id;
String string_sf_aimatch_AdvertiserId = String.valueOf(blah);

 Since I only need the one record theres no need to do a query. The I just get the ID value and the convert it to a string!

 

Thanks

All Answers

JitendraJitendra

You are getting error because the user does not have permission on Event and Activity.

 

Please refer below article for more info :

http://kb.omni-ts.com/entry/68/

 

Try below code:

private static testmethod void aimatchOpportunityGrabTest() {
        
        Profile p = [select id from profile where name='System Administrator']; 
        User u = new User(alias = 'standt', email='sysadmin@testorg.com', 
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id, 
            timezonesidkey='America/Los_Angeles', username='aimatchtest@testorg.com');


        System.runAs(u) {
            
            List<Opportunity> listOpportunity = new List<Opportunity>();
                    
            // Create Opportunity test data
            Opportunity o = new Opportunity (name = 'test', campaign_created_in_aimatch__c = true, 
                                                        stagename = 'Renew', 
														closedate = system.today(), 
														aimatch_advertiser__c = 'a00U00000016hLW',
                                                        campaign_start_date__c = Date.today(), 
														campaign_end_date__c = Date.today() + 30);
            insert o;                                                                                                  
            
            test.startTest();
                listOpportunity.add(o);
                update listOpportunity;
            test.stopTest();    
        
        }
}

 I have removed the Owner Id value in above code. give it a try.

ismyhcismyhc

Thanks JlTendra, but no luck with that!

 

I think one of my problems is that the method called relies on a custom object called aimatch_Advertiser__c. Its a lookup field which is required to save.

 

Im having trouble trying to create the test data for that field. I need the salesforce Id of my entry. This is what I am trying.

 

        List<aimatch_Advertiser__c> advObjects = new List<aimatch_Advertiser__c>();
        aimatch_Advertiser__c a = new aimatch_Advertiser__c(aimatch_AdvertiserId__c = '1', name = 'test');
                                       
        insert advObjects;	
        
        string query = 'Select Id from aimatch_Advertiser__c where name =\'test\' ';
        
        // Create object to hold Id Value
        sObject sf_aimatchAdvertiserId = Database.query(query);
        String blah = String.valueOf(sf_aimatchAdvertiserId);

 

15:23:50.094 (94151000)|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject

 

I cant determine whether my query is wrong, or that I am not inserting the data correctly?

 

Thanks!
Jacob

 

ismyhcismyhc

Okay, so I figured it out.

 

But I have a question about how to get just the ID of my query. Below is what I am doing.

 

        aimatch_Advertiser__c a = new aimatch_Advertiser__c (aimatch_AdvertiserId__c = '1', name = 'test');
        insert a;   
        
        string query = 'Select Id from aimatch_Advertiser__c where aimatch_Advertiser__c.name =\'test\' ';
        sObject sf_aimatchAdvertiserId = Database.query(query);
        String blah = String.valueOf(sf_aimatchAdvertiserId).substring(35, 53);

 

The string valueof I was getting was:

aiMatch__aiMatch_Advertiser__c:{Id=a00U0000005Ddt3IAC}

 

So I just did a substring to extract only the id value.

 

There must be a better way to do this?

 

Thanks!
Jacob

ismyhcismyhc

Okay, figured out a better way.

 

aimatch_Advertiser__c a = new aimatch_Advertiser__c (aimatch_AdvertiserId__c = '1', name = 'test');
insert a;   

ID sf_aimatch_AdvertiserId = a.Id;
String string_sf_aimatch_AdvertiserId = String.valueOf(blah);

 Since I only need the one record theres no need to do a query. The I just get the ID value and the convert it to a string!

 

Thanks

This was selected as the best answer