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
Soujanya Uppal 9Soujanya Uppal 9 

change contact owner on new record through trigger when industry = agriculture

Hi everyone!!

How to do with trigger?

Change contact owner on new record through trigger when industry = agriculture.
Amlan PatelAmlan Patel
Hey Soujanya,

As you state that you want to change the owner field of contact records, I assume that the records are already inserted.
So in this case
  • Create a trigger on the Contact object for after insert
  • Inside the trigger, write logic to find all inserted records ( basically Trigger.newMap )
  • Filter out records having industry = agriculture.
  • Change the owner based on your business requirement ( or possibly with a helo of custom setting)
Hope this helps
 
KMNKMN
Hi, Soujanya
This Code is fit for your requirement try this once
Here alias name 'sthri' is in my user list you can write your user name whose you want assign that record..
 
trigger userForAccount on Contact (before insert) {
    User u = [SELECT Id, Name FROM User WHERE alias = 'sthri'];
       for(Contact con : Trigger.new){  
             if(industry == agriculture){
               con.ownerId = u.id;
           }
       }
}
vaani krvaani kr
Hi,Soujanya

who created the record. OwnerId : ID of the User who has been assigned to work this record. If you update this field, the previous owner's access becomes Read Only or the access specified in your organization-wide,createdby is nothing but ownerid

trigger contacrTrigger on Contact (afetr insert) {
    User u = [SELECT Id, Name FROM User WHERE Name= 'padma'];
    list<contact> lstcon = [SELECT Id, Name FROM contact WHERE Id in:Trigger.new'];
       for(Contact con : lstcon ){  
             if(con.industry == 'agriculture'){
               con.createdbyId = u.id;
           }//end if
       }//end for
}// end Trigger


Hope This helpful

Thanks Records

Vaani