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
SuchitraSuchitra 

Apex trigger to change the contact owner after insert

Hi ,
 
I am trying to change the contact owner of the contact on insert and I am directed to the page saying insufficient permission.
The profile has a limites access on contact that is only read edit and create. Change the Owner option is available in the UI but unable to control on the edit page
 
Here is the code
 
trigger UpdateCOwner on Contact (before insert, after insert)
{
//Query for the ARS Account Leader of the contact's Account 
//Set the contact ARS Owner value to Leader value obtained 
//Insert Record 
Contact c = new Contact();
Map<String, Contact> contactMap = new Map<String, Contact>();
for (Contact contact : System.Trigger.old) 
{
if(Trigger.isBefore)
{

if (System.Trigger.isInsert)
{
string profileName = [select profile.name from user where id = :contact.OwnerId][0].profile.name;
if(profileName.indexOf('Call Center')>-1)
{
Account acnt = [select ARSaccountleader_lookup__c,ACWaccountleader_lookup__c from Account where Id=:contact.AccountId];
string acntLeader = [select ARSaccountleader_lookup__c from Account where Id=:contact.AccountId][0].ARSaccountleader_lookup__c;
//string acntLeader = [select ARSaccountleader_lookup__c from Account where Id=:contact.AccountId][0].ARSaccountleader_lookup__c;
//string test = acnt.ACWaccountleader_lookup__c;
contact.Created_by_Call_Center__c = true;
if(acnt.ARSaccountleader_lookup__c != null)
{
contact.OwnerId = acnt.ARSaccountleader_lookup__c;
}
else if (acnt.ACWaccountleader_lookup__c != null)
{
contact.OwnerId = acnt.ACWaccountleader_lookup__c;
}
contactMap.put(contact.OwnerId,contact);
//c = contact;

}
else contactMap.put(contact.Id,contact);
}
}

}
}
 
Can some one help with finding how this can be handled.A use with limited profile cannot create a contact with the differnt owner and this causing the problem.
Is there a way we can update the record after insert
 
Thanks in advance.
 
Thanks & Regards,
Suchitra


Message Edited by Suchitra on 03-07-2008 08:34 AM
A_SmithA_Smith
Hi Suchitra,

After changing to a new owner, the contact's creator by default no longer has access to the contact.  You need to provide this user access by one of the following methods:
  • Change the organization sharing model on Account/Contact to Read Only
  • Write a sharing rule that grants the contact creator access
  • Add a manual share to the contact after the owner change is complete
Thanks,
Andrew
SuchitraSuchitra

Hi Andrew,

Thanks a lot, The user already has read write access on the contacts of the account that is manual share.

Thanks & Regards,

P Suchitra

SuchitraSuchitra
Thanks I was able to fix this on my own
The owner couldnot be changed becuase a standard user will not be able to create a record with a different owner. So the trigger will throw an insufficient permission error on the isBefore section. Again when we try to update the record in the isAfter section there will agian be a read only access on the record so the option is to query the record and assign it to a new instance of SObject and then try to update on the after insert event this will work.
Samina DiamondSamina Diamond
Thanks for sharing this solution!
RKGRKG
Try this one

if(acnt.ARSaccountleader_lookup__c != null)
{
trigger.newMap().get(contact.id).OwnerId = acnt.ARSaccountleader_lookup__c;
}
else if (acnt.ACWaccountleader_lookup__c != null)
{
contact.OwnerId = acnt.ACWaccountleader_lookup__c;
}

becasue as you are using System.Trigger.old are read only