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
SFDC n12SFDC n12 

Trigger Help needed

Hi,

I am having a following requirement for which i have written a trigger which i need help on it


1) I am having a field called as DOS Approver__c which is a lookup to user object

2) I am having another field called as RVP Approver__c which is a look up to user object

3) I am having a custom field called as New Request__c which is a lookup to account  


All these 3 custom fields are present in my custom object called as "Account Exceptions"(Special_Programs_ADR_Exception__c)


My req is when i select a account(New Request__c ) and a DOS Approver__c the RVP Approver__c should be auto populated with the manager of the DOS APPROVER User 

This functionality should be applied only for Group Account record type(Based on the account that i select in my custom object)


MY TRIGGER :


trigger AF_RVP on Special_Programs_ADR_Exception__c (after insert, after update) {
List<Id> rvpId = new List<Id>();
Set<Id> adrId = new Set<Id>();
    for(Special_Programs_ADR_Exception__c spl:trigger.new)
    {
           rvpId.add(spl.DOSUser__c);
           rvpId.add(spl.NewRequest__c);
  adrId.add(spl.Id);
    }
  
   
RecordType  rtypes = [SELECT Name, id FROM RecordType WHERE isActive=true AND SobjectType='Account'AND Name = 'Group Account' limit 1];

User actingUsers =[SELECT Name, ManagerID, Manager.Name, Manager.ManagerID, Manager.Manager.Name FROM User WHERE Id = :rvpId LIMIT 1];

List<Special_Programs_ADR_Exception__c> adrExpList = [Select Id,RVP_Approver__c,NewRequest__c,NewRequest__r.RecordTypeId From Special_Programs_ADR_Exception__c Where Id IN :adrId];
 
List<Special_Programs_ADR_Exception__c> oppts = new List<Special_Programs_ADR_Exception__c>();
for (Special_Programs_ADR_Exception__c obj : adrExpList)
{
if(obj.NewRequest__r.RecordTypeId = rtypes.Id) {
obj.RVP_Approver__c=  actingUsers[0].ManagerID;
}   
}
}

I am getting the following error


Error: Compile Error: Expression must be a list type: SOBJECT:User at line 20 column 23

obj.RVP_Approver__c=  actingUsers[0].ManagerID;


Kindly help me what do i need to change in my trigger

Thanks in Advance
Best Answer chosen by SFDC n12
Ravi NarayananRavi Narayanan

for (Special_Programs_ADR_Exception__c obj : adrExpList)
{
if(obj.NewRequest__r.RecordTypeId = rtypes.Id) {
obj.RVP_Approver__c=  actingUsers[0].ManagerID;
}

 

Update adrExpList;

All Answers

Ravi NarayananRavi Narayanan
actingUsers is not a List. it is just a variable of the Sobject type User. 

So you cannot use actingUsers[0].ManagerId.

Instead of that try actingUsers.ManagerId

Hit Best Answer if it solved your problem 
Ravi NarayananRavi Narayanan
obj.RVP_Approver__c=  actingUsers.ManagerID
SFDC n12SFDC n12
i tried with this also 

if(obj.NewRequest__r.RecordTypeId == rtypes.Id) {
obj.RVP_Approver__c=  actingUsers.ManagerID;
}


its saving the trigger , but not updating the field , is there any issues with my trigger
Ravi NarayananRavi Narayanan

for (Special_Programs_ADR_Exception__c obj : adrExpList)
{
if(obj.NewRequest__r.RecordTypeId = rtypes.Id) {
obj.RVP_Approver__c=  actingUsers[0].ManagerID;
}

 

Update adrExpList;

This was selected as the best answer
SFDC n12SFDC n12
Thanks dude , it worked 
Arunkumar RArunkumar R
Hi SFDC,

You code look quite difficult, Could you try the below one

trigger AF_RVP on Special_Programs_ADR_Exception__c (before insert, before update) 
{
	Set<Id> rvpId = new Set<Id>();

	for(Special_Programs_ADR_Exception__c spl : Trigger.New)
	{
		rvpId.add(spl.DOSUser__c);
	}

	String groupAccountRecordTypeId = [SELECT Name, id FROM RecordType WHERE isActive=true AND SobjectType='Account' AND Name = 'Group Account' limit 1].Id;

	String managerIdforDOSUser =[SELECT Name, ManagerID, Manager.Name, Manager.ManagerID, Manager.Manager.Name FROM User WHERE Id = :rvpId LIMIT 1].ManagerId;


	for (Special_Programs_ADR_Exception__c obj : Trigger.New)
	{
		if(obj.NewRequest__r.RecordTypeId == groupAccountRecordTypeId && managerIdforDOSUser != null)
		{
			obj.RVP_Approver__c=  managerIdforDOSUser;
		}   
	}
}