0
trigger AssignToReportingManager on User (After update) {
    String query,userId,reportingManId;
    List<sObject> sObjectList = new List<sObject>();
    //Set<Id> userId = new Set<Id>();
    List<User> userList = new List<User>();
    List<SObject>getSobjectList = new List<SObject>();
    List<SObject>addSobjectList = new List<SObject>();
    List<List<SObject>> sObjectsList = new List<List<SObject>>();

    //Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

    for(User u : Trigger.New){
        if(u.isActive==false){   
            //userId.add(u.id);
            userId = u.id;
            reportingManId = u.Reporting_Manager__c;
        }
    }

    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    for(String str: schemaMap.keySet()){
        System.debug('---'+schemaMap.get(str));
        String str1 = String.valueOf(schemaMap.get(str));
        if(str1 == 'Account' || str1 =='Contact' || str1 =='Opportunity' || str1 =='Lead' || str1 =='Order'){
            getSobjectList = Database.query('SELECT ID , CreatedById FROM   '+schemaMap.get(str)+'  WHERE createdById =\'' + userId + '\''); 
            //List<sObject>getSobjectList = Database.query(query);     
            sObjectsList.add(getSobjectList);   
            System.debug('------'+ getSobjectList);
            System.debug('++++++'+ sObjectsList);  
        }           
    }
    for(List<SObject>sObjList : sObjectsList){
        if(sObjList.getSObjectType()==schemaMap.get('Account')||sObjList.getSObjectType()==schemaMap.get('Contact')||
           sObjList.getSObjectType()==schemaMap.get('Lead')||sObjList.getSObjectType()==schemaMap.get('Opportunity')){
           for(SObject sObj : sObjList){
               System.debug('---------'+sObj);
               sObj.CreatedById = reportingManId; 
               addSobjectList.add(sObj);
           }     
        }

    }
    //if(!addSobjectList.isEmpty()){
    //    update addSobjectList;
   // }        
}

I Don't know , why it's getting error because Account,Contact,Lead and Opportunity CreatedById Api Name is available, Please hepl me for solve out this problem , show me where i'm doing mistake.

1 Answer 1

0

In this loop:

      for(SObject sObj : sObjList){
          System.debug('---------'+sObj);
          sObj.CreatedById = reportingManId; 
          addSobjectList.add(sObj);
      }

you are telling the compiler that the type of sObj is the base class SObject which only has an Id property defined, rather than a specific concrete SObject type, such as Contact, that has (many) additional properties such as CreatedById defined. While this does allow your code to work for multiple concrete SObject types, it means you have to slightly change your approach to accessing properties.

So one way to fix this is to change to use the map-like methods of the base SObject class:

          sObj.put('CreatedById', reportingManId);

Here you are essentially saying "hey compiler, I know that all the concrete types I am using here have a CreatedById property, so don't get in my way".

PS

Note that only writable fields can be set, and as you have discovered, CreatedById is not writeable.

3
  • Thanks but when i'm trying using this then it's getting error at time for trigger execution and the error is 'System.SObjectException: Field CreatedById is not editable: Trigger.AssignToReportingManager: line 38, column 1.' Commented Mar 5, 2019 at 7:56
  • It's showing CreatedById isn't editable , is there any solution for this non editable filed accessability. Commented Mar 5, 2019 at 7:57
  • @ManishMourya My bad. AFAIK in this sort of Apex code, you can't "cheat". The platform sets that field.
    – Keith C
    Commented Mar 5, 2019 at 8:25

You must log in to answer this question.

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