You need to sign in to do that
Don't have an account?
Ken 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.
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.
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
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
@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
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;
}
}
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)))