+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Not Able To Update in Trigger (After Insert)

HI,

I can't get the CaseID from Case activity based on the condition. Can Anyone please check and let me know how can i solve this problem.

Trigger
/* 
Name          : BusinesStatusAndStatusUpdate
Description   : Update Receive Document , Receive & Track Credit Balance, Contact Provider, Confirm Credit Balance,Verify Provider Billing Issues And Create a New Case
Created By    : OrbitTeam_Soundar
Created Date  : 08/11/2017 
*/

trigger BusinesStatusAndStatusUpdate on Case_Activity__c (After Update) {
    Set<Id> caseId = New Set<Id>();
    List<Case_Activity__c> casActList = New List<Case_Activity__c>();
    List<Case_Activity__c> Up_casActList = New List<Case_Activity__c>();
    //For Getting a record Type
    Id creditbalreportId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Receive And Track Credit Balance').getRecordTypeId();
    System.debug('Credit Balance Report recordType name |' + creditbalreportId );
    string ownerId =''; 
    For(Case_Activity__c ca : Trigger.New){
        system.debug('Checking for CaseId ');
        If(ca.Case__c != Null && ca.RecordtypeId == creditbalreportId && ca.Activity_Business_Status__c == 'Credit Balance Reported' 
        && ca.Case__r.Type=='Credit – Balance Recoupment'){
            ownerId  = ca.Case__r.OwnerId;
            caseId.add(ca.Case__c);
            System.debug('Case ID | ' + caseId);
        }
    }
    
    Id receiveDocId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Receive Document').getRecordTypeId();
    System.debug('receive doc Rec |' + receiveDocId );
    Id ContProviderId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Contact Provider').getRecordTypeId();
    System.debug('Contact Provider Rec |' + ContProviderId );
    Id ConCredBalId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Confirm Credit Balance').getRecordTypeId();
    System.debug('Credit Balance Rec |' + ConCredBalId );
    Id BillIssueId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Verify Provider Billing Issues').getRecordTypeId();
    System.debug('Billing Issue Rec |' + BillIssueId );
    
    list<id> st_RecTyId = new list<id>();
    st_RecTyId.add(creditbalreportId);
    st_RecTyId.add(receiveDocId);
    st_RecTyId.add(ContProviderId);
    st_RecTyId.add(ConCredBalId);
    st_RecTyId.add(BillIssueId);
    
    List<Case_Activity__c> caActList = [Select id,Name, Case__c,Activity_Business_Status__c,Status__c  from Case_Activity__c Where Case__c  =:CaseId AND RecordTypeId IN: st_RecTyId];
    
    for(Case_Activity__c cAct : caActList){
        If(cAct.Activity_Business_Status__c != 'Activity Not Applicable'){
            cAct.Activity_Business_Status__c = 'Activity Not Applicable';
        }
        if(cAct.Status__c != 'Completed'){
            cAct.Status__c = 'Completed';
        }
        Up_casActList.add(cAct);
    }

    
    System.debug('casActList Size | ' + casActList.size());
    If(Up_casActList.size() > 0){
        Update Up_casActList;
        Case cs = new Case();
        cs.Case_Name__c= 'Provider Audit - Onsite';
        cs.Type='Provider Audit - Onsite';
        cs.OwnerId = ownerId;
        insert cs;
    }

}

Thanks In Advance.

Soundar.
Best Answer chosen by Soundar Raj Ponpandi
Soundar Raj PonpandiSoundar Raj Ponpandi
HI Tej Pal,

I corrected above code and make some changes.. Your Idea is little bit of usefull to myself (Thanks for your valuable Support Tej Pal).


Trigger:
 
trigger BusinesStatusAndStatusUpdate on Case_Activity__c (After Update) {
    Set<Id> caseId = New Set<Id>();
    List<Case_Activity__c> casActList = New List<Case_Activity__c>();
    List<Case_Activity__c> Up_casActList = New List<Case_Activity__c>();
    Set<Id> OwnerId1 = New Set<Id>();

        //For Getting a record Type
        Id creditbalreportId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Receive & Track Credit Balance').getRecordTypeId();
    //System.debug('Credit Balance Report recordType name |' + creditbalreportId );
    string ownerId =''; 
    For(Case_Activity__c ca : Trigger.New){
        system.debug('Checking for CaseId ');
        If(ca.Case__c != Null && ca.RecordtypeId == creditbalreportId && ca.Activity_Business_Status__c == 'Credit Balance Reported'){
            ownerId  = ca.Case__r.OwnerId;
            caseId.add(ca.Case__c);
            OwnerId1.add(ca.CreatedById);
            System.debug('Case ID | ' + caseId);
        }
    }
    System.debug('Owner Id |' + OwnerId1);
    Id receiveDocId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Receive Document').getRecordTypeId();
    //System.debug('receive doc Rec |' + receiveDocId );
    Id ContProviderId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Contact Provider').getRecordTypeId();
    //System.debug('Contact Provider Rec |' + ContProviderId );
    Id ConCredBalId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Confirm Credit Balance').getRecordTypeId();
    //System.debug('Credit Balance Rec |' + ConCredBalId );
    Id BillIssueId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Verify Provider Billing Issues').getRecordTypeId();
    //System.debug('Billing Issue Rec |' + BillIssueId );
    
    list<id> st_RecTyId = new list<id>();
    st_RecTyId.add(creditbalreportId);
    st_RecTyId.add(receiveDocId);
    st_RecTyId.add(ContProviderId);
    st_RecTyId.add(ConCredBalId);
    st_RecTyId.add(BillIssueId);
    
    List<Case_Activity__c> caActList = [Select id,Name, Case__c,Activity_Business_Status__c,Status__c  from Case_Activity__c Where Case__c  =:CaseId AND RecordTypeId IN: st_RecTyId];
    System.debug('Case Activity List |  ' + caActList);
    for(Case_Activity__c cAct : caActList){
        If(cAct.Activity_Business_Status__c != 'Activity Not Applicable'){
            cAct.Activity_Business_Status__c = 'Activity Not Applicable';
        }
        if(cAct.Status__c != 'Completed'){
            cAct.Status__c = 'Completed';
        }
        Up_casActList.add(cAct);
    }
    
    
    System.debug('casActList Size | ' + casActList.size());
    If(Up_casActList.size() > 0){
        Update Up_casActList;
        Case cs = new Case();
        cs.Case_Name__c= 'Provider Audit - Onsite';
        cs.Type='Provider Audit - Onsite';
        //cs.OwnerId = ownerId;
        //User usersRoleName =[select Id, Name, UserRole.Name  from User Where Id=:UserInfo.getUserId()];
        //cs.OwnerId = UserInfo.getUserRoleId();
        //System.debug('Owner Id | ' + ownerId);
        system.debug('New Case Name | ' + cs.Case_Name__c );
        IF(UserInfo.getUserName() != Null){
            cs.OwnerId = UserInfo.getUserId();
        }
        insert cs;
        System.debug('Case Created Successfully');
    }
    
}


Thnaks.
 

All Answers

Tej PalTej Pal
Hi Soundar,

Please change your query in line 42 'Where Case__c =:CaseId ' to 'Where Case__c IN : CaseId '.

If this answers your question mark Best Answer it as solution and then hit Like!
Soundar Raj PonpandiSoundar Raj Ponpandi
HI Tej Pal,

I corrected above code and make some changes.. Your Idea is little bit of usefull to myself (Thanks for your valuable Support Tej Pal).


Trigger:
 
trigger BusinesStatusAndStatusUpdate on Case_Activity__c (After Update) {
    Set<Id> caseId = New Set<Id>();
    List<Case_Activity__c> casActList = New List<Case_Activity__c>();
    List<Case_Activity__c> Up_casActList = New List<Case_Activity__c>();
    Set<Id> OwnerId1 = New Set<Id>();

        //For Getting a record Type
        Id creditbalreportId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Receive & Track Credit Balance').getRecordTypeId();
    //System.debug('Credit Balance Report recordType name |' + creditbalreportId );
    string ownerId =''; 
    For(Case_Activity__c ca : Trigger.New){
        system.debug('Checking for CaseId ');
        If(ca.Case__c != Null && ca.RecordtypeId == creditbalreportId && ca.Activity_Business_Status__c == 'Credit Balance Reported'){
            ownerId  = ca.Case__r.OwnerId;
            caseId.add(ca.Case__c);
            OwnerId1.add(ca.CreatedById);
            System.debug('Case ID | ' + caseId);
        }
    }
    System.debug('Owner Id |' + OwnerId1);
    Id receiveDocId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Receive Document').getRecordTypeId();
    //System.debug('receive doc Rec |' + receiveDocId );
    Id ContProviderId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Contact Provider').getRecordTypeId();
    //System.debug('Contact Provider Rec |' + ContProviderId );
    Id ConCredBalId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Confirm Credit Balance').getRecordTypeId();
    //System.debug('Credit Balance Rec |' + ConCredBalId );
    Id BillIssueId = Schema.SObjectType.Case_Activity__c.getRecordTypeInfosByName().get('Verify Provider Billing Issues').getRecordTypeId();
    //System.debug('Billing Issue Rec |' + BillIssueId );
    
    list<id> st_RecTyId = new list<id>();
    st_RecTyId.add(creditbalreportId);
    st_RecTyId.add(receiveDocId);
    st_RecTyId.add(ContProviderId);
    st_RecTyId.add(ConCredBalId);
    st_RecTyId.add(BillIssueId);
    
    List<Case_Activity__c> caActList = [Select id,Name, Case__c,Activity_Business_Status__c,Status__c  from Case_Activity__c Where Case__c  =:CaseId AND RecordTypeId IN: st_RecTyId];
    System.debug('Case Activity List |  ' + caActList);
    for(Case_Activity__c cAct : caActList){
        If(cAct.Activity_Business_Status__c != 'Activity Not Applicable'){
            cAct.Activity_Business_Status__c = 'Activity Not Applicable';
        }
        if(cAct.Status__c != 'Completed'){
            cAct.Status__c = 'Completed';
        }
        Up_casActList.add(cAct);
    }
    
    
    System.debug('casActList Size | ' + casActList.size());
    If(Up_casActList.size() > 0){
        Update Up_casActList;
        Case cs = new Case();
        cs.Case_Name__c= 'Provider Audit - Onsite';
        cs.Type='Provider Audit - Onsite';
        //cs.OwnerId = ownerId;
        //User usersRoleName =[select Id, Name, UserRole.Name  from User Where Id=:UserInfo.getUserId()];
        //cs.OwnerId = UserInfo.getUserRoleId();
        //System.debug('Owner Id | ' + ownerId);
        system.debug('New Case Name | ' + cs.Case_Name__c );
        IF(UserInfo.getUserName() != Null){
            cs.OwnerId = UserInfo.getUserId();
        }
        insert cs;
        System.debug('Case Created Successfully');
    }
    
}


Thnaks.
 
This was selected as the best answer