You need to sign in to do that
Don't have an account?
Trigger to update OwnerID to Customer Portal User
Hello..writing my first trigger. Could use your help :)
I have a custom object Financial__c that is a upload of payment information. I want to share this with Customer Portal users. The sharing is private since each customer can only see their payment. Also the upload has to happen using DataLoader.io which does not allow for OwnerID update. I need to update the OwnerId to the ID of the Customer Portal user so they can see it. As far I as know I can only do this with a trigger. Here is the simple trigger I've started with but getting an error. Appreciate your help!
trigger UpdateOwnerRecordBulk on Financial__c (before insert, before update) {
for (Financial__c oPayment : trigger.new) {
oPayment.OwnerID = [Select ID from USER WHERE ContactID=:oPayment.Home_Owner__c];
}
}
Error: Illegal assignment from LIST<User> to Id
Yes thats it!! worked like a charm with 100% coverage..thank you thank you so much for your help. I learnt a lot. Final code for everyone. This trigger and test class changes the OwnerID of a custom object on create and update. Thanks to Sean Tan for his HUGE help in this.
TEST CLASS
TRIGGER
All Answers
You're trying to assign the list of users from the query to the OwnerId. General SOQL statements in Apex will return a list of SObjects (this can vary depending on the type of query). You also want to avoid querying in a loop. Try something like this:
Thanks! that worked but having other issues now. Tried for a fewf hours to write a test method for the trigger to get 100% coverage but getting a really annoying error. Here is my entire code
trigger UpdateOwnerRecordBulk onFinancial__c (beforeinsert, beforeupdate)
{
Map<Id, User> userMap = new Map<Id, User>{};
for (Financial__c oPayment : trigger.new)
{
if (oPayment.Home_Owner__c != null)
{
userMap.put(oPayment.Home_Owner__c, null);
}
}
for (User u : [Select ID, ContactID from USER WHERE ContactID IN :userMap.keySet()])
{
userMap.put(u.ContactID, u);
}
for (Financial__c oPayment : Trigger.new)
{
User u = userMap.get(oPayment.Home_Owner__c);
if (u != null)
{
oPayment.OwnerId = u.Id;
}
}
}
@isTest
private class UpdateOwnerRecordBulk {
static testMethod void verifyOnwerIDChange(){
// Perform our data preparation.
List<Financial__c> payments = new List<Financial__c>{};
for(Integer i = 0; i < 200; i++){
Financial__c a = newFinancial__c(id);
payments.add(a);
}
// Start the test, this changes governor limit context to
// that of trigger rather than test.
test.startTest();
// Insert the Account records that cause the trigger to execute.
insert payments;
// Stop the test, this changes limit context back to test from trigger.
test.stopTest();
// Query the database for the newly inserted records.
List<Financial__c> insertedPayments = [SELECT ID, OwnerID
FROM Financial__c
WHERE Id IN :Payments];
// Assert that the Description fields contains the proper value now.
for(Financial__c a : insertedPayments){
insertedPayments.OwnerId = '005J00000013F86IAE'
}
}
Getting the error Save errod: unexpected token:@ just before the @isTest line..what am I doing wrong? Also does the test method look ok? Thanks again!!
Did you make a separate class file for the test class or did you try to put it in the same file as the trigger? If the latter try moving it to it's own test class.
In terms of the test class itself, you have a good portion of it there, the only problem is you're assuming data exists on the org (so if you were to deploy the test class to a different org it would fail). Nor are you assigning the Home_Owner__c value.
So I would do something like this:
This may still fail if some required fields are missing for the insert of records... but it should get you in the right direction.
Thanks that definetly helped. Here is my test class so far.
Error I am getting is on line 43 and the debug log says
FIELD_INTEGRITY_EXCEPTION, only portal users can be associated to a contact: []
which makes sense since the contact user is not a portal user. So how do we enable the contact as the portal user in the code?
Thanks so much for your help so far!
Reading this article and it looks like it could help http://wiki.developerforce.com/page/Apex_Testing_with_RunAs
Based off the article try this type of variation:
Thanks again..getting very close..now I have the error save error initial term of field expression must be a concrete sobject LIST<Financial__c>
I think the issue is with this for statement at the very end
since its a list..should it be in some other format? Thanks.
Are you trying to make sure the owner id is the same as the user id?
Then you should use an assert statement. The reason for the error is you're assigning the user id to an owner Id property on the List object not the looped record.
Try this for the loop:
Thanks Sean..that got rid of that error but now I get
FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject
Here is the entire code so far.
When you post the errors can you post the full trace (including the line numbers). It helps to pinpoint issues... In terms of the problem I'm guessing it is due to the following lines:
You can try changing it to something like the following:
Here is the entire Debug Log..Not suire if it's the ID rt1D1 line causing the error. Thanks.
Ok so it's this:
Profile p = [select id
from profile
where usertype = :portalType.name()
limit 1];
It's not returning any profile for that particular portal type... What type's of portal user type's are on the org? Unfortunately you can't insert these types in the test method so you have to assume that data exists. If you know which Profile type exists you can change the line here:
User u = getPortalUser(PortalType.PowerPartner, c, true);
And change the PortalType.PowerPartner to one of the following:
Yes thats it!! worked like a charm with 100% coverage..thank you thank you so much for your help. I learnt a lot. Final code for everyone. This trigger and test class changes the OwnerID of a custom object on create and update. Thanks to Sean Tan for his HUGE help in this.
TEST CLASS
TRIGGER
The trigger and class I posted work great but I tried to create a package and getting this DML exception error on the Class. Has anyone seen this?