+ Start a Discussion
LudivineLudivine 

ActiveTrigger doesn't make any shifts in Opportunity Object

Hello,

 

I have made a trigger to update records from account to Opportunity but it doesn't work.

I think there is a problem in the mapping... I have looked into salesforce Force.com Apex code Developer Guide but I make no success..

Here is my trigger, hope someone can tell me how to correct it.. thanks

 

trigger UpdateAccountToOppty on Account (Before Insert, Before Update) {

    List<Account> accountWithOpptys = [SELECT id,Industry__c,Segment__c,Sub_Segment__c, (select id,accountId,RecordTypeId, Industry__c,Segment__c,Sub_Segment__c 
         from Opportunities  where accountId IN :Trigger.newMap.keySet() 
         AND Business_group__c='Amcor Flexibles Europe & Americas'
         ) 
         from Account where Id IN :Trigger.newMap.keySet()];
    
    //Loop through Accounts only once
    for(Account a : accountWithOpptys){
        
         //Loop through related Opportunities only once
         for(Opportunity o: a.Opportunities){
            if(o.RecordTypeId != '012200000002HdgAAE'){
                o.industry__c = a.industry__c;
                o.segment__c = a.segment__c;
                o.sub_segment__c = a.sub_segment__c;
            }
         }
    
   }

}

 

Best Answer chosen by Ludivine
HariDineshHariDinesh

Hi,

 

As per your words,

There is trigger, but it is not working as expected (not updating something rit?)

To debug this use System.debug statement in the trigger to know whether the data is coming or not properly and

The functionality is working as expected or not.

To do this I can suggest how to use debug logs

For example

 

List<account> lstacc = [select id, name from account limit 10 ];

 Here you will expect that the list contain 10 records.

But to confirm this you can follow this

System.debug(‘@@@@’+ lstacc.size());

This will give the size of the list at run time.

From above you can understand that System.debug will give the values of runtime and which will be available in debug logs.

 

Debug logs is for debugging something.

If you execute some code at back end the values will be passed at run time rit. To know the values of run time Debug Logs will be useful.

First you need to Enable debug logs for your user

Setup -> Monitoring -> debug Logs

Click new and select your username and click save and now debug logs for your user is activated.

Now execute the trigger by updating some record like that and refresh the debug logs.

Now fresh logs will be available for you.

 

Now come to your trigger place the Debug logs like below

trigger UpdateAccountToOppty on Account (Before Insert, Before Update) {

    system.debug('@@@Trigger.newMap.keySet()'+Trigger.newMap.keySet())
    List<Account> accountWithOpptys = [SELECT id,Industry__c,Segment__c,Sub_Segment__c, (select id,accountId,RecordTypeId, Industry__c,Segment__c,Sub_Segment__c 
         from Opportunities  where accountId IN :Trigger.newMap.keySet() 
         AND Business_group__c='Amcor Flexibles Europe & Americas'
         ) 
         from Account where Id IN :Trigger.newMap.keySet()];
    system.debug('1@@@accountWithOpptys'+accountWithOpptys)
	system.debug('2@@@accountWithOpptys size'+accountWithOpptys.size())
    //Loop through Accounts only once
    for(Account a : accountWithOpptys)
	{
        //Loop through related Opportunities only once
         for(Opportunity o: a.Opportunities){
            if(o.RecordTypeId != '012200000002HdgAAE'){
                o.industry__c = a.industry__c;
                o.segment__c = a.segment__c;
                o.sub_segment__c = a.sub_segment__c;
            }
         }
    
   }

}

 

By placing the Debug's like above you can find the problem.

 

All Answers

HariDineshHariDinesh

Hi,

 

You can use debug Logs to solve your problem by yourself.

Place System.debug();  statement everywhere and try to find where the data is coming or not.

Here Debug logs is the best way for this as per my idea.

LudivineLudivine

Hi HariDinesh,

 

Sorry for my lack of knowledge but I am willing to learn! Could you tell me how I can acess to the Debug Log?

Must be very useful to have this but I have tried to paste my trigger in the Developer Console and it seems not to accept any trigger syntaxe..

 

In the meanwhile, I have find the Solution to my issue:

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 =='Text' 
           && Oppty.Field2 != '000012143UYT'){

        //  System.debug('I would like to see My selection acct');

          Oppty.industry__c= acct.industry__c;
          Oppty.segment__c = acct.segment__c;
          Oppty.sub_segment__c = acct.sub_segment__c;

       }

   }
} 

 And now I would like to know where I could test this with System.Debug and see My selection acct.

 

Thanks a lot for your help.

 

HariDineshHariDinesh

Hi,

 

As per your words,

There is trigger, but it is not working as expected (not updating something rit?)

To debug this use System.debug statement in the trigger to know whether the data is coming or not properly and

The functionality is working as expected or not.

To do this I can suggest how to use debug logs

For example

 

List<account> lstacc = [select id, name from account limit 10 ];

 Here you will expect that the list contain 10 records.

But to confirm this you can follow this

System.debug(‘@@@@’+ lstacc.size());

This will give the size of the list at run time.

From above you can understand that System.debug will give the values of runtime and which will be available in debug logs.

 

Debug logs is for debugging something.

If you execute some code at back end the values will be passed at run time rit. To know the values of run time Debug Logs will be useful.

First you need to Enable debug logs for your user

Setup -> Monitoring -> debug Logs

Click new and select your username and click save and now debug logs for your user is activated.

Now execute the trigger by updating some record like that and refresh the debug logs.

Now fresh logs will be available for you.

 

Now come to your trigger place the Debug logs like below

trigger UpdateAccountToOppty on Account (Before Insert, Before Update) {

    system.debug('@@@Trigger.newMap.keySet()'+Trigger.newMap.keySet())
    List<Account> accountWithOpptys = [SELECT id,Industry__c,Segment__c,Sub_Segment__c, (select id,accountId,RecordTypeId, Industry__c,Segment__c,Sub_Segment__c 
         from Opportunities  where accountId IN :Trigger.newMap.keySet() 
         AND Business_group__c='Amcor Flexibles Europe & Americas'
         ) 
         from Account where Id IN :Trigger.newMap.keySet()];
    system.debug('1@@@accountWithOpptys'+accountWithOpptys)
	system.debug('2@@@accountWithOpptys size'+accountWithOpptys.size())
    //Loop through Accounts only once
    for(Account a : accountWithOpptys)
	{
        //Loop through related Opportunities only once
         for(Opportunity o: a.Opportunities){
            if(o.RecordTypeId != '012200000002HdgAAE'){
                o.industry__c = a.industry__c;
                o.segment__c = a.segment__c;
                o.sub_segment__c = a.sub_segment__c;
            }
         }
    
   }

}

 

By placing the Debug's like above you can find the problem.

 

This was selected as the best answer
LudivineLudivine
Hi, THANK YOU so much for the time you took and for your great explaination!! Your answer will really be useful to me also for the next case I will have to test. You are simply the best! Kind regards.