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
Rajendra PogiriRajendra Pogiri 

apex after insert trigger error . please help

sir/mam,   i wrote a after trigger code for :-  if the amount is greater than 50,000 then the opportunity record should be shared with another user, so i in program at query i took the user-id  where euals to ALIAS name.   this is working fine.   But problem is if write the query for LASTNAME instead of ALIAS name, then i am getting error,, please help.....
 
trigger afterinsertTrigger on Opportunity (after insert) {
list share=new list();
     user u=[select id from user where lastname='pogiri'];
    for(Opportunity op:Trigger.New){
        if(op.amount>50000){
            Opportunityshare s=new Opportunityshare();
            s.OpportunityId=op.id;
            s.OpportunityAccessLevel='Edit';
            s.RowCause='Manual';
            s.UserOrGroupId=u.id;
            share.add(s);
        }
    }
      insert share;
}


User-added image
 
Ankit AroraAnkit Arora
Error message clearly states that your result of list with this  user u=[select id from user where lastname='pogiri']; is having more than one record. Means you've more than one user with lastname pogiri. It was working wiht alias as it is unique, so best is if you can pass the Id of user as that will also be unique (in where clause).
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
 
trigger afterinsertTrigger on Opportunity (after insert) 
{
	list share=new list();
    user u=[select id from user where lastname='pogiri' limit 1];
    for(Opportunity op:Trigger.New){
        if(op.amount>50000){
            Opportunityshare s=new Opportunityshare();
            s.OpportunityId=op.id;
            s.OpportunityAccessLevel='Edit';
            s.RowCause='Manual';
            s.UserOrGroupId=u.id;
            share.add(s);
        }
    }
      insert share;
}

Issue in your trigger is that in your org you have more then 1 user with pogiri last Name.

Please let us know if this will help you

Thanks
Amit Chaudhary

 
Amit Chaudhary 8Amit Chaudhary 8
You Can add ID or UserName in Filter also.