+ Start a Discussion
kumarcrm bingikumarcrm bingi 

Variable does not exist: NPS_Id__c

Hi,
 I am writting trigger in I have Stakeholder object that is the related list to Opportunity and Contact. On Contact detail page, we have NPS ID field on the Contact detail page that is look up to the Voice of NPS object (Customer Object). The following code will get the NPS ID field value from the Contact detail page in the Stackholders page which we can able to clickable.
Create NPS Id 1 field on the stackholders object which is the look up to the Voice of NPS object (Customer Object)

This is my trigger

trigger updateNpsid on shareholder__c (before insert,before update) {
List<id> ConList = new List<id>();
 for(shareholder__c sh:Trigger.New){
ConList.add(sh.Contact_Name__c);
 }
 List<Contact> lc = [Select NPS_Id__c,NPSlookup__c From Contact Where id IN : ConList];
 for(shareholder__c sh:Trigger.New){
 sh.NPS_Id1__c = lc.NPS_Id__c;
 }
}

Error is Variable does not exist: NPS_Id__c
Alredy i am create the field in contact 
Ravi Dutt SharmaRavi Dutt Sharma
 sh.NPS_Id1__c = lc.NPS_Id__c;

In the above line, lc refers to the list. You should change it to lc[0].NPS_Id__c

Please mark this as the best answer if it resolves your problem.
kumarcrm bingikumarcrm bingi
Hi ravi,
Trigger is working good but my requrment is not fulfilled.  Shareholder create a record they throw exception Apex trigger updateNpsid caused an unexpected exception, contact your administrator: updateNpsid: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 1: Trigger.updateNpsid: line 8, column 1
AND Another issue is 
 User-added image
Ravi Dutt SharmaRavi Dutt Sharma
Can you please post the updated trigger.
kumarcrm bingikumarcrm bingi
trigger updateNpsid on shareholder__c (before insert,before update) {
List<id> ConList = new List<id>();
 for(shareholder__c sh:Trigger.New){
ConList.add(sh.Contact_Name__c);
 }
 List<Contact> lc = [Select NPS_Id__c,NPSlookup__c From Contact Where id IN : ConList];
 for(shareholder__c sh:Trigger.New){
 sh.NPS_Id1__c = lc[1].NPS_Id__c;
 }
}
Ravi Dutt SharmaRavi Dutt Sharma
trigger updateNpsid on shareholder__c (before insert,before update) {
List<id> ConList = new List<id>();
 for(shareholder__c sh:Trigger.New){
ConList.add(sh.Contact_Name__c);
 }
 List<Contact> lc = [Select NPS_Id__c,NPSlookup__c From Contact Where id IN : ConList];
 for(shareholder__c sh:Trigger.New){
 sh.NPS_Id1__c = lc[0].NPS_Id__c;
 }
}

 
kumarcrm bingikumarcrm bingi
Hi Ravi,
 I can mode trigger array is taken to  [0] but still error is showing. i write the corrent but they still showing error
User-added image
 
Ravi Dutt SharmaRavi Dutt Sharma
What is the data type of NPS_Id1__c on Shareholder and Contact object? I think its a mismatch of data type.
kumarcrm bingikumarcrm bingi
Ravi both are Data types is text