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
Becky Miller 15Becky Miller 15 

Help with Test Script -

I am not 100% sure what is wrong with this Test Script:

@isTest
    public class EndoraDocumentAttachment { 

        //Method
        static testMethod void InsertAttachment() {

        Opportunity b = new opportunity();
        b.RecordType.developername = 'Capital';   
        b.name = 'TEST';
        b.Quote_Contact__c = '0033B000007Rz8X';   
        b.StageName = 'Attention';
        b.CloseDate = (System.Today().addDays(1));
        b.Account_Dept__c = 'GI/Endoscopy';
        insert b;

        Attachment attach = new Attachment();

        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=b.id;
        insert attach;

        List<Attachment> attachments=[select id, name from Attachment where parent.id=:b.id];
        System.assertEquals(1, attachments.size());

         attach = [SELECT Id, name from Attachment where parent.id=:b.id];

            delete attach;  

            b.Attachment__c = False;
            update b;      


    }
    }


Errors: System.NullPointerException: Attempt to de-reference a null object
Stack Trace: Class.EndoraDocumentAttachment.InsertAttachment: line 8, column 1
Best Answer chosen by Becky Miller 15
Akhil AnilAkhil Anil
Hi Becky,

When you want to create a new record with a specific recordtype you cannot assign it with a Developername directly. Instead the Id of the RecordType needs to be passed on to the RecordTypeId field. So you would do something like this. Just replace the RecordType assignment in your code with the below.
 
Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Capital').getRecordTypeId();
b.RecordTypeId = devRecordTypeId;

Kindly mark it as an answer if that works so that we can close the thread.
 

All Answers

Akhil AnilAkhil Anil
Hi Becky,

When you want to create a new record with a specific recordtype you cannot assign it with a Developername directly. Instead the Id of the RecordType needs to be passed on to the RecordTypeId field. So you would do something like this. Just replace the RecordType assignment in your code with the below.
 
Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Capital').getRecordTypeId();
b.RecordTypeId = devRecordTypeId;

Kindly mark it as an answer if that works so that we can close the thread.
 
This was selected as the best answer
RD@SFRD@SF
Hi Becky,

Can you try printing the values of b, and check if they are getting set.

Regards
RD