You need to sign in to do that
Don't have an account?

Batch Process Attempt to de-reference a null object
Not sure why I 'm getting a null pointer error here in the batch process, any thoughts are appreciated :)
Code:
public Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'SELECT Id, (SELECT ContactId, IsPrimary FROM OpportunityContactRoles ORDER BY IsPrimary DESC) FROM Opportunity WHERE LastModifiedDate = LAST_N_DAYS:365';
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List < sObject > s)
{
List < Opportunity > oppsToUpdate = new List < Opportunity > ();
for (sObject ssss: s)
{
Opportunity o = (Opportunity) ssss;
List < OpportunityContactRole > opptys = new List < OpportunityContactRole > ();
opptys = (List < OpportunityContactRole > ) o.getsobjects('OpportunityContactRoles'); //o.getsobjects will return a List<SObject>, in the same line we cast those into List<OpportunityContactRole>
for (OpportunityContactRole optt: opptys) // HERE IS WHERE THE ERROR IS. LOOPING THROUGH SUB QUERY LIST
{
String contId = '';
contId = String.ValueOf(optt.ContactId);
if (!String.IsBlank(contId))
{
o.Primary_Contact_Id__c = contId;
oppsToUpdate.add(o);
break;
}
}
}
update oppsToUpdate;
}
Code:
public Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'SELECT Id, (SELECT ContactId, IsPrimary FROM OpportunityContactRoles ORDER BY IsPrimary DESC) FROM Opportunity WHERE LastModifiedDate = LAST_N_DAYS:365';
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List < sObject > s)
{
List < Opportunity > oppsToUpdate = new List < Opportunity > ();
for (sObject ssss: s)
{
Opportunity o = (Opportunity) ssss;
List < OpportunityContactRole > opptys = new List < OpportunityContactRole > ();
opptys = (List < OpportunityContactRole > ) o.getsobjects('OpportunityContactRoles'); //o.getsobjects will return a List<SObject>, in the same line we cast those into List<OpportunityContactRole>
for (OpportunityContactRole optt: opptys) // HERE IS WHERE THE ERROR IS. LOOPING THROUGH SUB QUERY LIST
{
String contId = '';
contId = String.ValueOf(optt.ContactId);
if (!String.IsBlank(contId))
{
o.Primary_Contact_Id__c = contId;
oppsToUpdate.add(o);
break;
}
}
}
update oppsToUpdate;
}
The opportunity doesnot have the OpportunityContactRole. Try the below that might work
All Answers
The opportunity doesnot have the OpportunityContactRole. Try the below that might work