-1

I have written the below trigger where the fields of objects need to make it as editable for the user based on the one field of same object and profile.

firstly I have made all the fields of the object to read only for the profile. and then I have created the Permissionset for the fields which I want to give edit access for the user. then I wrote a trigger where it has to check 1.field condition and 2.need to get logged in user who is trying to create record, then need to check the profile name , then need to assign that permission set to the user.

trigger test on objectx(before insert)
{
if (Trigger.isBefore)
{
  if (Trigger.isInsert) 
  {
    for ( objectx x : trigger.new)
     {
      if(x.status == 'Draft')
         {
         id id1 = userinfo.getUserId();
         string pf = userinfo.getProfileId();
          if(pf=='00eG000000101pK')
        {
                 PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = '0PS3C0000004Fpa',AssigneeId = id1);
         insert psa; 
        }
        insert x;
         }
      }
   } 
 }        
}

getting error as

line 16:7 no viable alternative at character ' '

  • 1
    Did you copy this code from somewhere? Try typing it out yourself. Seems like a zero width space character might have crept into your code. – Adrian Larson Apr 18 '17 at 17:18
  • 1
    Also, I strongly advise against hardcoding IDs. It's much easier to use the Profile Name, or similar. – Sebastian Kessel Apr 18 '17 at 17:20
  • ok I wil replace the profile id with the profile name. but can you help me y am I getting the error – Sayed Apr 18 '17 at 17:23
  • One more thing you should not put DML insert inside for loop no never! – itzmukeshy7 Apr 18 '17 at 18:01
1

Here is the strange solution of no viable alternative at character ' '

  • Copy the code from the editor.
  • Paste the code into Notepad (if OS is Windows) or in TextEdit (MacOS).
  • Copy again from the notepad and paste into editor
  • Click Save.

This error is coming because code is copied from any website and paste into editor most probably, it is dev console.

I was able to save the code without any issue:- enter image description here

In addition you must move al hardcoded text into Custom Labels.

  • Hi ashwani, I haven't copied code from anywhere. I just wrote sample in notepad then copied. as you said I copied once again from notepad and pasted in editor. still facing same issue. is this code is right? – Sayed Apr 18 '17 at 18:34
  • I copied the code from you question and paste in dev console. I was able to save it. @Sayed – Ashwani Apr 18 '17 at 18:58
  • Thanks Ashwani. now i am able to save without error. can you please help me y I am getting the error while updating the existing record as 'Apex trigger PSA_on_WO caused an unexpected exception, contact your administrator: PSA_on_WO: execution of BeforeUpdate caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: ()' – Sayed Apr 20 '17 at 13:52
  • trigger PSA_on_WO on service_Order__c (before update){ if(Trigger.isBefore) { if(Trigger.isUpdate) { for ( Service_Order__c wo : trigger.old) { if(Order_Status__c == 'Pending') { id id1 = userinfo.getUserId(); string pf = userinfo.getProfileId(); if(pf=='profile1') { PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = '0PS3C0000004Fpa',AssigneeId = id1); insert psa; } update wo; } } } } } @ashwani – Sayed Apr 20 '17 at 13:53
  • you can not dml update insert same record which is part of trigger.new. @Sayed only make changes dont DML – Ashwani Apr 20 '17 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.