0

I have a custom object Service Request with a lookup field of Account.Service Request has one more field called Web Email(Email type).I need a trigger to search accounts with the same value in web email and populate the lookup field on service request.

Eg Service Request has web email as abc@gmail.com, I want to search the accounts with email value abc@gmail.com and populate in the lookup field of that service account containing abc@gmail.com.

0

you can query all the accounts with matching email with something like below sample code. Once the query retrieves the results, you can assign the account id value to the lookup field. However you need to come up with the logic if more than one account is exists with matching email address.

List<account> accts=[SELECT ID from ACCOUNT where emailxx=:varEmailxx Limit 1];
if(accts.Size() > 0)ServiceRequest.Account__c=accts[0].id;
0

Got my answer

trigger EmailMatchwithAccountEmail on Service_Request__c (before insert) {
    Map<String,Service_Request__c[]> assotiactedServiceByEmail = new Map<String,Service_Request__c[]>();

    for(Service_Request__c assService : trigger.New) {
        if(!assotiactedServiceByEmail.containsKey(assService.Web_Email__c)){
            assotiactedServiceByEmail.put(assService.Web_Email__c, new Service_Request__c[]{});
        }

        assotiactedServiceByEmail.get(assService.Web_Email__c).add(assService);
    }

    for(Account a : [select Id, Subscription_Email__c, Name from Account where Subscription_Email__c in : assotiactedServiceByEmail.keySet()]){
        for(Service_Request__c assService : assotiactedServiceByEmail.get(a.Subscription_Email__c)){
            assService.Account_Name__c = a.Id;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.