+ Start a Discussion
Ken sfdc1Ken sfdc1 

Add logic to existing trigger case to account field update


·        "Active Persistence Status" picklist from case -- adherence record type update on Account record
·        1. On Drug - Will Re-Order
·        2. On Drug - Unlikely to Re-Order
·        3. Not on Drug - Unlikely to Re-Start
Trigger CaseAfterInsertUpdate on Case (after insert, after update) {
    Set<String> caseIds = new Set<String>();
    
    for (Case c : Trigger.new)
    {
        if(Trigger.isInsert)
        {
            caseIds.add(c.Id);
        }
        else
        {
            Case oldCase = Trigger.oldMap.get(c.Id);
            if(oldCase.Nutrition_Consult_Dietitian__c != c.Nutrition_Consult_Dietitian__c)
            {
                caseIds.add(c.Id);
            }
        }
    }
    
    RecordType rt = [select Id from RecordType where Name = 'Nutrition Consult' and sObjectType = 'Case'];
    
    Map<String, String> AccountMap = new Map<String, String>();
    for(Case c : [select Nutrition_Consult_Dietitian__c, AccountId from Case where Id in :caseIds and (Type = 'Initial' or Type = 'Follow Up')  and  RecordTypeId = :rt.Id])
    {
        AccountMap.put(c.AccountId, c.Nutrition_Consult_Dietitian__c);
    }
    List<Account> accounts = new List<Account>();
    for(Account acc : [select Id, RD_Consult__c from Account where Id in :AccountMap.keySet()])
    {
        acc.RD_Consult__c = AccountMap.get(acc.Id);
        accounts.add(acc);
    }
    
    try {
        update accounts;
    } catch (Exception ex) {
        System.debug('Could not update Last Survey Sent field on Account with cause: ' + ex.getCause());
    }
}

Add the logic to this trigger pls
Best Answer chosen by Ken sfdc1
ManojjenaManojjena
Hi Ken,

Try with below code it wil work for sure !
 
Trigger CaseAfterInsertUpdateactivepersistence on Case (after insert, after update) {
    List<Account> accList = new List<Account>();
    for (Case c : Trigger.new) {
        Account acc = new Account(
            Id = c.AccountId,
            Active_Persistence_Status__c = c.Active_Persistence_Status__c
            );
        
        accList.add(acc);
    }
    try {
        update accList;
    } catch (Exception ex) {
        System.debug('Could not update Last Survey Sent field on Account with cause: ' + ex.getCause());
    }
}

Let me know if it helps !!!

Thanks 
Manoj

All Answers

SonamSonam (Salesforce Developers) 
From what I understand, you are trying to update the Account record type depending on the vlaue of the picklist on the case.Is that correct?
Ken sfdc1Ken sfdc1
Active Persistence Status" picklist is there on both case and account but iam trying to automate it from case object to account object. when updated or modified
·        1. On Drug - Will Re-Order
·        2. On Drug - Unlikely to Re-Order
·        3. Not on Drug - Unlikely to Re-Start
Ken sfdc1Ken sfdc1
Trigger CaseAfterInsertUpdateactivepersistence on Case (after insert, after update) {
    List<Account> accList = new List<Account>();
    for (Case c : Trigger.new) {
        Account acc = new Account(
            Id = c.Account.Id,
            Active_Persistence_Status__c = c.Active_Persistence_Status__c
            );
        
        accList.add(acc);
    }
    try {
        update accList;
    } catch (Exception ex) {
        System.debug('Could not update Last Survey Sent field on Account with cause: ' + ex.getCause());
    }
}

My trigger is not working can anyone verify according to it.
ManojjenaManojjena
Hi Ken,

Try with below code it wil work for sure !
 
Trigger CaseAfterInsertUpdateactivepersistence on Case (after insert, after update) {
    List<Account> accList = new List<Account>();
    for (Case c : Trigger.new) {
        Account acc = new Account(
            Id = c.AccountId,
            Active_Persistence_Status__c = c.Active_Persistence_Status__c
            );
        
        accList.add(acc);
    }
    try {
        update accList;
    } catch (Exception ex) {
        System.debug('Could not update Last Survey Sent field on Account with cause: ' + ex.getCause());
    }
}

Let me know if it helps !!!

Thanks 
Manoj
This was selected as the best answer