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
Aditya RautAditya Raut 

Can Anyone help me for with test class for the following trigger?

trigger createfutherdetails on Opportunity (before insert) {
    Set<Id> accSpecificIds = new Set<Id>();
    
    List<OpportunityContactRole> ocrlist = new List<OpportunityContactRole>(); 
    
    Map<Id, List<Contact>> accountSpecificContacts = new Map<Id, List<Contact>>();
    
    for(Opportunity o: Trigger.New) { 
        if(o.AccountSpecificOppId__c != Null){
            accSpecificIds.add(o.AccountSpecificOppId__c);
        }
    }
    for(Contact con: [select Id,AccountSpecificId__c from Contact where AccountSpecificId__c IN: accSpecificIds]) {
            if(!accountSpecificContacts.containsKey(con.AccountSpecificId__c)){
                accountSpecificContacts.put(con.AccountSpecificId__c, new List<Contact>());
                accountSpecificContacts.get(con.AccountSpecificId__c).add(con);
            }
    }
    for(Opportunity opp: Trigger.New) {
                if(accountSpecificContacts.containskey(opp.AccountSpecificOppId__c)&& accountSpecificContacts.get(opp.AccountSpecificOppId__c) != NULL) { 
                    Boolean isFirstContact = true;
                                   
                    for(Contact c: accountSpecificContacts.get(opp.AccountSpecificOppId__c)) {
                        OpportunityContactRole ocr = new OpportunityContactRole(ContactId = c.Id, OpportunityId = opp.Id);
                        if(isFirstContact) {
                            ocr.IsPrimary = true; 
                            isFirstContact = false;
                        }
                        ocrList.add(ocr);
                    }
                }
            
    }
    
        if(ocrlist.size() > 0)
        insert ocrlist;
        
        
    }
Best Answer chosen by Aditya Raut
CharuDuttCharuDutt
Hii Adutya
Try Below Code In Before Insert Method You Will Not Get Opporutnity Id Make It After Insert
@isTest
Public Class createfutherdetailsTest{
@isTest
public Static Void unittest(){
Account Acc = new Account();
Acc.Name = 'test Account'
insert Acc;
Contact Con = new Contact();
Con.LastName = 'Test Contact';
Con.AccountSpecificId__c  = Acc.Id;
Con.AccountId = Acc.Id;
Insert Con;
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.StageName = 'Closed Won'
opp.CloseDate = System.today();
opp.AccountSpecificId__c  = Acc.Id;
opp.AccountId = Acc.Id;
insert Opp;
}
} 
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

PriyaPriya (Salesforce Developers) 

Hey Aditya,

Hope you are doing good.

Kindly provide the test code you have attempted, we will try to fix to increase the code coverage.

Thanks,

Priya Ranjan

CharuDuttCharuDutt
Hii Adutya
Try Below Code In Before Insert Method You Will Not Get Opporutnity Id Make It After Insert
@isTest
Public Class createfutherdetailsTest{
@isTest
public Static Void unittest(){
Account Acc = new Account();
Acc.Name = 'test Account'
insert Acc;
Contact Con = new Contact();
Con.LastName = 'Test Contact';
Con.AccountSpecificId__c  = Acc.Id;
Con.AccountId = Acc.Id;
Insert Con;
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.StageName = 'Closed Won'
opp.CloseDate = System.today();
opp.AccountSpecificId__c  = Acc.Id;
opp.AccountId = Acc.Id;
insert Opp;
}
} 
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer