public class AccountContactCreator {
public class AccountContactInput {
@InvocableVariable(required=true)
public String accountName;
@InvocableVariable
public String accountPhone;
@InvocableVariable
public String accountWebsite;
@InvocableVariable(required=true)
public String contactFirstName;
@InvocableVariable(required=true)
public String contactLastName;
@InvocableVariable
public String contactEmail;
@InvocableVariable
public String contactPhone;
}
public class AccountContactOutput {
@InvocableVariable
public String resultMessage;
@InvocableVariable
public Id accountId;
@InvocableVariable
public Id contactId;
}
@InvocableMethod(label='Create Account and Contact' description='Creates Account and associated Contact records')
public static List<AccountContactOutput> createAccountContact(List<AccountContactInput> inputList) {
List<AccountContactOutput> results = new List<AccountContactOutput>();
for (AccountContactInput input : inputList) {
AccountContactOutput output = new AccountContactOutput();
try {
// Check if Account already exists
List<Account> existingAccount = [SELECT Id FROM Account WHERE Name = :input.accountName LIMIT 1];
Account account;
if (existingAccount.size() > 0) {
account = existingAccount[0];
} else {
account = new Account(
Name = input.accountName,
Phone = input.accountPhone,
Website = input.accountWebsite
);
insert account;
}
Contact contact = new Contact(
FirstName = input.contactFirstName,
LastName = input.contactLastName,
Email = input.contactEmail,
Phone = input.contactPhone,
AccountId = account.Id
);
insert contact;
output.resultMessage = 'Account and Contact created successfully.';
output.accountId = account.Id;
output.contactId = contact.Id;
} catch (Exception ex) {
output.resultMessage = 'Error creating Account/Contact: ' + ex.getMessage();
}
results.add(output);
}
return results;
}
}
No comments:
Post a Comment