+ Start a Discussion
Pedro Garcia GPedro Garcia G 

Case conditional status

Hi...

Our student service department processes the student request through cases.
There are some requests that a payment is required. So, we associate Case with Order. One case can have one or more than one Orders.

REQUIREMENT:
The Case can only be closed when the Order(s) status is "Activated" 

QUESTION:
How can we aware of the user when she/he tries to close the Case that the Order(s) is not Activated yet?

We're thinking to develop the Case validation in the Trigger event (before update). How could we launch a pop up from the Trigger?

Thanks,
Pedro
 
Best Answer chosen by Pedro Garcia G
TylerBrooksTylerBrooks
trigger Alerttest on Case (before update) 
{
    LIST<Order_Object__c> AllOrders = [SELECT ID, NAME FROM Order_Object__c WHERE OrderCaseId IN :Trigger.New];//Get all orders
    List<Order_Object__c> ActivatedOrders = [SELECT ID, NAME FROM Order_Object__c WHERE OrderCaseId IN :Trigger.New, Order_Status__c = 'Activated'];//Get activated orders
    for(Case myCase : trigger.new)
    {
        if(AllOrders.Size() > 0)//Make sure there are orders related to case
        {
            if(ActivatedOrders.Size() != AllOrders.Size())//Determine if All related orders are activated
            {
                if(myCase.Status == 'Closed')//If case is closed
                {
                    myCase.addError('Please Wait until All Orders are Activated before closing the case.');// Throw error
                }
            }
        }
    }
//You'll need to add your own Object name and fields into the list and SOQL query
}

Forgot to add the colons for the IN :Trigger.New portion of the code so it probably would have been erroring out.

All Answers

TylerBrooksTylerBrooks
Pedro,

I don't have your object names so this is just spitballing but you could do something like this
trigger Alerttest on Case (before update) 
{
    LIST<Order_Object__c> AllOrders = [SELECT ID, NAME FROM Order_Object__c WHERE OrderCaseId IN Trigger.New];//Get all orders
    List<Order_Object__c> ActivatedOrders = [SELECT ID, NAME FROM Order_Object__c WHERE OrderCaseId IN Trigger.New, Order_Status__c = 'Activated'];//Get activated orders
    for(Case myCase : trigger.new)
    {
        if(AllOrders.Size() > 0)//Make sure there are orders related to case
        {
            if(ActivatedOrders.Size() != AllOrders.Size())//Determine if All related orders are activated
            {
                if(myCase.Status == 'Closed')//If case is closed
                {
                    myCase.addError('Please Wait until All Orders are Activated before closing the case.');// Throw error
                }
            }
        }
    }
//You'll need to add your own Object name and fields into the list and SOQL query
}

If this helps please mark as best answer to help keep the community clean!
Regards,
Tyler
TylerBrooksTylerBrooks
trigger Alerttest on Case (before update) 
{
    LIST<Order_Object__c> AllOrders = [SELECT ID, NAME FROM Order_Object__c WHERE OrderCaseId IN :Trigger.New];//Get all orders
    List<Order_Object__c> ActivatedOrders = [SELECT ID, NAME FROM Order_Object__c WHERE OrderCaseId IN :Trigger.New, Order_Status__c = 'Activated'];//Get activated orders
    for(Case myCase : trigger.new)
    {
        if(AllOrders.Size() > 0)//Make sure there are orders related to case
        {
            if(ActivatedOrders.Size() != AllOrders.Size())//Determine if All related orders are activated
            {
                if(myCase.Status == 'Closed')//If case is closed
                {
                    myCase.addError('Please Wait until All Orders are Activated before closing the case.');// Throw error
                }
            }
        }
    }
//You'll need to add your own Object name and fields into the list and SOQL query
}

Forgot to add the colons for the IN :Trigger.New portion of the code so it probably would have been erroring out.
This was selected as the best answer