+ Start a Discussion
azlaneazlane 

Need help creating trigger on the case object

I am new to creating triggers and was wondering if someone could help me create the following triggers...

 

When a case is assigned to a specific user (meaning it is owned by a queue) and the status is New, Review or Queue, then change Status to Assigned and update Engineer field to the Case Owner.

 

I was told this could be done with a workflow for every Engineer value... very time consuming.

----

 

Also, when the case Status is updated to Reassigned, update the case owner to the Engineer value.

 

More information may be needed so just let me know. 

 

Thanks

 

 

Best Answer chosen by Admin (Salesforce Developers) 
samarsamar

Thry the following code. Hope this ll fulfil your requirement

 

trigger AssignEngineer on Case (before update) {
    for(Case c : Trigger.New){
        if(c.Owner.Type=='User' && (c.status == 'New' || c.status == 'Review' || c.status == 'Queue')){
            c.Engineer__c = c.OwnerId;
            c.Status = 'Assigned';    
        }
    }
}

 If you try this please let me know does it work or not.

 

Regards,

Samarjit

 

All Answers

sravusravu

Can you list your fields and their datatypes. And could you elaborate your first trigger.

b-Forceb-Force

provide field details and buzziness need in more detail manner

thanks,

Bala

azlaneazlane

Thanks for the reply. 

 

This is will be used for internal cases.  So basically when an Engineer changes ownership from a queue to their user name and set the status to New, Review or Queue, we need  sfdc to update the Engineer field to populate the value of the case owner and update the status to Assigned.  I can live without the Assigned status if needed.   I hope these helps.

 

The fields involved are...

 

Field Label: Case Owner

Field Name: Owner

Data Type: Lookup(User,Queue)

 

Field Label:Engineer

API Name: Engineer__c

Data Type: Lookup(User)

 

Field Label: Status

Field Name: Status

Data Type: Picklist

-value needs to be either

 

 

sravusravu

Hi,

 

Try the following code and see if it is working according to your requirement

 

trigger CaseUpdateTrigger on Case (before update) {
    for(Case c : Trigger.New){
        if(c.OwnerId == userinfo.getuserId()&& c.status == 'New'){
            c.Engineer__c = c.OwnerId;
            c.Status = 'Assigned';    
        }
        if(c.Status=='Assigned'){
            c.ownerId = c.Engineer__c;
        }
    }
}

 

Let me know if anything goes wrong or doesn't work.

samarsamar

Thry the following code. Hope this ll fulfil your requirement

 

trigger AssignEngineer on Case (before update) {
    for(Case c : Trigger.New){
        if(c.Owner.Type=='User' && (c.status == 'New' || c.status == 'Review' || c.status == 'Queue')){
            c.Engineer__c = c.OwnerId;
            c.Status = 'Assigned';    
        }
    }
}

 If you try this please let me know does it work or not.

 

Regards,

Samarjit

 

This was selected as the best answer
azlaneazlane

I apologize for the time it took to reply, I was out of the office.  I ended up using the following and it works...

 

 

trigger AssignEngineer on Case (before update) { 

    for(Case c : Trigger.New){ 
        if(c.recordTypeId=='012700000005ifP' && (c.status == 'New' || c.status == 'Review' || c.status == 'Queue')){ 
        c.Engineer__c = c.OwnerId; 
        c.Status = 'Assigned';     
        }  
   }       
}

 

 

Now, I am working on creating a test class for it.  Any ideas would be greatly appreciated. 

sravusravu

To test the trigger write a test class

 

@isTest

private classname{

    static testMethod void method(){

}

 

}

 

In this class add the test method and insert one case update it so that it will fire that trigger

 

Hope this helps you.