+ Start a Discussion
Pathan SPathan S 

getting error plz wts the error

Getting Error---Illegal assignment from Decimal to String
I Created one object n auto number field.
trigger Records on Student__c(before insert,before delete)
    {
        List<Student__c> cp = new List<Student__c>();
        set<id> id = new set<id>();
        List <AggregateResult> requirements = new List<AggregateResult>();
        
       if (Trigger.isBefore && Trigger.isInsert)
       {
        List <Student__c> reqSize = [select id,NO_of_Student__c from Student__c];
        for(Student__c pt:trigger.new)
        {
         requirements=[select Count(NO_of_Student__c) from Student__c];
         system.debug('+++++++++++'+requirements.size());
         system.debug('++++++reqSize+++++'+reqSize.size());
         if(requirements.size() > 0)
         {
         for(AggregateResult ar:requirements)
         {
         pt.NO_of_Student__c = Decimal.ValueOf(String.ValueOf(ar.get('expr0')))+1;
          }
      }
         if(reqSize.size() <= 0){
          pt.NO_of_Student__c = 1;
         }
      }
}
      if (Trigger.isBefore && Trigger.isDelete)
      {
         List<Student__c> allRecs = [select id,NO_of_Student__c from Student__c where id not in: trigger.oldmap.keyset()];
         Map<Decimal,Student__c> RecMap = new Map<Decimal,Student__c>();
                 
         for(Student__c rcs: allRecs){
              
              RecMap.put(rcs.NO_of_Student__c,rcs);
         }
         for(Student__c pt1:trigger.old)
          {
              for(Student__c rc : allRecs){
                  if(rc.NO_of_Student__c > pt1.NO_of_Student__c){
                      rc.NO_of_Student__c-=  1;
                  }
              }
        
        
          }
          update allRecs;
          }   
     }
Banwari kevat1Banwari kevat1
Hi Pathan,
Can you please send full error message so that it will easy to find out which line is cause of error.
and filed type of NO_of_Student__c ?
Pathan SPathan S
pt.NO_of_Student__c = Decimal.ValueOf(String.ValueOf(ar.get('expr0')))+1;   in this line showing error.... data type is autonumber for no of student
Banwari kevat1Banwari kevat1
Hi Pathan,
   You can not assign value to a auto number type field. If you do this, it cause an exception of field is not writable. If you want to assign value to this field first of all you have to change the data type of this field to text and then assign value accordingly. 
rajat Maheshwari 6rajat Maheshwari 6

Pathan,

Please give a try with these statement and let me know the same.

 pt.NO_of_Student__c = ((Decimal)ar.get('expr0'))+1;

 

Thanks
Rajat

rajat Maheshwari 6rajat Maheshwari 6

Hi Pathan,

I would like to inform you that, Autonumber field can not be updated, so It would be ideal if you use "No.of student" as number field and use my approach as I have shared above.

 

Thanks

Pathan SPathan S
..Question No:1--I need to autopopulate the number datatype field after the record is deleted.
For example,
I have 5 records and their serial numbers are  1,2,3,4 and 5. If i delete the 3rd record , then the serial number of the 4th record should be automatically populated as 3 and 5th records as 4.
How to achieve this delete operation?

Question No: 2-- I want to Auto Number field perfomance with out selecting autonumber field.through trigger or any anthoer way.
if any one can plz reply