You need to sign in to do that
Don't have an account?
RaoS
Trigger to populate account on Case
Hi Gurus,
I am facing an issue while creating a case.
I am creating a case based on service object with no value for Account. When I am creating the new case I would like to update the Account on Case object with the Account from the Service object.
Here is my code.
trigger UpdateAccountOnCaseForSE on Case (before insert) {
List<Id> lstcasesToUpdate=new List<Id>();
for(Case c : trigger.new)
{
If ((c.Account == NULL) && ((c.RecordTypeId =='01240000000codw') || (c.RecordTypeId =='01240000000cohe')) )
{
lstcasesToUpdate.add(c.Id);
}
}
If(lstcasesToUpdate.size()>0)
{
for (Case c1 : [Select id, Service_Entity__r.Account__c from Case where id in : lstcasesToUpdate])
{
C1.AccountId=c1.Service_Entity__r.Account__c;
}
}
}
I have no errors but the Case is not getting created saying Account is required.
Thank you for any guidance in this regard.
Rao
I am facing an issue while creating a case.
I am creating a case based on service object with no value for Account. When I am creating the new case I would like to update the Account on Case object with the Account from the Service object.
Here is my code.
trigger UpdateAccountOnCaseForSE on Case (before insert) {
List<Id> lstcasesToUpdate=new List<Id>();
for(Case c : trigger.new)
{
If ((c.Account == NULL) && ((c.RecordTypeId =='01240000000codw') || (c.RecordTypeId =='01240000000cohe')) )
{
lstcasesToUpdate.add(c.Id);
}
}
If(lstcasesToUpdate.size()>0)
{
for (Case c1 : [Select id, Service_Entity__r.Account__c from Case where id in : lstcasesToUpdate])
{
C1.AccountId=c1.Service_Entity__r.Account__c;
}
}
}
I have no errors but the Case is not getting created saying Account is required.
Thank you for any guidance in this regard.
Rao
2. Please try the below code and let me know :
trigger UpdateAccountOnCaseForSE on Case (before insert) {
Id rectypeId1 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Client Experience Case Feed').getRecordTypeId();
Id rectypeId12 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Digital Cases').getRecordTypeId();
Set<Id> serviceContst = new Set<Id>();
for(Case c : trigger.new)
{
If ((c.Account == NULL) && ((c.RecordTypeId ==rectypeId1) || (c.RecordTypeId ==rectypeId12)) && c.Service_Entity__c != null)
{
serviceContst.add(c.Service_Entity__c);
//Don't add the below debug. From the Trigger.New you can't reference the related Object Details.It will throw the null Exception.
// system.debug('c.Service_Entity__r.Account__c---:' +c.Service_Entity__r.Account__c);
}
}
Map<Id,Service_Entity__c> mapIfVal =new Map<Id,Service_Entity__c>([Select id ,Account__c from Service_Entity__c where Id IN : serviceContst]);
// If you want to prepopulate the value then you need to overide the trigger.new values like below in before insert.
// If you are using the soql for loop based on the Service_Entity__c it won't fetch the current record in before insert.
for (Case c1 : trigger.new)
{
c1.AccountId = mapIfVal.get(c1.Service_Entity__c).Account__c ;
system.debug(' @@@ Account ---:' +c1.AccountId);
}
}
Thanks,
Maharajan.C
All Answers
Thank you for your update.
But the recordtypeId that i am using in my code are specifc record types for Case object that I need to check. They are not related to Accounts.
I am checking if the account on the case is blank and the record type of Case is either of those two values. If it satisfies the condition then process the record. For the selected record, get the account from the Service object that is listed on the case and then assign that account to the Case.
Let me know.
Thanks
Rao
Don't hardcode the any Id's in Trigger --> use the Record Type Name to get the Id with the help of RecordTypeInfo Class
trigger UpdateAccountOnCaseForSE on Case (before insert) {
Id rectypeId1 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('RECORDTYPE1').getRecordTypeId();
Id rectypeId12 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('RECORDTYPE2').getRecordTypeId();
Set<Id> serviceContst = new Set<Id>();
for(Case c : trigger.new)
{
If ((c.Account == NULL) && ((c.RecordTypeId ==rectypeId1) || (c.RecordTypeId ==rectypeId12)) && c.Service_Entity__c != null)
{
serviceContst.add(c.Service_Entity__c);
}
}
Map<Id,Service_Entity__c> mapIfVal =new Map<Id,Service_Entity__c>([Select id ,Account__c from Service_Entity__c where Id IN : serviceContst ])
for (Case c1 : trigger.new)
{
if(mapIfVal.containsKey(c1.Service_Entity__c))
{
C1.AccountId=mapIfVal.get(c1.Service_Entity__c).Account__c;
}
}
}
Thanks,
Maharajan.C
Let me check
I don't have any errors but I am getting the following error when I am creating a case without an Account
"You must assign an Account to the Case"
Thanks.
When I have debuged the code I am getting null for c.Service_Entity__r.Account__c at line 11.
Thanks.
Raj,
Whats the best # to reach you?
Thanks
I am getting the id of service entity on line 11. But line 17 is not getting any rows. Also I think lstcasesToUpdate on line 17 should be updated to serviceContst as we not capturing any list of cases to update. I changed it but still the for loop is not getting executed.
Thanks
I have modified the code to the below
Id rectypeId1 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Client Experience Case Feed').getRecordTypeId();
Id rectypeId12 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Digital Cases').getRecordTypeId();
Set<Id> serviceContst = new Set<Id>();
system.debug('rectypeId1....' + rectypeId1);
for(Case c : trigger.new)
{system.debug('..c.RecordTypeId.' + c.RecordTypeId);
If ((c.Account == NULL) && ((c.RecordTypeId ==rectypeId1) || (c.RecordTypeId ==rectypeId12)) )
{
//serviceContst.add(c.Service_Entity__r.Account__c);
serviceContst.add(c.Service_Entity__c);
system.debug('c.Service_Entity__r.Account__c---:' +c.Service_Entity__r.Account__c);
}
}
system.debug('serviceContst..'+ serviceContst);
Map<Id,Service_Entity__c> mapIfVal =new Map<Id,Service_Entity__c>([Select id ,Account__c from Service_Entity__c where Id IN : serviceContst]);
system.debug('-----mapIfVal---' + mapIfVal);
for (Case c1 : [Select id, Service_Entity__r.Account__c,Service_Entity__r.Id from Case where Service_Entity__r.Id in : serviceContst])
{
Service_Entity__c ent = mapIfVal.get(c1.Service_Entity__r.Account__c) ;
system.debug('ent.Account__c---:' +ent.Account__c);
C1.AccountId=ent.Account__c;
}
}
Here is the error I am getting
16:17:54.0 (39178645)|SOQL_EXECUTE_BEGIN|[37]|Aggregations:0|SELECT id, Service_Entity__r.Account__c, Service_Entity__r.Id FROM Case 16:17:54.0 (102820734)|SOQL_EXECUTE_END|[37]|Rows:253 16:17:54.0 (103434906)|EXCEPTION_THROWN|[41]|System.NullPointerException: Attempt to de-reference a null object 16:17:54.0 (103574885)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object
2. Please try the below code and let me know :
trigger UpdateAccountOnCaseForSE on Case (before insert) {
Id rectypeId1 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Client Experience Case Feed').getRecordTypeId();
Id rectypeId12 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Digital Cases').getRecordTypeId();
Set<Id> serviceContst = new Set<Id>();
for(Case c : trigger.new)
{
If ((c.Account == NULL) && ((c.RecordTypeId ==rectypeId1) || (c.RecordTypeId ==rectypeId12)) && c.Service_Entity__c != null)
{
serviceContst.add(c.Service_Entity__c);
//Don't add the below debug. From the Trigger.New you can't reference the related Object Details.It will throw the null Exception.
// system.debug('c.Service_Entity__r.Account__c---:' +c.Service_Entity__r.Account__c);
}
}
Map<Id,Service_Entity__c> mapIfVal =new Map<Id,Service_Entity__c>([Select id ,Account__c from Service_Entity__c where Id IN : serviceContst]);
// If you want to prepopulate the value then you need to overide the trigger.new values like below in before insert.
// If you are using the soql for loop based on the Service_Entity__c it won't fetch the current record in before insert.
for (Case c1 : trigger.new)
{
c1.AccountId = mapIfVal.get(c1.Service_Entity__c).Account__c ;
system.debug(' @@@ Account ---:' +c1.AccountId);
}
}
Thanks,
Maharajan.C
Thank you for pointing out the last mistake that i was doing..having soql in the for loop. That fixed the issue.
Thank you Raj for the code.
Its working now.
The trigger is working fine for first record type but is not working for the second record type.
Do i need check any other settings to make sure it works for the second record type also?
Thanks.