function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SamuliSamuli 

How to let SF create case automatically when certain criteria is fulfilled?

I would like SF to create a new case automatically when another one with certain conditions has been closed.

- Create a new case with custom case type and assign it to a queue.

- Closed case has to fill certain criterias: case type, some field values must match criteria, and so on..

 

How can this be done? I'm not able to do very complicated programming. Do you have any pointers on how to proceed with this task?

Best Answer chosen by Admin (Salesforce Developers) 
zen_njzen_nj

The way to have case created automatically when certain criteria is fulfilled would be to use trigger.

In your trigger code, you can also specify the custom case record type.

 

In terms of assigning the new case (with custom case type) to a queue, you can use workflow rules for that 

and essentially set up a field update rule for the workflow so that it will change case ownership to a specific queue.

 

In terms of making sure you can only close out cases when certain criterias are met, you would use validation rules

to do that.

 

The workflow rule and validation rules are pretty easy to do but the trigger code will require that you have a sandbox or developer environment so that you can write/develop your trigger code (and create apex class for proper testmethod to ensure code coverage).

All Answers

zen_njzen_nj

The way to have case created automatically when certain criteria is fulfilled would be to use trigger.

In your trigger code, you can also specify the custom case record type.

 

In terms of assigning the new case (with custom case type) to a queue, you can use workflow rules for that 

and essentially set up a field update rule for the workflow so that it will change case ownership to a specific queue.

 

In terms of making sure you can only close out cases when certain criterias are met, you would use validation rules

to do that.

 

The workflow rule and validation rules are pretty easy to do but the trigger code will require that you have a sandbox or developer environment so that you can write/develop your trigger code (and create apex class for proper testmethod to ensure code coverage).

This was selected as the best answer
SamuliSamuli

OK, thanks. I'll start reading about triggers then. We have Enterprise edition so that should be possible. Sounds like not the most complicated programming task.

cafe2cafe2

Did you ever find instructions about how to create a case with a trigger? I've been looking around developer force and haven't been able to find a solution. Can you point me in the right direction?

 

Thanks!

zen_njzen_nj

The actual code for creating the case with a trigger is pretty straight forward, it's the test method that you have to write to successfully deploy it that takes a bit more work.

 

 

But in terms of just writing the trigger code, below is one I had written for the following situation:

 

We created a custom picklist field in Case called Cleanup_ByOperations and if a certain value is selected ("Yes - Need to Create Child Case for Operations Team"), then we want to automatically have a "Child Case" created as well. The Child Case would inherit all the values from the original case. We also would only have this trigger fire if this Case is itself not a Child Case (i.e. it does not have a Parent Case associated with it - ParentId value is null)

 

 

trigger AutoCreateSubCase on Case (after insert, after update) {
    List<Case> listCases = new List<Case>();
    for(Case c: Trigger.new) {
        // create the children cases only for cases that are not 
        // children cases themselves 
        if( (c.ParentId == null) && (Trigger.new[0].CleanUp_By_Operations__c == 'Yes - Need to Create Child Case for Operations Team') && (Trigger.isInsert || (Trigger.old[0].CleanUp_By_Operations__c != 'Yes - Need to Create Child Case for Operations Team'))) {
            Case newChildCase = c.clone(false);
            newChildCase.ParentId = c.ID;
            listCases.add(newChildCase);
        }

    }
    insert listCases;
}