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
babloo123babloo123 

Bulkifying Trigger causing error

When I try to dataload I am getting the below error but line 20 whwn checked in query editor works fine. Can some one help where is the mistake.

ERROR: ResearchApplicationTriggers: execution of AfterInsertcaused by: System.QueryException: List has no rows for assignment to SObjectClass.ResearchApplicationTriggerClass.createCOIautomatically: line 20, column 1Trigger.ResearchApplicationTriggers: line 7, column 1

Trigger Handler 

----------------------------------------
//This Trigger is used to automatically create COI and update TechnicalAbstract from RAObject to COI
trigger ResearchApplicationTriggers on Research_Application__c (after insert, after update) {
     ResearchApplicationTriggerClass handler = new ResearchApplicationTriggerClass();
    for(Research_Application__c rap:trigger.new){
     if(Trigger.isInsert && Trigger.isAfter){
         system.debug(rap);
     handler.CreateCOIautomatically(rap); 
         //handler.UpdateCOItechnicalabstract(rap);
    }
     if(Trigger.isUpdate && Trigger.isAfter)
    {
        handler.UpdateCOItechnicalabstract(rap);
    }
}
}

----------------------------------------------------------------------
Trigger Handler Class

//This is a TriggerHandler Class that handles the triggers on ResearchApplication Object
public class ResearchApplicationTriggerClass {
public ResearchApplicationTriggerClass(){}
        //This method creates the COI automatically for all reviewers in the panel assignment for that particular panel.
    public void createCOIautomatically(Research_Application__c rap)
    {
      //Below Soql query gets the list of Reviewers assigned in the pane assignments for that particular Panel  
        list<panel_assignment__c> lp=[select Reviewer_Name__c from panel_assignment__C where panel__C in (select id from panel__c where id =: rap.panel__C )];
system.debug('The Reviewers are' + lp);
        
        for (integer i=0;i<lp.size();i++)
    {
  system.debug(lp[i].Reviewer_Name__c);
   COI_Expertise__c  c=new COI_Expertise__c();
        //Initialize and assign the the COI object with the fields we require
        c.Research_Application__c= rap.Id;
        system.Debug(c.Research_Application__c);
         c.Reviewer_Name__c = lp[i].Reviewer_Name__c;
      system.debug(lp[i].Reviewer_Name__c);
        user u=[select id,firstname,lastname from user where contactID=:lp[i].Reviewer_Name__c];
      Id  usrid=string.valueof(u.id);
        system.debug('USer ID:' + u.id + 'Reviewer ID' + lp[i].Reviewer_Name__c);
        //Assign the owner of the COI to be the same as Reviewer Name
          c.Ownerid = usrid;
       insert(c);
             } 
        
        
            
    }
    
    //This method copies the technical abstract on Research Application to the COI associated to it.
        public void updateCOItechnicalabstract(Research_Application__c rap)
    {
        //Below is the soql query to get the technical abstract from that particular research application
        Research_Application__c fa=[select id,technical_abstract__c from Research_Application__c where id=:rap.Id limit 1];
    system.debug(fa);

    //Below is the soql query to get the COI records from that particular research application
 list<COI_Expertise__c> c=[select id,technical_abstract__c from COI_Expertise__c where Research_Application__r.Id=:fa.Id];
  //Assign the technical abstract from RA to respective COI's
    for(COI_Expertise__c ci:c)
    {
   ci.Technical_Abstract__c=fa.Technical_Abstract__c;
     update(c);   
    }
    
        }
}
Jigar.LakhaniJigar.Lakhani
Hello,

You are assigning a SOQL query to direct object and it is not finding any record for it that's why it is giving "List has no rows for assignment".
As well as you have used SOQL query in for loop which is not correct, I have updated "createCOIautomatically".

Please update "createCOIautomatically" in trigger handler class.
//This method creates the COI automatically for all reviewers in the panel assignment for that particular panel.
    public void createCOIautomatically(Research_Application__c rap) {
		
		//Below Soql query gets the list of Reviewers assigned in the pane assignments for that particular Panel  
		List<panel_assignment__c> listPanelAssignment = new List<panel_assignment__c>([SELECT Reviewer_Name__c FROM panel_assignment__C 
																						WHERE panel__C In: (SELECT Id FROM panel__c WHERE Id =: rap.panel__c)]);
		
		Set<Id> setContactId = new Set<Id>();
		for (panel_assignment__c objPanelAssignment:listPanelAssignment) {
			if (objPanelAssignment.Reviewer_Name__c != null) {
				setContactId.Add(objPanelAssignment.Reviewer_Name__c);
			}
		}
		
		List<User> listUser = new List<User>([SELECT Id,ContactId FROM User WHERE ContactId In:setContactId]);
		Map<Id, User> mapContactIdUser = new Map<Id, User>();
		for (User objUser:listUser) {
			mapContactIdUser.Put(objUser.ContactId, objUser);
		}
		
		if (listPanelAssignment != null && listPanelAssignment.size() > 0) {
			for (panel_assignment__c objPanelAssignment:listPanelAssignment) {
				
				COI_Expertise__c  objCOIExpertise = new COI_Expertise__c();
				//Initialize and assign the the COI object with the fields we require
				objCOIExpertise.Research_Application__c= rap.Id;
				objCOIExpertise.Reviewer_Name__c = objPanelAssignment.Reviewer_Name__c;
				
				//Assign the owner of the COI to be the same as Reviewer Name
				if (mapContactIdUser != null && mapContactIdUser.sie() > 0 && mapContactIdUser.Get(objPanelAssignment.Reviewer_Name__c) != null) {
					objCOIExpertise.OwnerId = mapContactIdUser.Get(objPanelAssignment.Reviewer_Name__c).Id;
				}
				
				Insert objCOIExpertise;
			}
		}
    }

Thanks and Cheers,
Jigar