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
RajKumaR MRajKumaR M 

Update owner field based on user field in my custom object via work flow...

We have two custom objects. These objects have master details relationship that's why the owner of master  record is owner of child record.

 

In master we have a user look up field(Teaacher Name),based on this filed the owner field will need to update.Now the current user created by id is assigned to Record owner by default.

 

For example if we have 'John Smith' as 'Teacher name' ,we need to update owner field as 'John Smith'.WE HAVE MORE THAN 20 Teachers in our org.Work flow field update don't have the formula to do this in single work flow.

 

Is there is any alternative solution to do this Except Apex Trigger.

Thanks in Advance.

Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9

Hi....Here the trigger code that you may want to get started with, put the right master custom object name where ever I've used custom_object__c.

 

trigger customMasterObjectTrigger on custom_object__c(before insert, before update){

    set<id> contactIds = new Set<Id>();
    for(custom_object__c obj : trigger.new){

            contactIds.add(obj.Contact__c);
    }
    List<user> users = [Select id, ContactId from user where ContactId IN  :contactIds]
    Map<id, id> contactToUserMap = new Map<id, id>();
    for(user u : users)
    {
        contactToUserMap.put(u.ContactId, u.id);
    }

    for(custom_object__c obj : trigger.new){
        obj.OwnerId = contactToUserMap.get(obj.contactId);
    }
}

 

All Answers

magicforce9magicforce9

Hi Raj,

 

There is a way you can achive this with out any code. Lets see if my approach suits you(Others can advise if there is a better approach to this). Also this can be achived using a trigger, so let me know if you want to use a trigger and I can provide a sample code for it.

 

Steps:

 

1. Create a PiskList on the master custom object with the name of all Teachers in your org.

2. Create a Workflow on master custom object and and the entry criteria should be: Picklist Value = Teacher 1

3. Add a field update action and in that select the Owner field and then from look'up select Teacher 1(user record)

4. Save and activate the workflow.

 

You need to follow the same steps and create 20+ workflow rules each per Teacher(user) 

 

You can also watch this video for step by step : http://screencast.com/t/4Ub0j8s4k

 

Note: By following the above steps you'll end 'up creating 20+ workflows each for a specific teacher but if the number of Teachers grows then you might hit the limit to 50 active workflow rules per object for enterprise edition - (Limits apply to any combination of active workflow, assignment, auto-response, and escalation rules). You can in total have 300 per object but only 50 active at a time per object.

RajKumaR MRajKumaR M
Thanks for quick and detailed reply on this Mohammed ,

I stick with trigger but the new requirement is ,



In master we have a contact look up field(Teacher Name),This contact look up (Teachers Name) should compare with user records and if users found based on this user id ,Record owner field will need to update.Now the current user created by id is assigned to Record owner by default.

magicforce9magicforce9

Hi....Here the trigger code that you may want to get started with, put the right master custom object name where ever I've used custom_object__c.

 

trigger customMasterObjectTrigger on custom_object__c(before insert, before update){

    set<id> contactIds = new Set<Id>();
    for(custom_object__c obj : trigger.new){

            contactIds.add(obj.Contact__c);
    }
    List<user> users = [Select id, ContactId from user where ContactId IN  :contactIds]
    Map<id, id> contactToUserMap = new Map<id, id>();
    for(user u : users)
    {
        contactToUserMap.put(u.ContactId, u.id);
    }

    for(custom_object__c obj : trigger.new){
        obj.OwnerId = contactToUserMap.get(obj.contactId);
    }
}

 

This was selected as the best answer
Groundwire ConsultingGroundwire Consulting

Hi Rajkumar,

    

we can go through the workflow rule option on the  field update action ,when you update the field  in custom object automatically owner  field will be changed.

 

and if you have any more questions please feel to contact me on support@groundwireconsulting.com

RajKumaR MRajKumaR M
Thank you very much Mohammed.
RajKumaR MRajKumaR M

Thanks for your advise on this Groundwire Consulting. But in my scnario i 'm not able to update Owner Field based on cretiea ( formula).If its text field we can have a formula on field update but in look up we don't have a formula.

 

Its still in Ideas -https://success.salesforce.com/ideaView?id=08730000000BrGdAAK

 

Finaly i fixed this by Apex Trigger.