+ Start a Discussion
LudivineLudivine 

Trigger to automatic copy picklist values

Hello !

 

I have begun a trigger to copy on the Actions page (Opportunity page) ,the picklist values of 3 fields from the Account related list

 

But I meet troubles to do the mapping.

 

I can't map the AccountId field from Oppty(Actions), I have the following error :

 

Error: Compile Error: Illegal assignment from Schema.SObjectField to String at line 30 column 10 

 

Here here my code... I have done my best using other existing triggers in my org but it is probably wrong...

 

Trigger UpdateIndustrySegmentSubSegment on Opportunity (Before insert,Before Update) 
{
    // Initialise ID maps for related sObjects
    map<Id, Account> mapAccount   = new map<Id, Account>();
    
    // Fill the Id -- > sObject maps for Accounts
  for(Account[] arrAccount: [
                                    select  Id,
                                            Name,industry__c,segment__c,sub_segment__c
                                    from    Account
                                   where   id in :mapAccount.keySet()
                                ])
   {
        for(Account sAccount : arrAccount)
        {
            mapAccount.put(sAccount.Id, sAccount);
        }
    }
    
   for(Opportunity sOppty : trigger.new)
    {
        
        Account  thisAccount   = mapAccount.get(sOppty.Account.Id);
 
        if(
        sOppty.Business_group__c == 'Amcor Flexibles Europe & Americas' && 
        //thisAccount.industry__c != null && 
        sOppty.RecordTypeId != '012200000002HdgAAE')
        
        {sOppty.industry__c = Account.industry__c;
        sOppty.segment__c = Account.segment__c;
        sOppty.sub_segment__c = Account.sub_segment__c;}
  
    }
}

 Thanks a lot for your help

Best Answer chosen by Ludivine
LudivineLudivine

Hi all,

 

For those who are working night and day like me on these kind of simple isuues , I am glad to write below a nice present e.g Ze Solution :)

 

trigger UpdateOpptyPicklists on Opportunity(Before Insert, Before Update) { 
    for(Opportunity Oppty: Trigger.new){
     Account acct = [select id,industry__c,segment__c,sub_segment__c from Account where Id=:oppty.AccountId];
       if(Oppty.Field1__c =='NameXX'
           && Oppty.RecordTypeId != '010000000002HXXXxT'){
        
          Oppty.industry__c= acct.industry__c;
          Oppty.segment__c = acct.segment__c;
          Oppty.sub_segment__c = acct.sub_segment__c;
       }
   }
}

 

All Answers

JeffStevensJeffStevens

Try wrapping your three assignments at the bottom in a string.valueOf()

LudivineLudivine

Thanks for your reply, I have made the changes.

 

Do you know how I could map the AccountId field from opportunity to Account.ID related list in my trigger because in my code it is not good :(

 

Many thanks

LudivineLudivine

Hi all,

 

For those who are working night and day like me on these kind of simple isuues , I am glad to write below a nice present e.g Ze Solution :)

 

trigger UpdateOpptyPicklists on Opportunity(Before Insert, Before Update) { 
    for(Opportunity Oppty: Trigger.new){
     Account acct = [select id,industry__c,segment__c,sub_segment__c from Account where Id=:oppty.AccountId];
       if(Oppty.Field1__c =='NameXX'
           && Oppty.RecordTypeId != '010000000002HXXXxT'){
        
          Oppty.industry__c= acct.industry__c;
          Oppty.segment__c = acct.segment__c;
          Oppty.sub_segment__c = acct.sub_segment__c;
       }
   }
}

 

This was selected as the best answer