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
Jim MontgomeryJim Montgomery 

Getting error "variable does not exist QP.Name".

Here is my code.

trigger CopyPromotionName on Apttus_Proposal__Proposal_Line_Item__c (after insert,after update) {


  Map<String,String> PromotionLookup = new Map<String,String>();
  Set<String> PromotionCode = new Set<String>();    

  for(Apttus_proposal__proposal_line_Item__c p:Trigger.new) {
    PromotionCode.add(p.apts_promo_code__c);
  }
  
  for(Promotion__c QP:

    [SELECT      name,promo_code__c
     FROM        Promotion__c
     WHERE       promo_code__c IN :PromotionCode]) {

     PromotionLookup.put(QP.Name,QP.Promotion_Code__c);

  }

  for(Apttus_proposal__proposal_line_Item__c p:Trigger.new) {

    if(PromotionLookup.containsKey(p.apts_Promo_Code__c)) {

      p.Promotion_Name__C = PromotionLookup.get(QP.name);

    }

  }
Best Answer chosen by Jim Montgomery
LBKLBK
I think I get it. You have Promotion Code in Apttus_proposal__proposal_line_Item__c object, you want to lookup for this code in Promotion__c object and populate the name.

If yes, here is the updated trigger for you.
 
trigger CopyPromotionName on Apttus_Proposal__Proposal_Line_Item__c (before insert,before update) {

  Map<String,String> PromotionLookup = new Map<String,String>();
  Set<String> PromotionCode = new Set<String>();    

  for(Apttus_proposal__proposal_line_Item__c p:Trigger.new) {
    PromotionCode.add(p.apts_promo_code__c);
  }
  
  for(Promotion__c QP:

    [SELECT      name,promo_code__c
     FROM        Promotion__c
     WHERE       promo_code__c IN :PromotionCode]) {

     PromotionLookup.put(QP.Promotion_Code__c, QP.Name);
  }

  for(Apttus_proposal__proposal_line_Item__c p:Trigger.new) {

    if(PromotionLookup.containsKey(p.apts_Promo_Code__c)) {
      p.Promotion_Name__C = PromotionLookup.get(p.apts_Promo_Code__c);
    }
  }
}
I guess you need a before insert, before update trigger, not the after trigger.
 

All Answers

LBKLBK
You are trying to access QP object outside the scope of the for loop.

QP is not available for the line you have highlighted.
Jim MontgomeryJim Montgomery
how would I manke it available?
LBKLBK
I think what you need is this.
 
p.Promotion_Name__C = PromotionLookup.get(p.apts_Promo_Code__c);
Can you replace the highlighted line in your code with the above line?
 
Jim MontgomeryJim Montgomery
I did that, and no errors, but it does not update the promotion name filed on the proposal line item.
LBKLBK
I think I get it. You have Promotion Code in Apttus_proposal__proposal_line_Item__c object, you want to lookup for this code in Promotion__c object and populate the name.

If yes, here is the updated trigger for you.
 
trigger CopyPromotionName on Apttus_Proposal__Proposal_Line_Item__c (before insert,before update) {

  Map<String,String> PromotionLookup = new Map<String,String>();
  Set<String> PromotionCode = new Set<String>();    

  for(Apttus_proposal__proposal_line_Item__c p:Trigger.new) {
    PromotionCode.add(p.apts_promo_code__c);
  }
  
  for(Promotion__c QP:

    [SELECT      name,promo_code__c
     FROM        Promotion__c
     WHERE       promo_code__c IN :PromotionCode]) {

     PromotionLookup.put(QP.Promotion_Code__c, QP.Name);
  }

  for(Apttus_proposal__proposal_line_Item__c p:Trigger.new) {

    if(PromotionLookup.containsKey(p.apts_Promo_Code__c)) {
      p.Promotion_Name__C = PromotionLookup.get(p.apts_Promo_Code__c);
    }
  }
}
I guess you need a before insert, before update trigger, not the after trigger.
 
This was selected as the best answer
Jim MontgomeryJim Montgomery
Yes, that is what I want it to do. Pasted in your code and it still does not update the promotion name filed.
LBKLBK
Can you add some debug statements to see if the Promotion__c and Apttus_proposal__proposal_line_Item__c has the right Promotion Code values in them?

Also, check if the PromotionLookup  Map is populated with right values.
rajat Maheshwari 6rajat Maheshwari 6

Hi Jim,

Promotion_Code__c and promo_code__c  are two different fields ? 

Jim MontgomeryJim Montgomery
I added debug code, and it started working. I removed it and it is still working, so THANK YOU!!!!