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
Sandilya kumarSandilya kumar 

WorkOrder's owner should assign to a user who is specific to the Region based on the country field.

Create a custom object, Work Order object. It should have a country field. When we are creating a new record for WorkOrder, The WorkOrder's owner should assign to a user who is specific to the Region based on the country field. 

Create two custom objects( Region__c ,  Region_Country__c). Region object should have User lookup. Region_Country__c should have all the countries list with Regions.
trigger WorkOrders on WorkOrder (after insert) {
    set<Country__c> con =new set<Country__c>();
    list<Workorder__C>worklist=new list<Workorder__C>();
    list<Workorder__C>work_list=new list<Workorder__C>();
    
    list<Region__c>Reglist=new list<Region__c>();
   
    if(trigger.isinsert)                              
    {
        for(Region__c R:trigger.new)
        {
            Reglist = [select User__c from Workorder__C ];
        }
    }
    if (trigger.isdelete)                         
    {
        for(contact con:trigger.old)
        {
            accid.add(con.accountid);
        }
    }
    acclist=[SELECT id,name FROM account WHERE id in:accid];
    contactlist=[SELECT id,name,accountid FROM contact WHERE accountid in:accid];
 

}

Since am new to developing am trying to solve the use case,please can anyone help me .
thanks in advance.
Best Answer chosen by Sandilya kumar
Raj VakatiRaj Vakati
try this code
 
trigger WorkOrders on WorkOrder (before insert) {
	
	Set<String> str = new Set<String>() ; 
	
	for(WorkOrder w : trigger.new){
		str.add(w.country__c) ;
	}
	
	List<Region_Country__c> regCoun = [Select Name , Id ,User__c  from Region_Country__c where Name IN  : str]; 
	
	Map<String , Region_Country__c> mapOf = new Map<String , Region_Country__c>() ; 
	
	
	for(Region_Country__c r : regCoun){
		mapOf.put(r.Name , r) ; 
	}
	
	
	for(WorkOrder w : trigger.new){
	Region_Country__c c = mapOf.get(w.country__c) ; 
	w.OwnerId =c.User__c;
	}
	
   

}