+ Start a Discussion
Ken sfdc1Ken sfdc1 

Trigger for updating from case object to Account and validation on before insert

I have 2 requirements of trigger

1. SR date field on Case of record type nutrition consult when it is filled it must be updated to account record with SR date field.
2. There must be a validation of before insert when a new case record is created then if the SR date field on account record is filled then they cannot create a new case record type of nutrition consult with type = initial.

Can anyone help in creating a child to parent update of a date field and also a validation like this  by a trigger.
PratikPratik (Salesforce Developers) 
Hi Ken,

1. This you can do with trigger. In trigger insert and update event, check for case recordtype and if it matches then update the corresponding account's field.

2. The second condition can be done through Validation Rule on Case object.  

           ISBLANK(Account.SRdate__C )     

Hope this will help you.

Thanks,
Pratik
 
MCunninghamMCunningham
Had you considered doing this with workflow instead.  As Ptatik commented a valdiation rule will prevent the save - and a field update can set field Account.SRDate_c without the need for any code.

the true artists of SFDC avoid code like ebola - so give this a try as it will open a world of possibilities!!

Good luck

Michael
PratikPratik (Salesforce Developers) 
Thanks Michael for your inputs!

@Ken: Try the workflow + Validation approach and in case it doesn't work try for Trigger+Validation. Let us know if you have any issues.

regards,
Pratik
Ken sfdc1Ken sfdc1
Hi Pratik,

  We cannot do a workflow for an update from child to Parent is not possible right?

Thats why I need to add the logic in the existing trigger  class I added about nutrition consult but I need the logic for getting SR Date__c.

public class SR_DateApprovedUpdateTriggerHandler{
    public void TriggerAfterUpdate(Case[] cs){
         updateDateApproved(cs);       
    }
    public void updateDateApproved(Case[] cs){
        Set<id> caseId = new Set<id>();
        Set<id> TCcaseID = new Set<id>();
        Id IARecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Insurance Authorization').getRecordTypeId(); 
        Id TCRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Test Claim').getRecordTypeId();
        Id NCRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Nutrition Consult').getRecordTypeId();

        for(Case c: cs){
                caseId.add(c.Id);
            TCcaseID.add(c.id);
            NCcaseID.add(c.id);
        }
        
        List<Account> patientList = [SELECT id, Date_Approved__c,First_Ship_Date__c, (SELECT id, Status, AccountId, Date_Approved__c FROM Cases WHERE Id IN:caseId ORDER BY Date_Approved__c desc) FROM Account WHERE Id IN(SELECT Accountid FROM Case WHERE Id IN:caseId AND RecordTypeId =: IARecordTypeId)];
        for(Account a: patientList){
            for(Case c: a.cases){
                if(a.Date_Approved__c == null){
                    if(c.Status.equalsIgnoreCase('Approved')){
                        system.debug('@@@@@ '+a.Date_Approved__c);
                        system.debug('@@@@@ '+patientList);
                        a.Date_Approved__c = c.Date_Approved__c;
                    }
                }
            }
            system.debug('!!!!!! '+a.First_Ship_Date__c);
            update patientList;
        }
    
        patientList = [SELECT id, Date_Approved__c,First_Ship_Date__c, (SELECT id, Status, AccountId, Date_Approved__c FROM Cases WHERE Id IN:caseId ORDER BY Date_Approved__c desc) FROM Account WHERE Id IN(SELECT Accountid FROM Case WHERE Id IN:caseId AND RecordTypeId =: TCRecordTypeId)];
        for(Account a: patientList){
            for(Case c: a.cases){
                if(a.Date_Approved__c == null){
                    if(c.Status.equalsIgnoreCase('Payable')){
                        a.Date_Approved__c = Date.today();
                    }
                }

         
            system.debug('!!!!!! '+a.First_Ship_Date__c);
        }
        
        update patientList;
    }
}
MCunninghamMCunningham
Actually - yes you can.  We creating the field update on the case worflow use the formula editor field browser to find the "Account" field.  Note that the are two one has a ">" symbol at the end - select this one and you can then browse the fields in the related Account record - browse to the SR_Date_c field and set however you need.   This is a cross-object worflow and can be used in field updates, validation rules, to show the image of a product for instance in a formula field - the possibilities are numerous.

Use the force dude - avoid coding wherever possible.

Good luck

Michael

PS: the validation rule logic would be "and(RecordtypeID=<whatever>,NOT(ISBLANK(Account.SR_date_c)))