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
Gab SilvermotionGab Silvermotion 

Help with simple trigger

Hello!

I'm looking to create a trigger that will populate a picklist field from the asset name. Like If asset name starts with X, then set value Y. I tried with field updates but it won't fire until i save the record, and i need it to fire at the creation of the record (when i click on ''new case'')

the reason why i need a picklist auto populated is because i need it to control another picklist, and it won't work with a formula field.

can anyone help?

thanks!

Best Answer chosen by Admin (Salesforce Developers) 
David WrightDavid Wright

Based on your description, what I suggested should work perfectly. By creating a Case list view button, you can define a link that will go to the case detail page with the "Select_Project__c" field filled in based on logic you define within that button.

 

For example, this code in a case list view button will create a Button that goes to the case entry page with the Asset lookup populated:

 

https://cs10.salesforce.com/500/e?def_asset_id=02iA0000000yFbB&def_account_id=001A000000YCGOQ&ent=Case

 What you would need to do is view your Case edit page, do a "view source" or "inspect element" on the Select Project field (Google Chrome is a great tool for this), and look at what the ID of that field is. Take that ID, and append it as a paremeter to the URL. So if the ID of your Select_Project__c field is "00NA0000009nIRK", then you would append: &00NA0000009nIRK={!ValueYouWant}

 

you can use the logic built into the custom button creator to dynamically send the right value for "ValueYouWant"

 

No trigger necessary. Trigger isn't the right route here anyway, since you're talking about something that happens before any DML occurs.

All Answers

Abhi_TripathiAbhi_Tripathi

you are trying to update records, so trigger have only two condition for the Trigger

 

1. Before Update

2. After Update.

 

I think you shoud try before update and assign the value of the field as you want and then update List of record.

 

Thanks

 

Gab SilvermotionGab Silvermotion

would that kick in immediatly when i press ''new case'''?  or only when i save it?

Abhi_TripathiAbhi_Tripathi

It works when you click save button not on "new Case" 

Gab SilvermotionGab Silvermotion

what could i use then to prepopulate values when i press ''new''?

 

Nirmal ChristopherNirmal Christopher

Use before trigger in trigger event if your object u want to update is the same object.  for example if u want to do field update on assets from the values in assets write a before insert trigger in asset. If you want to update a field which is related with asset  user after insert trigger. Explain me which objects u want to build trigger i will help you

 

Gab SilvermotionGab Silvermotion

wow thanks!

 

so what lead me here is this situation.

 

we are a real estate promoter and we build condos. we use the cases to manage construction deficiencies. we name assets the following way : Project - Phase - Unit#, exemple : the royal - Phase 2 - 303.

 

we have a exhaustive list of subcontractors working on each project, and i wanted to use field dependency so if we create a case for a unit in project A, only the subcontractors for project A would show up in the list. i though i could use a formula field to control the subcontractor picklist, but no i've got to use another picklist.  I tried with workflow and field updates but it wont fire until i save the record.

 

si i'm trying to get something that uses something like IF ''asset.name'' STARTS WITH X, update field ''case.select_project__c'' with Y with the project and phase, so the field dependency will work without having my construction guys manually select a project.

David WrightDavid Wright

If I'm understanding you correctly, this is the process you're trying to implement:

 

1. User clicks "New Case"

2. User chooses an Asset from the asset lookup field

3. Upon selection of an asset, you want a picklist value to be immediately populated on the case edit screen

 

Is that correct? If so, I would suggest an alternative approach. Make the starting point for case entry be from the Asset detail page, by clicking "New Case" on the cases related list on that page (this would be a process decision.) You can make a Case list button that returns the URL for the case edit page, with the correct values appended to the URL as parameters to automatically fill in the fields you want.

 

You can do a view source on the case edit page to get the ID of the field you are trying to auto populate, and include that ID as a parameter in the URL returned by the list button along with the value you want assigned. Within the button definition, you can do the logic on whatever asset field you're evaluating to return the appropriate value.

Gab SilvermotionGab Silvermotion

this is the process.

 

1) from an asset, they click ''new case''

2) new case windows opens with the asset name already populated.

3) they select a project from the ''select_project__c'' field, which in turn controls the picklist of subcontractors. if they select a wrong project, the wrong list of subcontractor will display, causing errors in reports and everywhere.

 

 

i just want them to avoid the step of choosing a project (step 3) and have this done automatically.

David WrightDavid Wright

Based on your description, what I suggested should work perfectly. By creating a Case list view button, you can define a link that will go to the case detail page with the "Select_Project__c" field filled in based on logic you define within that button.

 

For example, this code in a case list view button will create a Button that goes to the case entry page with the Asset lookup populated:

 

https://cs10.salesforce.com/500/e?def_asset_id=02iA0000000yFbB&def_account_id=001A000000YCGOQ&ent=Case

 What you would need to do is view your Case edit page, do a "view source" or "inspect element" on the Select Project field (Google Chrome is a great tool for this), and look at what the ID of that field is. Take that ID, and append it as a paremeter to the URL. So if the ID of your Select_Project__c field is "00NA0000009nIRK", then you would append: &00NA0000009nIRK={!ValueYouWant}

 

you can use the logic built into the custom button creator to dynamically send the right value for "ValueYouWant"

 

No trigger necessary. Trigger isn't the right route here anyway, since you're talking about something that happens before any DML occurs.

This was selected as the best answer
Gab SilvermotionGab Silvermotion

ho wow thanks! i'll try playing with that this afternoon, this is great! :)

 

 

David WrightDavid Wright
Glad to help. Post back here if you have trouble getting it working and I'll see if I can assist further.

This message and any attachments are solely for the use of intended recipients. The information contained herein may include trade secrets, protected health or personal information, privileged or otherwise confidential information. Unauthorized review, forwarding, printing, copying, distributing, or using such information is strictly prohibited and may be unlawful. If you are not an intended recipient, you are hereby notified that you received this email in error, and that any review, dissemination, distribution or copying of this email and any attachment is strictly prohibited. If you have received this email in error, please contact the sender and delete the message and any attachment from your system. Thank you for your cooperation
Gab SilvermotionGab Silvermotion

ok so :

 

when i am in assets, and i click the new case button, this is the url i'm getting

 

https://na9.salesforce.com/500/e?retURL=%2F02iE0000001F579%3FsrPos%3D0%26srKp%3D02i&def_asset_id=02iE0000001F579&def_account_id=001E000000OZSyt&def_contact_id=003E000000Ogmz0

 

it autopopulates the asset, contact and account fields.

 

i used inspect element of chrome to retrieve the ID of the field which is 00NE0000004yfLC

 

so i would need to create a button where i append the &00NE0000004yfLC={!ValueYouWant} at the end? something like

 

https://na9.salesforce.com/500/e?def_asset_id={!Asset.Id}def_account_id={!Account.Id}&def_contact_id={!Contact.Id}ent=Case&00NE0000004yfLC={!ValueYouWant}, where {!ValueYouWant} is a formula? 

 

 

David WrightDavid Wright

That is correct. You'll create a Case button, and make it the "List View" type. This will allow you to add it to the "Cases" related list when you edit the Asset page layout and modify the Cases related list.

 

As far as the {!ValueYouWant} portion, this can be literally anything. Based on what you were saying, you are wanting to evaluate an Asset field and return a different value based on that asset field. So you could, for instance, have an IF formula that evaluates an asset field and returns the value you want to be put into the Case field on the case edit page. IF({!Asset.Name = "X","Y","Z") 

Gab SilvermotionGab Silvermotion

ok i'm trying and it says ''error : the field name is required'' when i check the syntax. it says that on the url you provided as well.

 

thanks again for all the help :) 

David WrightDavid Wright

Can you provide the full link you're attempting to save? Make sure you use the "Insert Code" button on the text editor when posting it so nothing gets lost

Gab SilvermotionGab Silvermotion
https://na9.salesforce.com/500/e?def_asset_id={!Asset.Id}def_account_id={!Account.Id}&def_contact_id={!Contact.Id}ent=Case&00NE0000004yfLC=[FORMULA]

 

that's what i'm trying to use.

 

so you're basically teaching me URL hacking as i can see :)  thank you, this is a superb skill to own :)

David WrightDavid Wright

Hmm... I do not know why you're getting an error. I just saved that link you posted into a button and received no errors.

 

To be clear, this is where you should be pasting the code:

 

1. Setup-->Cases-->Buttons, Links and Actions-->New Button or Link

2. Choose the "List Button" radio and select "Display in existing window without sidebar or header" from the "Behaviour" dropdown

3. Uncheck "Display checkboxes (for multi-record selection)"

4. Select "URL" from the "Content Source" dropdown

 

You should then be able to paste the url into the content of the button on that page and save without errors. The button editor (in my experience) is very forgiving and I've encountered very few errors. Like I said, I copied your exact link into a button in my org and it saved perfectly.

 

Some immediate corrections to your link are needed (keep in mind that since you're putting this on the cases related list of the Asset detail page, only Asset merge fields will work.

 

https://cs10.salesforce.com/500/e?def_asset_id={!Asset.Id}def_account_id={!Asset.AccountId}&ent=Case&00NE0000004yfLC=[FORMULA]

 

I changed {!Account.Id} to {!Asset.AccountId}. I also removed the contact parameter, since you don't have an asset field that references a contact ID (unless you happen to have created a custom contact lookup field for that, in which you could replace that parameter with the value of that custom field)

Gab SilvermotionGab Silvermotion

hello!  so your button works, i was able to add the def_contact_id parameter and everything works, it's just not populating the field i want to.  i double checked the ID of the field and it's ok.  maybe it's my formula? this formula works elsewhere though.

 

by the way you just teached me a very good thing to know!

 

 

https://na9.salesforce.com/500/e?def_asset_id={!Asset.Id}&def_account_id={!Asset.AccountId}&def_contact_id={!Asset.ContactId}&ent=Case&00NE0000004yfLC=
IF(BEGINS( {!Asset.Name}, "M9 Phase 2 - ") , "M9 Phase 2",
IF(BEGINS( {!Asset.Name}, "M9 Phase 3 - ") , "M9 Phase 3",
IF(BEGINS( {!Asset.Name}, "M9 Phase 4 - ") , "M9 Phase 4",
IF(BEGINS( {!Asset.Name}, "Avenue32 - I - ") , "Avenue32 - I",
IF(BEGINS( {!Asset.Name}, "Avenue32 - II - ") , "Avenue32 - II",
IF(BEGINS( {!Asset.Name}, "Castelnau - I - ") , "Castelnau - I",
IF(BEGINS( {!Asset.Name}, "Castelnau - II - ") , "Castelnau - II",
IF(BEGINS( {!Asset.Name}, "Castelnau - III - ") , "Castelnau - III",
IF(BEGINS( {!Asset.Name}, "St-Dominique - ") , "St-Dominique",
IF(BEGINS( {!Asset.Name}, "Square Benny - ") , "Square Benny",
null
))))))))))

 

 

 

David WrightDavid Wright

To be clear, is the URL appropriately evaluating as expected, so that once you hit the Case entry page, there's an actual value assigned to that parameter from the formula?

Gab SilvermotionGab Silvermotion

there is a value assigned to all the referenced fields (accound, contact) but not my picklist field.  if I try with a text field (let's say the subject field with ID ''cas14'' = derpderp, it work with ''derpderp'' prepopulated in the subject field.

 

anything particular with picklist fields?

 

should I use <option value="Castelnau - I"> in my formula rather than just "Castelnau - I" ?

 

edit : if i specify ''Avenue32 - I'' instead of the formula, it works, (prepopulates with ''Avenue32 - I" so there is something wrong with my formula i guess

David WrightDavid Wright

I just did some testing with this, what I found is that this DOES work for picklist fields, but you have to return a value in the URL that exactly matches an existing picklist value in your picklist, or it won't work. If you try to return a value that isn't in your picklist field, like "ERMAGHERD-BERKS", it will just not do anything.

 

What I'd suggest is to test each of the possible values by just editting the url on the case entry page, typing in each value manually, and make sure it sets the picklist field as desired. You might find some issues in your formula return not exactly matching the picklist values.

Gab SilvermotionGab Silvermotion

VICTORY!!!!!! thank you so much!

 

so i realised that maybe it had to fo with url encoding or whatnot, so instead, i created a formula field in assets with the formula i pasted earlier, and i'm just referencing that field in the URL used in the button.  it works perfectly.

 

wow this url hacking thing is going to be really handy! 

 

 

David WrightDavid Wright

I prefer "Maximizing the functional potential of URLs", but URL Hacking works too ;)

 

Enjoy!