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
EuclidEuclid 

INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]

Hi,

 

I have been working on a page to have a mass update of owner in Salesforce.com for Leads & Opportunities. 

Based on the input(csv file) from user the code is supposed to update the Lead & Opportunity object with appropriate Owners.

 

However, I m getting the following error when trying to update the owner information. 

 

 

 

System.DmlException: Update failed. First exception on row 0 with id 006Q000000BKaeIIAT; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]

 

Following is the method which is being called:

 

public Pagereference ReadFile()
{
nameFile=contentFile.toString();
filelines = nameFile.split('\n');
system.debug('file lines: '+filelines);
system.debug('file line size: '+filelines.size());
OpptyTaskTransfer_CTE o = new OpptyTaskTransfer_CTE();
opptytoupdate = new List<Opportunity>();
try
{

for (Integer i=1;i<filelines.size();i++)
{
List<String> inputvalues=filelines[i].split(',');
if(o.isValidId(inputvalues[0].trim()))
{
listOppty.add(inputvalues[0].trim());
listUser.add(inputvalues[1].trim());
mapOpptyOwner.put(inputvalues[1].trim(),inputvalues[0].trim());
}
else
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please check the Lead Id: '+inputvalues[0].trim());
ApexPages.addMessage(errormsg);
}

}

Set<String> setUsers = new Set<String>();
setUsers.addAll(listUser);

for(String uname: setUsers)
{
if([select Id, Name,Username from User where username=:uname and isActive=true limit 1].size()!=1)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please check the Username: '+uname);
ApexPages.addMessage(errormsg);
}
}


allUsers = [select Id,Name,Username from User where Username IN: setUsers and isActive=true];

for (String username : setUsers)
{
for(User u : allUsers)
{
if(username ==u.Username)
{
mapUser.put(u.username,u.Id);
}
}
}

for(String uName : mapOpptyOwner.keySet())
{
mapOpptyOwnerId.put(mapOpptyOwner.get(uname),mapUser.get(uname));
}

allOppty = [select Id,OwnerId from Opportunity where Id IN: listOppty];

for(Id i : listOppty)
{
for(Opportunity op : allOppty)
{
if(i==op.Id)
{
op.OwnerId=mapOpptyOwnerId.get(i);
opptyToUpdate.add(op);
}
}
}

if(!opptytoupdate.isEmpty())
{
update opptytoupdate;
ApexPages.Message succmsg = new ApexPages.Message(ApexPages.severity.INFO,'Updated '+opptytoupdate.size()+' record(s) successfully.');
ApexPages.addMessage(succmsg);
}
}
catch (Exception e)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the file uploaded or try again later');
ApexPages.addMessage(errormsg);
}
return null;
}

 

I am unable to figure out the issue here. Can some one please help?

 

Thanks...

Best Answer chosen by Admin (Salesforce Developers) 
Yoganand GadekarYoganand Gadekar

this could be becuase of the wrong user id ( or no user id) provided for Owner of opportunity.

 

check this statement

op.OwnerId=mapOpptyOwnerId.get(i);

 

Debug this "mapOpptyOwnerId.get(i);" for proper user id or if it returns no value.

System.debug('**User id for opportunity owner**'+mapOpptyOwnerId.get(i));

 

Thanks,

All Answers

souvik9086souvik9086

Check '006Q000000BKaeIIAT' with this opportunity id. Here may be you are refrencing invalid owner id or blank owner. Please check in csv file for this opportunity id, what is the owner id there and the ckeck in the organisation whether that owner exists or not. If the owner id doesn't exist then the error will throw. You have to give a valid owner id.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Yoganand GadekarYoganand Gadekar

this could be becuase of the wrong user id ( or no user id) provided for Owner of opportunity.

 

check this statement

op.OwnerId=mapOpptyOwnerId.get(i);

 

Debug this "mapOpptyOwnerId.get(i);" for proper user id or if it returns no value.

System.debug('**User id for opportunity owner**'+mapOpptyOwnerId.get(i));

 

Thanks,

This was selected as the best answer
EuclidEuclid

Thanks Souvik & Yoganand.

It was stupid of me not to debug the map outputs. The issue was with the same getting null values. Thanks again!