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
Suneel B 9Suneel B 9 

I am trying ti change ownership field to public when type is prospect in the account field using trigger, below is my program can you please help me out:

Hai all,
I am trying ti change ownership field to public when type is prospect in the account fiels using trigger, below is my program can you please help me out:

trigger oppinsert on Account (After insert) 

{
   set<id> accid = new set<id>();      
   List<Account> acc = Trigger.new;  
    
    for(Account accset1:acc)
    {
    if( accset1.Type == 'Prospect'){
     // accset1.Ownership = 'Public';
      
      accset1.Ownership.addError('Please select');
      
   }

 } 
    
    for(Account accset:acc)        
    {
       accid.add(accset.id);       
    }
    
    List<Account> acclst = [Select name,id from Account where id In:accid];  
    
   
 

    
    List <Opportunity> opp = new List<Opportunity>();     
    
          for (Account ac:acclst)                           
    {
     Opportunity o =new Opportunity();                
     o.Name = ac.Name;
     o.CloseDate = system.today();
     o.StageName = 'prospecting';
     o.Accountid = ac.id;
     opp.add(o);   
     }
    insert opp;
}
bob_buzzardbob_buzzard
You won't be able to do this in an after update trigger as the records in the trigger are read only by then - instead you'll have to use a before update and you can then change the field as you like.

acc.Ownership won't work as that is the format for a standard field - instead it would be acc.Ownership__c or similar.

What specific problems are you encountering?
Suneel B 9Suneel B 9
hey thanks for your reply i got the solution..
I created a new trigger.

trigger acc1 on Account (before insert, before update) {

for(Account a:Trigger.new)
{
if (a.Type =='Prospect')

   a.Ownership = 'Public';
  }
else{
        a.Ownership = null;
}
   }

}

This is it.. 
Anyways thanks .