You need to sign in to do that
Don't have an account?
Sourav P
Autopopulate the fields based on a look field value from a standard object
Hi, Can anyone help me out with this,
This is from a custom object " Quotation", The look up field " Main Driver" is a look up to Personal account object. The below fields are as well exists in the personal account. I need whatever value would be choosen in the main driver, automatically the below values will get pop up on below fields. I need to write a trigger for this ( I cant use formula field, as its depends upon another criteria field, so somtimes the user need to enter values manually in these fields , Process builder also doesnt worked).
I tried to write a trigger, but its not working , showing issue as " Error: Compile Error: IN operator must be used with an iterable expression at line 12 column 104 " Can anyone plz rectify my trigger to make it work. Thnx .( I just have taken two fields below for test)
trigger MainDriverRelatedFields on Quotation__c (before insert,before update) {
Quotation__c newQuote=new Quotation__c();
for(Quotation__c obj : Trigger.new){
newQuote=obj ;
}
List<Account> Acc= [SELECT id,Age__c, Acccidents_in_the_last_12_mnths__c FROM Account WHERE Id IN: newQuote.Main_Driver__r];
if(newQuote.Main_Driver__c!=null) {
newQuote.AgeMainD__c=Acc.Age__c;
newQuote.Accidents_in_the_last_12_mnthsMainD__c= Acc.Acccidents_in_the_last_12_mnths__c;
}
}
This is from a custom object " Quotation", The look up field " Main Driver" is a look up to Personal account object. The below fields are as well exists in the personal account. I need whatever value would be choosen in the main driver, automatically the below values will get pop up on below fields. I need to write a trigger for this ( I cant use formula field, as its depends upon another criteria field, so somtimes the user need to enter values manually in these fields , Process builder also doesnt worked).
I tried to write a trigger, but its not working , showing issue as " Error: Compile Error: IN operator must be used with an iterable expression at line 12 column 104 " Can anyone plz rectify my trigger to make it work. Thnx .( I just have taken two fields below for test)
trigger MainDriverRelatedFields on Quotation__c (before insert,before update) {
Quotation__c newQuote=new Quotation__c();
for(Quotation__c obj : Trigger.new){
newQuote=obj ;
}
List<Account> Acc= [SELECT id,Age__c, Acccidents_in_the_last_12_mnths__c FROM Account WHERE Id IN: newQuote.Main_Driver__r];
if(newQuote.Main_Driver__c!=null) {
newQuote.AgeMainD__c=Acc.Age__c;
newQuote.Accidents_in_the_last_12_mnthsMainD__c= Acc.Acccidents_in_the_last_12_mnths__c;
}
}
Try this below trigger. I am doing only one field and if that field is filled manually by user it wont populate value from account.
Let me know if you have any issues.
Mark it as best answer if it works.
Regards
All Answers
Try this below trigger. I am doing only one field and if that field is filled manually by user it wont populate value from account.
Let me know if you have any issues.
Mark it as best answer if it works.
Regards
Just a small change, Is that possible that, even if there is an existing value present, and user will go and edit and save , it will take the values from the account again ? ( as of now, its taking only if its blank, if there is a value its not updating ), So, where in the code we need to change or add that ?
But if i keep this much ( deleted IF condition), The trigger not working. whereas its not showing any error in saving , but while i edit and save its failing.
trigger trg2MainDriverRelatedFields on Quotation__c (after insert,after update) {
list <Quotation__c > pobs = new list<Quotation__c >();
for(Quotation__c p : [select id,Main_Driver__r.Acccidents_in_the_last_12_mnths__c,
Accidents_in_the_last_12_mnthsMainD__c,Main_Driver__r.Age__c,AgeMainD__c,Main_Driver__r.Contact_Individual_Gender__c,GenderMainD__c,Main_Driver__r.Contact_Individual_Marital_Status__c,Marital_StatusMainD__c,Main_Driver__r.Driving_Experience__c,Driving_ExperienceMainD__c from Quotation__c
Where Id IN: trigger.new]){
system.debug('sumtotal'+p.Main_Driver__r.Acccidents_in_the_last_12_mnths__c);
system.debug('sumtotal'+p.Main_Driver__r.Age__c);
system.debug('sumtotal'+p.Main_Driver__r.Contact_Individual_Gender__c);
system.debug('sumtotal'+p.Main_Driver__r.Contact_Individual_Marital_Status__c);
system.debug('sumtotal'+p.Main_Driver__r.Driving_Experience__c);
Quotation__c pob= p;
pob.Accidents_in_the_last_12_mnthsMainD__c=p.Main_Driver__r.Acccidents_in_the_last_12_mnths__c;
pob.AgeMainD__c=p.Main_Driver__r.Age__c;
pob.GenderMainD__c=p.Main_Driver__r.Contact_Individual_Gender__c;
pob.Marital_StatusMainD__c=p.Main_Driver__r.Contact_Individual_Marital_Status__c;
pob.Driving_ExperienceMainD__c=p.Main_Driver__r.Driving_Experience__c;
pobs.add(pob);
}
update pobs;
}
using visual force we can achive its not salesforce native future
apex class
public class autopopulatechild {
public id accid{set;get;}
public account acc{set;get;}
public contact con{set;get;}
public void child(){
acc=[select id,name,industry from account where id=:accid];
con=new contact();
con.LastName=acc.name;
con.AccountId=acc.id;
con.CleanStatus=acc.industry;
}
}
visual force page
<apex:page controller="autopopulatechild" >
<apex:form>
<apex:inputText value="{!accid}">
<apex:actionSupport event="onchange" action="{!child}" reRender="f"/>
</apex:inputText>
<apex:pageBlock title="contact table " id="f">
<apex:pageBlockSection columns="2">
<apex:inputText value="{!con.lastname}" />
<apex:inputText value="{!con.accountid}" />
<apex:inputText value="{!con.CleanStatus}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
I Hope This Is Help Full For For u If your Problem Is Resolved Please Make It Best Answer It Will Helpe TO Others
and then modify your trigger like below
Regards
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XdABQA0