0

I'm new in apex trigger. I want to assign owner (standard lookup field) in incident (custom object) based on selected category (lookup field) using apex trigger. API Name for object incident is BMCServiceDesk__Incident__c , category lookup field is BMCServiceDesk__FKCategory__c but i don't know API Name for owner field because it only have field name and field label. Can anyone help me to create this trigger? Thanks in advanced.

  • Why do you explicitly want to accomplish this using a trigger? Have you looked into how workflows can also handle this type of requirement? – Robin De Bondt Jun 2 '15 at 10:49
  • Yes i know that workflow can handle this but workflow has limit until 500 workflow, if i want assign 100 categories using workflow it wouldn't handle other requirement i need – Marissa Cicilia Jun 2 '15 at 14:30
2

The Owner API name is usually OwnerId.

If you use Eclipse with the Salesforce plugin, there is a handy way of seeing the API names by clicking on the "salesforce.schema" file in your project. This lists all the objects and their field API names.

  • I use BMC RemedyForce platform in salesforce, how can i seeing the API names? – Marissa Cicilia Jun 2 '15 at 14:31
  • I'm not familiar with that platform, but there are object references docs such as this one for Contact. – SineadCoyle Jun 3 '15 at 15:00
0

You can write a trigger on your custom object like :

    trigger triggername on Incident__c(before insert, before update){

    Group g = [select Id from Group where Name = 'YOURGROUPNAME' and Type = 'Queue'];
    String cat = 'Account Administration','Category A','Category B';

    for(Incident__c obj : trigger.new){
        if(cat.contains(FKCategory__c))
            obj.OwnerId = g.Id;
    }
}

Hope this may help.

  • Thanks for your help. But i can't use this code. It has error like this: Variable does not exist: BMCServiceDesk__FKCategory__c. BMCServiceDesk__FKCategory__c and owner are lookup field May you help me to solve this problem? Thanks. – Marissa Cicilia Jun 2 '15 at 14:34
  • I think you are using some package named "BMCServiceDesk". Try to remove "BMCServiceDesk__" this text before object name as well as field name. – Nebula Jun 2 '15 at 14:36
  • I using this trigger to assign owner based on selected category: But i can't using it Do you know the matter? – Marissa Cicilia Jun 2 '15 at 14:42
  • trigger AssignOwner on BMCServiceDesk__Incident__c(before insert){ Set<String> Categories= new Set<String>{'Account Administration','Category A','Category B'}; for(BMCServiceDesk__Incident__c obj : trigger.new){ List<BMCServiceDesk__Incident__c> l_cp = [SELECT Id, BMCServiceDesk__FKCategory__c FROM BMCServiceDesk__Incident__c WHERE BMCServiceDesk__FKCategory__c=:Categories]; for(BMCServiceDesk__Incident__c am_cp : l_cp) { obj.Owner = l_cp.get(obj.Owner).OwnerId; update l_cp ; } } } – Marissa Cicilia Jun 2 '15 at 14:43
  • Ok so if FKCategory__c is among those three values which you have mentioned then in this case which owner you want to assign in owner field? – Nebula Jun 2 '15 at 14:51
0

I've tried your code above, but it can't work. I try this code: Can you let me know which part i've missed?

trigger AssignOwner on BMCServiceDesk__Incident__c(before insert){ Set Categories= new Set{'Account Administration','Category A','Category B'};

Group g = [select Id from Group where Name = 'Payroll' and Type = 'Queue'];

for(BMCServiceDesk__Incident__c obj : trigger.new){
  List<BMCServiceDesk__Incident__c> l_cp = [SELECT Id, BMCServiceDesk__FKCategory__c FROM BMCServiceDesk__Incident__c WHERE BMCServiceDesk__FKCategory__c=:Categories];

for(BMCServiceDesk__Incident__c c: l_cp ) {
     if(Categories.contains(l_cp))
        obj.OwnerId = g.Id;      
     }
 }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.