+ Start a Discussion
AntonyasLenAntonyasLen 

Set a basic value to a lookup field

Hi,

 

I'm trying to set a "default" value to a lookup field called Carrier on Opportunity Product.

My Opportunity Product also have a text field called Default Carrier.

Every product have a default carrier, so i  want to set a default value for my lookup field.( the salesman can choose an other Carrier if they want).

 

I know that we can't use the workflow to populate the lookup field so i tried the trigger below:

 

trigger SetCarrier on Account (before insert) {
    for(OpportunityLineItem op : Trigger.new){
       op.Carrier__c = [Select Carrier__c.Id from Carrier__c Where Carrier__c.Name = :op.Default_carrier__c];
    }
}

 i got this error message:

 

Save error: Illegal assignment from LIST<Carrier__c> to Id

 

But in every case i'll not have a list =/.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Richie DRichie D

Hi,

 

You are trying to link an 'id' to an sobject. This should fix the problem:-

trigger SetCarrier on Account (before insert) {
    for(OpportunityLineItem op : Trigger.new){
       Carrier__c default = [Select Carrier__c.Id from Carrier__c Where Carrier__c.Name = :op.Default_carrier__c];
       op.Carrier__c = default.id;
    }
}

 You should look at bulkifying your trigger and checking for default not being available; but that is for another post...

 

Regards,

Rich.

All Answers

Richie DRichie D

Hi,

 

You are trying to link an 'id' to an sobject. This should fix the problem:-

trigger SetCarrier on Account (before insert) {
    for(OpportunityLineItem op : Trigger.new){
       Carrier__c default = [Select Carrier__c.Id from Carrier__c Where Carrier__c.Name = :op.Default_carrier__c];
       op.Carrier__c = default.id;
    }
}

 You should look at bulkifying your trigger and checking for default not being available; but that is for another post...

 

Regards,

Rich.

This was selected as the best answer
AntonyasLenAntonyasLen

It looks like working thanks.

 

But don't forget that you can not use the "default" word is you don''t want to get this error
>> Save error: Identifier name is reserved: default

 

but i wanted to be sure...this will just set the value the first time isn't it?