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
Wayne_ClarkWayne_Clark 

Need Help adding IF statement in Trigger

Any idea on how I could make this trigger fire to another field if the 1st field is full?

 

I have a custom object called Quote_Ocean_Container__c that is a Master-Detail to the Quote and I'm unable to create a workflow to update the custom field on the quote.  Whenever a record is created in the Quote_Ocean_Container__c, the trigger will combine to fields and update a custom field on the Quote called "Container_1__c". 

 

However, if Container_1__c is already filled in, I want the trigger to update Container_2__c, and if that is filled in, then update Container_3__c and so on.  There are 6 Container fields.

 

Thanks in advance for any guidance or advise.

 

 

Trigger UpdateQuoteContainer1 on Quote_Ocean_Container__c(after insert, after update)

{

Set<Id> ids = trigger.newmap.keySet();

List<Quote_Ocean_Container__c> quocon=[select Quote__r.Container_1__c, Number_Required__c, Container_Type__c, from  Quote_Ocean_Container__c where id IN :ids];

List<Quote> updateQuote=new List<Quote>();

For(Quote_Ocean_Container__c Quocon:quo)

{

Quocon.Quote__r.Ocean_Container__c=Quocon.Number_Required__c + '-' + '  ' + Quocon.Container_Type__c;

updateQuote_Ocean_Container__c.add(Quocon.Quote__r);

}

Update updateQuote_Ocean_Container__c;

}

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef

I am not sure of a better way to do this but here is my 2 cents.

 

 

For(Quote_Ocean_Container__c Quocon:quo)

{
if(uocon.Quote__r.Ocean_Container__c == null){
  
  Quocon.Quote__r.Ocean_Container__c=Quocon.Number_Required__c + '-' + '  ' + Quocon.Container_Type__c;
}else if(uocon.Quote__r.Ocean_Container_1__c == null){
  Quocon.Quote__r.Ocean_Container_1__c=Quocon.Number_Required__c + '-' + '  ' +  Quocon.Container_Type__c;
}else if(uocon.Quote__r.Ocean_Container_2__c == null){
  Quocon.Quote__r.Ocean_Container_2__c=Quocon.Number_Required__c + '-' + '  ' + Quocon.Container_Type__c;
}else if(uocon.Quote__r.Ocean_Container_3__c == null){
  Quocon.Quote__r.Ocean_Container_3__c=Quocon.Number_Required__c + '-' + '  ' + Quocon.Container_Type__c;
}//and so on

updateQuote_Ocean_Container__c.add(Quocon.Quote__r);

}

The way you have your data model set up I don't think there is anything else you can do. The else if statements will only fire the one that is true and move on so you don't have to worry about over writing the container fields.

 

One question will there ever be more then 6 containers? If so you might want to solve this problem with so visualforce.

 

Hope this helps.

 

All Answers

mikefmikef

I am not sure of a better way to do this but here is my 2 cents.

 

 

For(Quote_Ocean_Container__c Quocon:quo)

{
if(uocon.Quote__r.Ocean_Container__c == null){
  
  Quocon.Quote__r.Ocean_Container__c=Quocon.Number_Required__c + '-' + '  ' + Quocon.Container_Type__c;
}else if(uocon.Quote__r.Ocean_Container_1__c == null){
  Quocon.Quote__r.Ocean_Container_1__c=Quocon.Number_Required__c + '-' + '  ' +  Quocon.Container_Type__c;
}else if(uocon.Quote__r.Ocean_Container_2__c == null){
  Quocon.Quote__r.Ocean_Container_2__c=Quocon.Number_Required__c + '-' + '  ' + Quocon.Container_Type__c;
}else if(uocon.Quote__r.Ocean_Container_3__c == null){
  Quocon.Quote__r.Ocean_Container_3__c=Quocon.Number_Required__c + '-' + '  ' + Quocon.Container_Type__c;
}//and so on

updateQuote_Ocean_Container__c.add(Quocon.Quote__r);

}

The way you have your data model set up I don't think there is anything else you can do. The else if statements will only fire the one that is true and move on so you don't have to worry about over writing the container fields.

 

One question will there ever be more then 6 containers? If so you might want to solve this problem with so visualforce.

 

Hope this helps.

 

This was selected as the best answer
Wayne_ClarkWayne_Clark

Thanks Mike,

 

That's what I was looking for, the If's and else If's, your code worked with a little modifications.

 

My only issue now is deleting the updated field when the corresponding record in the Container object is deleted.

 

Maybe I should just put an idea in to allow custom controllers on the Quote templates.

 

Thanks again!