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
@anilbathula@@anilbathula@ 

System.ListException: List index out of bounds

Hi guys,

 

please help me to come out of this error.

 

for(Comments__c cs:trigger.New){
cms=cs;
if(cms.Opportunity__c==null && cms.Contact__c!=null ){
sobjectSetOfIds.add(cms.Contact__c);
}
}

Map<Id,Contact>smap1= new Map<Id, Contact>([Select id
,Name
,(select id ,Name from opportunities limit1)
from Contact
where Id in : sobjectSetOfIds]);

for(Comments__c s: trigger.new){
if(smap1.containsKey(s.Contact__c)){
s.Opportunity__c=smap1.get(s.Contact__c).Opportunities__r[0].id;  //Error
}
}

 

Thanks

Anil.B

sanjdevsanjdev

Hi 

 

Just remove [0] from the line "s.Opportunity__c=smap1.get(s.Contact__c).Opportunities__r[0].id;" and make it as below 

s.Opportunity__c=smap1.get(s.Contact__c).Opportunities__r.id .

 

Mark it as resolved if it helps you.

Sanj

@anilbathula@@anilbathula@

Hi sanjdev,

 

it gives error when u remove [0].

the error will be :- Invalid foreign key relationship: Contact.Opportunities__r

Thanks for ur response

 

Thanks

Anil.B

SamuelDeRyckeSamuelDeRycke

I think it is 

 

s.Opportunity__c=smap1.get(s.Contact__c).Opportunities[0].id;

@anilbathula@@anilbathula@

Hi Sdry

 

Thanks for the reply .

 

actually its giving error.

if i use this   s.Opportunity__c=smap1.get(s.Contact__c).Opportunities[0].id;

Error:The configuration of your org has changed, please reload the page. Missing dependent object: Field: Opportunity.ContactId at line 47 column 54


If i use this way s.Opportunity__c=smap1.get(s.Contact__c).Opportunities__r[0].id;

 No error on saving the trigger .But it displaying error on creating the record.

Error:

System.ListException: List index out of bounds: 0: Trigger.Updtcon: line 48, column 1

 

 

Thanks

Anil.B

 


SamuelDeRyckeSamuelDeRycke

That makes it easier .. it is never good practise to hardcode an index without checking if it and the list exist.

 

if(smap1.get(s.Contact__c).Opportunities__r[0] != null && smap1.get(s.Contact__c).Opportunities__r[0].size() >0){

s.Opportunity__c=smap1.get(s.Contact__c).Opportunities__r[0].id ;

}

@anilbathula@@anilbathula@

Hi Sdry

 

It giving a new error that is Method doesnot exist or incorrect signature : [SOBJECT:Opportunity].size() at line 48 column 114

 

Can u check the trigger ;-

trigger Updtcon on comments__c (before insert) {
Set<Id> sobjectSetOfIds = new Set<Id>();

Comments__c cms;
opportunity opp;
Contact con;

/* updating Contact from Opportunity object comments related list*/
for(Comments__c cs:trigger.New){
cms=cs;
if(cms.Opportunity__c!=null && cms.Contact__c==null ){
sobjectSetOfIds.add(cms.Opportunity__c);
}
}

Map<Id,Opportunity>smap= new Map<Id, Opportunity>([Select id
,Name
,Borrower__c
from Opportunity
where Id in : sobjectSetOfIds]);

for(Comments__c s: trigger.new){
if(smap.containsKey(s.Opportunity__c)){
s.Contact__c=smap.get(s.Opportunity__c).Borrower__c;

}
}
/* updating opportunity from contact object comments related list*/

for(Comments__c cs:trigger.New){
cms=cs;
if(cms.Opportunity__c==null && cms.Contact__c!=null ){
sobjectSetOfIds.add(cms.Contact__c);
}
}

Map<Id,Contact>smap1= new Map<Id, Contact>([Select id
,Name
,(select id,name from opportunities)
from Contact
where Id in : sobjectSetOfIds]);
System.debug('::->'+smap1);


for(Comments__c s: trigger.new){
if(smap1.containsKey(s.Contact__c)){

if(smap1.get(s.Contact__c).Opportunities__r[0] != null && smap1.get(s.Contact__c).Opportunities__r[0].size()>0){   //error
s.Opportunity__c=smap1.get(s.Contact__c).Opportunities__r[0].id ;
}
}

}
}

 

 

 

Thanks

Anil.B



Jerun JoseJerun Jose

First of all, you have the relationship name wrong. It should be

smap1.get(s.Contact__c).Opportunities not smap1.get(s.Contact__c).Opportunities__r

 

Next,

smap1.get(s.Contact__c).Opportunities points to a related list of records.

What you have to do is to check if this list is empty.
So what you will need to do is

if(smap1.get(s.Contact__c).Opportunities != null && smap1.get(s.Contact__c).Opportunities.size()>0)

If you know that the list Opportunities has elements, then you can reference the first entry in that list.

 

Edit - Updated to use Opportunities instead of Opportunities__r

@anilbathula@@anilbathula@

Hi Jerun jose

 

Thanks a lot for ur reply.

 

When i use Opportunities it is showing a different error:-

Error: Compile Error: The configuration of your org has changed, please reload the page. Missing dependent object: Field: Opportunity.ContactId at line 49 column 58

 

Thanks

Anil.B

 

Jerun JoseJerun Jose
Could you post the complete trigger again ?
@anilbathula@@anilbathula@

The updated trigger:-

 

 

trigger Updtcon on comments__c (before insert) {
Set<Id> sobjectSetOfIds = new Set<Id>();

Comments__c cms;
opportunity opp;


/* updating Contact from Opportunity object comments related list*/
for(Comments__c cs:trigger.New){
cms=cs;
if(cms.Opportunity__c!=null && cms.Contact__c==null ){
sobjectSetOfIds.add(cms.Opportunity__c);
}
}

Map<Id,Opportunity>smap= new Map<Id, Opportunity>([Select id
,Name
,Borrower__c
from Opportunity
where Id in : sobjectSetOfIds]);

for(Comments__c s: trigger.new){
if(smap.containsKey(s.Opportunity__c)){
s.Contact__c=smap.get(s.Opportunity__c).Borrower__c;

}
}
/* updating opportunity from contact object comments related list*/

for(Comments__c cs:trigger.New){
cms=cs;
if(cms.Opportunity__c==null && cms.Contact__c!=null ){
sobjectSetOfIds.add(cms.Contact__c);
}
}

Map<Id,Contact>smap1= new Map<Id,Contact>([Select id
,Name
,(select id,name from opportunities)
from Contact
where Id in : sobjectSetOfIds]);
System.debug('::->'+smap1);


for(Comments__c s: trigger.new){
if(smap1.containsKey(s.Contact__c)){


if(smap1.get(s.Contact__c).Opportunities[0] != null && smap1.get(s.Contact__c).Opportunities[0].size()>0){
s.Opportunity__c=smap1.get(s.Contact__c).Opportunities[0].id ;
}
}

}
}

Jerun JoseJerun Jose

Try the code below and let me know if you have issues

 

trigger Updtcon on comments__c (before insert) {
	Set<Id> sobjectSetOfIds = new Set<Id>();
	Comments__c cms;
	opportunity opp;

	/* updating Contact from Opportunity object comments related list*/
	for(Comments__c cs:trigger.New){
		cms=cs;
		if(cms.Opportunity__c!=null && cms.Contact__c==null ){
			sobjectSetOfIds.add(cms.Opportunity__c);
		}
	}

	Map<Id,Opportunity>smap= new Map<Id, Opportunity>([Select id, Name, Borrower__c from Opportunity where Id in : sobjectSetOfIds]);

	for(Comments__c s: trigger.new){
		if(smap.containsKey(s.Opportunity__c)){
			s.Contact__c=smap.get(s.Opportunity__c).Borrower__c;
		}
	}
	/* updating opportunity from contact object comments related list*/

	for(Comments__c cs:trigger.New){
		cms=cs;
		if(cms.Opportunity__c==null && cms.Contact__c!=null ){
			sobjectSetOfIds.add(cms.Contact__c);
		}
	}

	Map<Id,Contact>smap1= new Map<Id,Contact>([Select id, Name, (select id,name from opportunities) from Contact where Id in : sobjectSetOfIds]);
	System.debug('::->'+smap1);

	for(Comments__c s: trigger.new){
		if(smap1.containsKey(s.Contact__c)){
			if(smap1.get(s.Contact__c).Opportunities != null && smap1.get(s.Contact__c).Opportunities.size()>0){
				s.Opportunity__c=smap1.get(s.Contact__c).Opportunities[0].id ;
			}
		}
	}
}

 

@anilbathula@@anilbathula@

Jerun Jose,

 

sorry man it showing the same error.

Error: Compile Error: The configuration of your org has changed, please reload the page. Missing dependent object: Field: Opportunity.ContactId at line 48 column 58

 

Thanks

Anil.B

 

 

Jerun JoseJerun Jose
I did a quick search and seems like this error message is something like a salesforce bug. The solutions that were offered involved changing the API version and saving the code in a separate class.
@anilbathula@@anilbathula@

Hi guys

 

Thanks a lot for ur support to get me the solution.

But unfortunately we could not able to figure out the solution in this way.

But i used another way and got it solved temporarily.By the help of   kerwintang .

this is the link:-

http://boards.developerforce.com/t5/Apex-Code-Development/How-to-query-for-opportunity-from-contact/m-p/474297#M87041

 

The efforts u put on for giving me a solution is marvelous .

Hats off to u guys Jerun Jose and  Sdry.

I raised a case to salesforce on this but they said they stopped support for developers .so i raised a case from my company org lets check what they will do.

 

Thanks to sanjdev also.

 

Thanks

Anil.B