0

I am sure someone here can help me figure out what I am doing wrong here.

We have an opportunity record type where partner portal users (they are also contact) have to select a lookup field called "Study" that looks up to a custom object called Project__c when creating oppty. However, Project__c also has another child-related object called Nurse_Training_Session__c.

Nurse_Training_Session__c has a field that looks up to Contact called HcP__c and another lookup field called Project__c. Now, HcP__c is used to track contacts who have been assigned to work on a project (Project__c).

When they log in to the portal to create an opportunity, we want to throw an error if they select a study they have not been assigned.

    @TestVisible
    private static final String UNALLOCATED_STUDY_ERROR_MSG = 'You have not been allocated to work on this study. Please select the correct study';
    
    public void beforeInsert(List<Opportunity> newList){
        Set<Id> projIds = new Set<Id>(); // Store Project__r.Id
        Id nurseSuppliesRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Nurse_Supplies').getRecordTypeId();
        Id curUserId   = userinfo.getUserId(); // Get current logged in user
        User curUser   = [SELECT Id, ContactId FROM User WHERE Id = :curUserId AND ContactId != null];
        Contact contId = [SELECT Id FROM Contact WHERE Id = :curUser.ContactId];

        List<Nurse_Training_Session__c> lstHcps = [SELECT Id, 
                                                      HcP__r.Id, 
                                                          Project__r.Id
                                                     FROM Nurse_Training_Session__c 
                                                      WHERE HcP__r.Id = :contId.Id];
        
        If(curUser != null && !lstHcps.isEmpty()){
        
            for(Nurse_Training_Session__c hcp: lstHcps){
                projIds.add(hcp.Project__r.Id);
            }
            for(Opportunity op: newList){
                if(op.RecordTypeId == nurseSuppliesRecordTypeId 
                   && op.Study__c         != null 
                   && !projIds.contains(op.Study__r.Id))
                {
                    op.Study__c.addError(UNALLOCATED_STUDY_ERROR_MSG); 
                }
            }
        }
    }
3
  • You've mistakenly used Study__r.Id instead of Study__c. Simply switching that out may be all you need.
    – Adrian Larson
    Nov 18, 2021 at 20:56
  • Thanks @AdrianLarson, I've thought of that too and also did as you suggested just in case. But it didn't solve it. Thanks for looking, may be another look might help. For the life of me I just couldn't work this one out.
    – sams ameen
    Nov 19, 2021 at 10:34
  • Have you tried changing op.Study__c.addError to op.addError?
    – Adrian Larson
    Nov 19, 2021 at 15:49

1 Answer 1

0

I was finally able to fix the issue. At a point after making some changes, I started getting other errors which only affected the internal users but with this final changes I got it to work.

@TestVisible
private static final String UNALLOCATED_STUDY_ERROR_MSG = 'You have not been allocated to work on this study. Please select the correct study';

  Set<Id> projIds = new Set<Id>();
  Id nurseSuppliesRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Nurse_Supplies').getRecordTypeId();
        Id curUserId   = userinfo.getUserId();
        User[] curUser   = [SELECT Id, ContactId FROM User WHERE Id = :curUserId AND ContactId != null LIMIT 1];

  IF(curUser.size() > 0){
    List<Nurse_Training_Session__c> lstHcps = [SELECT Id, 
                                                      HcP__c, 
                                                      Project__c
                                               FROM Nurse_Training_Session__c 
                                                 WHERE HcP__c = :curUser[0].ContactId];

    for(Opportunity op: Trigger.new){
        IF(!lstHcps.isEmpty() && op.RecordTypeId == nurseSuppliesRecordTypeId && op.Study__c != null){
            for(Nurse_Training_Session__c hcp: lstHcps)
                {
                   projIds.add(hcp.Project__c);
                }
              IF(!projIds.contains(op.Study__c))
                {
                    op.Study__c.addError(UNALLOCATED_STUDY_ERROR_MSG); 
                }
        } 
   }

 }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .