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
praveenkumarpraveenkumar 

Opportunity Product line check Trigger

Hi All,

 

The below is the code for Opportunity product line check trigger, when Save this code i am getting an error like 

 

 

ErrorError: Compile Error: Invalid field Product_Line__c for SObject Opportunity at line 26 column 30

 

 

This Product_Line__c field exists in OpportunityLineItem Object and Exclusion_Check__c exists in Opportunity object, How can i use the Product_Line__c field value, if write the trigger in OpportunityLineItem.... 

 

trigger OpportunityProductLineCheck on Opportunity (before update)

{

    String CN_var='';

    String OE_var='';

    String OB_var='';

    String OR_var='';

 

Set<id> OpptyIDs = new set<id>(); // Creating a set for storing Opportunity ids

 

for(opportunity o:trigger.new)

{

    OpptyIDs.add(o.id); // Adding ids into OpptyIDs

}

 

list<Opportunitylineitem> opp=[Select Id,Product_Line_F__c from OpportunityLineItem where OpportunityId in : OpptyIDs];

 //Getting id and Product_Line__c field values in to opp list

 

list<Opportunitylineitem> proline= [select Product_Line_F__c from OpportunityLineItem where OpportunityId in : OpptyIDs];

//Creating a list and adding the Product_Line__c field values into proline list

 

map<id,id> id_pro=new map<id,id>();

//Creating a list for putting the ids and Product_Line__c in to id_pro Map 

 

for(Opportunity op:trigger.new)

{

     mymap.put(op.id, op.Product_Line__c  );

}

list<Opportunitylineitem> opppro=new  list<Opportunitylineitem>();

for(Opportunitylineitem o : opp )

 {

     opppro=[select Id,Product_Line_F__c from OpportunityLineItem where OpportunityId = : o.ID];

      for(OpportunityLineItem oli : opppro)

      {

         if(oli.Product_Line_F__c=='Connect')

         {

              CN_var = '[CN]';

         }

         if(oli.Product_Line_F__c=='OpenEdge')

         {

              OE_var = '[OE]';

         }

         if(oli.Product_Line_F__c=='ObjectStore')

         {

              OB_var = '[OB]';

         }

         if(oli.Product_Line_F__c=='Orbix')

         {

              OR_var = '[OR]';

         }

     }

     for(opportunity op: opp)

     {

              op.Exclusion_Check__c = CN_var + OE_var + OB_var + OR_var;

              system.debug('*** Filter products checked; applicable code added to filter field ****');

     }

}

 

 

Writing the trigger on one object, i need to use the field value of another object....

 

Please help me in writing this trigger...

 

 

Thanks in Advance...

 

Regards,

Praveen K

bob_buzzardbob_buzzard

Looks like a typo - you are referring to the field as Product_Line_F__c except for this one line:

 

 mymap.put(op.id, op.Product_Line__c  );

 

praveenkumarpraveenkumar

Hi Bob buzzard, 

 

Thanks for your reply towards helping out me, I have changed it but still it is showing the same error.

 

I am writing the trigger on opportunity,

 

But, This Product_Line_F__c Exists in different object ( OpportunityLineItem ) not in opportunity...

 

How can i Use the field from one object to another object....

 

 

Please let me know...

 

 

Thanks,

Praveen K

bob_buzzardbob_buzzard

Ah, I see.  In that case its simply that Product_Line__c doesn't exist as a field on the opportunity sobject type - have you checked that is definitely the field API name?

praveenkumarpraveenkumar

Hi ,

 

The fields Product_line_F__c and Product_Line__c are not two fields, That was a mistake while posting..

 

But The actual field API name is Product_line_F__c only.

 

Product_line_F__c exists in OpportunityLineItem, now my requirement is i need to use this field in Opportunity object...

 

What statement i need to use to get the value of Product_line_F__c field value in Opportunity object..

 

I am writing the trigger on Opportunity Object only... 

 

Thanks in advance....

 

bob_buzzardbob_buzzard

As it doesn't exist on the opportunity, you'll need to query all the related opportunity line items and extract the field from those.  However, this will give you many fields rather than just one - is that what you are looking for?