+ Start a Discussion
sieb4mesieb4me 

explain code

Can anyone please explain to me what this code below is doing? I did not get the last few lines.

//Methd to handle the backing up of actual recordtype on closing and restoring the actual recordtype on re-opening
    public static void updateRecordtypeOnReopen(List<case> caselis,Map<Id,Case> caseNewMap,Map<Id,Case> caseOldMap) {
        //Loop to assign recordtype from the Actual RecordType Id field when the case is getting re-opened
        Schema.DescribeSObjectResult d = Schema.SObjectType.Case;
        Map<Id,Schema.RecordTypeInfo> rtMapById = d.getRecordTypeInfosById();
        Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
        for(Case c:caselis)
        {
            //To backup the actual recordtype on closing/resolving the case
            if(caseOldMap.get(c.Id).Status!=caseNewMap.get(c.Id).Status && (caseNewMap.get(c.Id).Status=='Closed' || casenewMap.get(c.Id).Status=='Resolved') && String.isBlank(c.Actual_RecordType__c))
            {
                c.Actual_RecordType__c = ''+c.RecordTypeId;
                c.Case_RecordType_Name__c =  rtMapById.get(c.RecordTypeId).getName();
            }
            //Restroing the actual recordtype on re-opening the case
            if(caseOldMap.get(c.Id).Status!=caseNewMap.get(c.Id).Status && (caseOldMap.get(c.Id).Status=='Closed' || caseOldMap.get(c.Id).Status=='Resolved') && !String.isBlank(c.Actual_RecordType__c) && (c.Case_RecordType_Name__c =='Administrative' || c.Case_RecordType_Name__c =='Shipping Request' ||c.Case_RecordType_Name__c =='Technical'))
            {
                c.RecordTypeId = (Id)c.Actual_RecordType__c;
            }
        }
       
    }
Best Answer chosen by sieb4me
kevin Carotherskevin Carothers

Looks like a helper class for an after update trigger on case.  If the previous values for the Status field in the cases for the trigger-set  have changed and the previous status was 'Closed' or 'Resolved'  *AND* the current value of the Actual_RecordType__c field is not blank  *AND*  is either 'Administrative', 'Shipping Request', 'Technical'  then update the recordtypeid field to the Actual_RecordType__c.

So, I think what's happening is if the case was reopened, for some specific "Actual_RecordType__c" values, they are copied to the RecordTypeId field.... which I don't think will work.... You have to put an actual "ID" field into the RecordTypeId.

The casting of the Actual_RecordType__c to an Id field I don't think will work.  I'd put the following code in;

public static void updateRecordtypeOnReopen(List<case> caselis, Map<Id,Case> caseNewMap,Map<Id,Case> caseOldMap) {
    
    //Loop to assign recordtype from the Actual RecordType Id field when the case is getting re-opened
    Schema.DescribeSObjectResult d = Schema.SObjectType.Case;
    Map<Id,Schema.RecordTypeInfo> rtMapById = d.getRecordTypeInfosById();
    Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();

    List<RecordType> theRTList = new List<RecordType> ([Select Id, Name from RecordType WHERE SobjectType = 'Case']);
    Map<String, Id> xRTList = new Map<String, Id>();
        
    for (RecordType xr :theRTList) {
        xRTList.put(xr.Name, xr.Id);
        }
            
    for(Case c:caselis) {
        //To backup the actual recordtype on closing/resolving the case
        if(caseOldMap.get(c.Id).Status!=caseNewMap.get(c.Id).Status && (caseNewMap.get(c.Id).Status=='Closed'
                || casenewMap.get(c.Id).Status=='Resolved') && String.isBlank(c.Actual_RecordType__c)) {
            c.Actual_RecordType__c = ''+c.RecordTypeId;
            c.Case_RecordType_Name__c =  rtMapById.get(c.RecordTypeId).getName();
            }
        
        //Restroing the actual recordtype on re-opening the case
        if(caseOldMap.get(c.Id).Status!=caseNewMap.get(c.Id).Status
            && (caseOldMap.get(c.Id).Status=='Closed'
            || caseOldMap.get(c.Id).Status=='Resolved')
            && !String.isBlank(c.Actual_RecordType__c)
                && (c.Case_RecordType_Name__c =='Administrative'
                || c.Case_RecordType_Name__c =='Shipping Request'
                || c.Case_RecordType_Name__c =='Technical'))   {
                       
            c.RecordTypeId = xRTList.get(c.Actual_RecordType__c);           
            }
        }
       
    }




All Answers

kevin Carotherskevin Carothers

Looks like a helper class for an after update trigger on case.  If the previous values for the Status field in the cases for the trigger-set  have changed and the previous status was 'Closed' or 'Resolved'  *AND* the current value of the Actual_RecordType__c field is not blank  *AND*  is either 'Administrative', 'Shipping Request', 'Technical'  then update the recordtypeid field to the Actual_RecordType__c.

So, I think what's happening is if the case was reopened, for some specific "Actual_RecordType__c" values, they are copied to the RecordTypeId field.... which I don't think will work.... You have to put an actual "ID" field into the RecordTypeId.

The casting of the Actual_RecordType__c to an Id field I don't think will work.  I'd put the following code in;

public static void updateRecordtypeOnReopen(List<case> caselis, Map<Id,Case> caseNewMap,Map<Id,Case> caseOldMap) {
    
    //Loop to assign recordtype from the Actual RecordType Id field when the case is getting re-opened
    Schema.DescribeSObjectResult d = Schema.SObjectType.Case;
    Map<Id,Schema.RecordTypeInfo> rtMapById = d.getRecordTypeInfosById();
    Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();

    List<RecordType> theRTList = new List<RecordType> ([Select Id, Name from RecordType WHERE SobjectType = 'Case']);
    Map<String, Id> xRTList = new Map<String, Id>();
        
    for (RecordType xr :theRTList) {
        xRTList.put(xr.Name, xr.Id);
        }
            
    for(Case c:caselis) {
        //To backup the actual recordtype on closing/resolving the case
        if(caseOldMap.get(c.Id).Status!=caseNewMap.get(c.Id).Status && (caseNewMap.get(c.Id).Status=='Closed'
                || casenewMap.get(c.Id).Status=='Resolved') && String.isBlank(c.Actual_RecordType__c)) {
            c.Actual_RecordType__c = ''+c.RecordTypeId;
            c.Case_RecordType_Name__c =  rtMapById.get(c.RecordTypeId).getName();
            }
        
        //Restroing the actual recordtype on re-opening the case
        if(caseOldMap.get(c.Id).Status!=caseNewMap.get(c.Id).Status
            && (caseOldMap.get(c.Id).Status=='Closed'
            || caseOldMap.get(c.Id).Status=='Resolved')
            && !String.isBlank(c.Actual_RecordType__c)
                && (c.Case_RecordType_Name__c =='Administrative'
                || c.Case_RecordType_Name__c =='Shipping Request'
                || c.Case_RecordType_Name__c =='Technical'))   {
                       
            c.RecordTypeId = xRTList.get(c.Actual_RecordType__c);           
            }
        }
       
    }




This was selected as the best answer
sieb4mesieb4me
Thanks a lot.