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
Sourav PSourav P 

Autopopulate the fields based on a look field value from a standard object

Hi, Can anyone help me out with this,
User-added image
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;

        }
}




 
Best Answer chosen by Sourav P
EldonEldon
Hi Sourav,

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. 

 
trigger MainDriverRelatedFields  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 from Quotation__c  
                           Where Id IN: trigger.new]){
       
        system.debug('sumtotal'+p.Main_Driver__r.Acccidents_in_the_last_12_mnths__c);
        if(p.Accidents_in_the_last_12_mnthsMainD__c==null){
            Quotation__c  pob= p;
            pob.Accidents_in_the_last_12_mnthsMainD__c=p.Main_Driver__r.Acccidents_in_the_last_12_mnths__c;
            pobs.add(pob);
        }
        
    }
    update pobs;
}

Let me know if you have any issues.
Mark it as best answer if it works.

Regards
 

All Answers

EldonEldon
Hi Sourav,

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. 

 
trigger MainDriverRelatedFields  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 from Quotation__c  
                           Where Id IN: trigger.new]){
       
        system.debug('sumtotal'+p.Main_Driver__r.Acccidents_in_the_last_12_mnths__c);
        if(p.Accidents_in_the_last_12_mnthsMainD__c==null){
            Quotation__c  pob= p;
            pob.Accidents_in_the_last_12_mnthsMainD__c=p.Main_Driver__r.Acccidents_in_the_last_12_mnths__c;
            pobs.add(pob);
        }
        
    }
    update pobs;
}

Let me know if you have any issues.
Mark it as best answer if it works.

Regards
 
This was selected as the best answer
Sourav PSourav P
Hi Eldon, Thank you so much. I put now all the fields and it worked perfectly.
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 ?
Sourav PSourav P
I think in the if condition, ==null, we need to modify , what should we input there ?
EldonEldon
Just delete that if condition.
Sourav PSourav P
Hi eldon, Thanks
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;
}
sandeep reddy 37sandeep reddy 37
use this coad you are problem is resolved 
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


 
Sourav PSourav P
Thanks Sandeep ...
EldonEldon
If your error is trigger depth reached then you may create the below class
 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
        if(run){
            run=false;
            return true;
        }else{
            return run;
        }
    }
}

and then modify your trigger like below
 
trigger MainDriverRelatedFields  on Quotation__c  (after insert,after update) {
    
    if(checkRecursive.runOnce())
        
    {
        
        //write your code here            
        
    }
    
}

Regards
Sourav PSourav P
Hi Eldon, thanks a lot. I wrote your previous trigger code and put a validation rule and it worked. But i can try this one as well.
Sourav PSourav P
@Eldon, just need your another help in my recently posted another question where i wrote a trigger but its not working , i got stuck, if you have time please. thnx
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XdABQA0